Add error func to node deserialization

This commit is contained in:
Timm Bäder 2019-04-24 18:30:46 +02:00 committed by Benjamin Otte
parent 5530331b35
commit dde69802cd
9 changed files with 102 additions and 33 deletions

View File

@ -387,12 +387,13 @@ gsk_render_node_write_to_file (GskRenderNode *node,
* error.
**/
GskRenderNode *
gsk_render_node_deserialize (GBytes *bytes,
GError **error)
gsk_render_node_deserialize (GBytes *bytes,
GskParseErrorFunc error_func,
gpointer user_data)
{
GskRenderNode *node = NULL;
node = gsk_render_node_deserialize_from_bytes (bytes, error);
node = gsk_render_node_deserialize_from_bytes (bytes, error_func, user_data);
return node;
}

View File

@ -25,6 +25,7 @@
#include <gsk/gskroundedrect.h>
#include <gsk/gsktypes.h>
#include <gtk/css/gtkcss.h>
G_BEGIN_DECLS
@ -52,6 +53,10 @@ struct _GskShadow
float radius;
};
typedef void (* GskParseErrorFunc) (const GtkCssSection *section,
const GError *error,
gpointer user_data);
GDK_AVAILABLE_IN_ALL
GType gsk_render_node_get_type (void) G_GNUC_CONST;
@ -81,8 +86,9 @@ gboolean gsk_render_node_write_to_file (GskRenderNode *
const char *filename,
GError **error);
GDK_AVAILABLE_IN_ALL
GskRenderNode * gsk_render_node_deserialize (GBytes *bytes,
GError **error);
GskRenderNode * gsk_render_node_deserialize (GBytes *bytes,
GskParseErrorFunc error_func,
gpointer user_data);
GDK_AVAILABLE_IN_ALL
GskRenderNode * gsk_debug_node_new (GskRenderNode *child,

View File

@ -1072,28 +1072,34 @@ gsk_render_node_parser_error (GtkCssParser *parser,
const GError *error,
gpointer user_data)
{
GString **error_string = user_data;
struct {
GskParseErrorFunc error_func;
gpointer user_data;
} *error_func_pair = user_data;
if (!*error_string)
*error_string = g_string_new (NULL);
if (error_func_pair->error_func)
{
GtkCssSection *section = gtk_css_section_new (gtk_css_parser_get_file (parser), start, end);
g_string_append_printf (*error_string,
"ERROR: %zu:%zu: %s\n",
start->lines + 1,
start->line_chars,
error->message);
error_func_pair->error_func (section, error, error_func_pair->user_data);
gtk_css_section_unref (section);
}
}
GskRenderNode *
gsk_render_node_deserialize_from_bytes (GBytes *bytes,
GError **error)
gsk_render_node_deserialize_from_bytes (GBytes *bytes,
GskParseErrorFunc error_func,
gpointer user_data)
{
GskRenderNode *root = NULL;
GtkCssParser *parser;
GString *error_string = NULL;
struct {
GskParseErrorFunc error_func;
gpointer user_data;
} error_func_pair = { error_func, user_data };
parser = gtk_css_parser_new_for_bytes (bytes, NULL, NULL, gsk_render_node_parser_error,
&error_string, NULL);
&error_func_pair, NULL);
root = parse_container_node (parser);
if (root && gsk_container_node_get_n_children (root) == 1)
@ -1107,12 +1113,6 @@ gsk_render_node_deserialize_from_bytes (GBytes *bytes,
gtk_css_parser_unref (parser);
if (error_string != NULL)
{
*error = g_error_new_literal (GTK_CSS_PARSER_ERROR, 0, error_string->str);
g_string_free (error_string, TRUE);
}
return root;
}

View File

@ -4,8 +4,9 @@
#include "gskrendernode.h"
GskRenderNode * gsk_render_node_deserialize_from_bytes (GBytes *bytes,
GError **error);
GskRenderNode * gsk_render_node_deserialize_from_bytes (GBytes *bytes,
GskParseErrorFunc error_func,
gpointer user_data);
char * gsk_render_node_serialize_to_string (GskRenderNode *root);
#endif

View File

@ -13,6 +13,19 @@ static GOptionEntry options[] = {
{ NULL }
};
static void
deserialize_error_func (const GtkCssSection *section,
const GError *error,
gpointer user_data)
{
char *section_str = gtk_css_section_to_string (section);
g_warning ("Error at %s: %s", section_str, error->message);
free (section_str);
}
int
main(int argc, char **argv)
{
@ -66,7 +79,7 @@ main(int argc, char **argv)
}
start = g_get_monotonic_time ();
node = gsk_render_node_deserialize (bytes, &error);
node = gsk_render_node_deserialize (bytes, deserialize_error_func, NULL);
end = g_get_monotonic_time ();
if (benchmark)
{
@ -78,8 +91,6 @@ main(int argc, char **argv)
if (node == NULL)
{
g_printerr ("Invalid node file: %s\n", error->message);
g_clear_error (&error);
return 1;
}

View File

@ -108,6 +108,18 @@ gtk_node_view_class_init (GtkNodeViewClass *klass)
widget_class->snapshot = gtk_node_view_snapshot;
}
static void
deserialize_error_func (const GtkCssSection *section,
const GError *error,
gpointer user_data)
{
char *section_str = gtk_css_section_to_string (section);
g_warning ("Error at %s: %s", section_str, error->message);
free (section_str);
}
int
main (int argc, char **argv)
{
@ -152,7 +164,7 @@ main (int argc, char **argv)
}
bytes = g_bytes_new_take (contents, len);
GTK_NODE_VIEW (nodeview)->node = gsk_render_node_deserialize (bytes, &error);
GTK_NODE_VIEW (nodeview)->node = gsk_render_node_deserialize (bytes, deserialize_error_func, NULL);
g_bytes_unref (bytes);
if (GTK_NODE_VIEW (nodeview)->node == NULL)

View File

@ -54,6 +54,18 @@ save_image (cairo_surface_t *surface,
g_free (filename);
}
static void
deserialize_error_func (const GtkCssSection *section,
const GError *error,
gpointer user_data)
{
char *section_str = gtk_css_section_to_string (section);
g_error ("Error at %s: %s", section_str, error->message);
free (section_str);
}
/*
* Arguments:
* 1) .node file to compare
@ -101,7 +113,7 @@ main (int argc, char **argv)
}
bytes = g_bytes_new_take (contents, len);
node = gsk_render_node_deserialize (bytes, &error);
node = gsk_render_node_deserialize (bytes, deserialize_error_func, NULL);
g_bytes_unref (bytes);
g_assert_no_error (error);

View File

@ -1,5 +1,19 @@
#include <gtk/gtk.h>
static void
deserialize_error_func (const GtkCssSection *section,
const GError *error,
gpointer user_data)
{
char *section_str = gtk_css_section_to_string (section);
/* We want to parse invalid node files in this test and simply assert that the
* parser doesn't crash. So, just g_message() here instead of a warning or error. */
g_message ("Error at %s: %s", section_str, error->message);
free (section_str);
}
int
main (int argc, char **argv)
{
@ -17,7 +31,7 @@ main (int argc, char **argv)
g_assert_no_error (error);
g_assert (bytes != NULL);
node = gsk_render_node_deserialize (bytes, &error);
node = gsk_render_node_deserialize (bytes, deserialize_error_func, NULL);
if (error)
g_test_message ("Error: %s\n", error->message);

View File

@ -1,5 +1,17 @@
#include <gtk/gtk.h>
static void
deserialize_error_func (const GtkCssSection *section,
const GError *error,
gpointer user_data)
{
char *section_str = gtk_css_section_to_string (section);
g_error ("Error at %s: %s", section_str, error->message);
free (section_str);
}
int
main (int argc, char **argv)
{
@ -18,14 +30,14 @@ main (int argc, char **argv)
g_assert_no_error (error);
g_assert (bytes != NULL);
node = gsk_render_node_deserialize (bytes, &error);
node = gsk_render_node_deserialize (bytes, deserialize_error_func, NULL);
g_assert_no_error (error);
/* Now serialize */
g_bytes_unref (bytes);
bytes = gsk_render_node_serialize (node);
/* and deserialize again... */
deserialized = gsk_render_node_deserialize (bytes, &error);
deserialized = gsk_render_node_deserialize (bytes, deserialize_error_func, NULL);
if (error)
g_message ("OUTPUT:\n%.*s", (int)g_bytes_get_size (bytes), (char *)g_bytes_get_data (bytes, NULL));