[gtk/wip/baedert/nodeeditor: 20/23] Add error func to node deserialization



commit 6a81de36b55e4a719fb832b29817b5cd77027733
Author: Timm Bäder <mail baedert org>
Date:   Wed Apr 24 18:30:46 2019 +0200

    Add error func to node deserialization

 gsk/gskrendernode.c                   |  7 ++++---
 gsk/gskrendernode.h                   | 10 ++++++++--
 gsk/gskrendernodeparser.c             | 37 ++++++++++++++++++-----------------
 gsk/gskrendernodeparserprivate.h      |  5 +++--
 tests/rendernode.c                    | 17 +++++++++++++---
 tests/showrendernode.c                | 14 ++++++++++++-
 testsuite/gsk/compare-render.c        | 14 ++++++++++++-
 testsuite/gsk/node-parser.c           | 16 ++++++++++++++-
 testsuite/gsk/serialize-deserialize.c | 16 +++++++++++++--
 9 files changed, 103 insertions(+), 33 deletions(-)
---
diff --git a/gsk/gskrendernode.c b/gsk/gskrendernode.c
index 5f30c70b05..1b1dd2d78f 100644
--- a/gsk/gskrendernode.c
+++ b/gsk/gskrendernode.c
@@ -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;
 }
diff --git a/gsk/gskrendernode.h b/gsk/gskrendernode.h
index 2aa270cb37..b1ae30785e 100644
--- a/gsk/gskrendernode.h
+++ b/gsk/gskrendernode.h
@@ -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,
diff --git a/gsk/gskrendernodeparser.c b/gsk/gskrendernodeparser.c
index d32294edab..9f87052806 100644
--- a/gsk/gskrendernodeparser.c
+++ b/gsk/gskrendernodeparser.c
@@ -1158,16 +1158,19 @@ 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_func_pair->error_func)
+    {
+      GtkCssSection *section = gtk_css_section_new (gtk_css_parser_get_file (parser), start, end);
 
-  if (!*error_string)
-    *error_string = g_string_new (NULL);
+      error_func_pair->error_func (section, error, error_func_pair->user_data);
+      gtk_css_section_unref (section);
+    }
 
-  g_string_append_printf (*error_string,
-                          "ERROR: %zu:%zu: %s\n",
-                          start->lines + 1,
-                          start->line_chars,
-                          error->message);
 #if 0
   g_print ("ERROR: %zu:%zu: %s\n",
               start->lines + 1,
@@ -1177,15 +1180,19 @@ gsk_render_node_parser_error (GtkCssParser         *parser,
 }
 
 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)
@@ -1199,12 +1206,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;
 }
 
diff --git a/gsk/gskrendernodeparserprivate.h b/gsk/gskrendernodeparserprivate.h
index 9d72643c00..a506244429 100644
--- a/gsk/gskrendernodeparserprivate.h
+++ b/gsk/gskrendernodeparserprivate.h
@@ -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
diff --git a/tests/rendernode.c b/tests/rendernode.c
index e0f5e1f9ec..4d69693c65 100644
--- a/tests/rendernode.c
+++ b/tests/rendernode.c
@@ -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;
     }
 
diff --git a/tests/showrendernode.c b/tests/showrendernode.c
index afb8ac2974..bd1e502cd9 100644
--- a/tests/showrendernode.c
+++ b/tests/showrendernode.c
@@ -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)
diff --git a/testsuite/gsk/compare-render.c b/testsuite/gsk/compare-render.c
index 642252fdd7..6c868849a9 100644
--- a/testsuite/gsk/compare-render.c
+++ b/testsuite/gsk/compare-render.c
@@ -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);
diff --git a/testsuite/gsk/node-parser.c b/testsuite/gsk/node-parser.c
index 397b13f69f..9db6df204b 100644
--- a/testsuite/gsk/node-parser.c
+++ b/testsuite/gsk/node-parser.c
@@ -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);
 
diff --git a/testsuite/gsk/serialize-deserialize.c b/testsuite/gsk/serialize-deserialize.c
index 21621e3f95..300f617468 100644
--- a/testsuite/gsk/serialize-deserialize.c
+++ b/testsuite/gsk/serialize-deserialize.c
@@ -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));
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]