From 8bc6a0b78375586fa82576101f87d4501ebd4426 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sun, 13 Jan 2019 12:47:20 +0100 Subject: [PATCH] oarticles: don't assume particle content is a dictionary This is done by having each particle implement a top-level verifier function. --- config-verify.c | 4 +++- particle.h | 2 +- particles/empty.c | 16 ++++++++++++---- particles/list.c | 16 ++++++++++++---- particles/map.c | 16 ++++++++++++---- particles/progress-bar.c | 16 ++++++++++++---- particles/ramp.c | 18 +++++++++++++----- particles/string.c | 16 ++++++++++++---- 8 files changed, 77 insertions(+), 27 deletions(-) diff --git a/config-verify.c b/config-verify.c index 41407ec..6dd8d7c 100644 --- a/config-verify.c +++ b/config-verify.c @@ -292,8 +292,10 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node) return false; } + assert(info->verify_conf != NULL); + chain_push(chain, particle_name); - if (!conf_verify_dict(chain, values, info->attrs)) + if (!info->verify_conf(chain, values)) return false; chain_pop(chain); diff --git a/particle.h b/particle.h index fed44ae..f867578 100644 --- a/particle.h +++ b/particle.h @@ -14,11 +14,11 @@ struct particle; struct exposable; struct particle_info { + bool (*verify_conf)(keychain_t *chain, const struct yml_node *node); struct particle *(*from_conf)(const struct yml_node *node, const struct font *parent_font, int left_margin, int right_margin, const char *on_click_template); - const struct attr_info attrs[]; #define PARTICLE_COMMON_ATTRS_COUNT 5 #define PARTICLE_COMMON_ATTRS \ diff --git a/particles/empty.c b/particles/empty.c index 4aa6294..dde25d0 100644 --- a/particles/empty.c +++ b/particles/empty.c @@ -48,9 +48,17 @@ from_conf(const struct yml_node *node, const struct font *parent_font, return empty_new(left_margin, right_margin, on_click_template); } -const struct particle_info plugin_info = { - .from_conf = &from_conf, - .attrs = { +static bool +verify_conf(keychain_t *chain, const struct yml_node *node) +{ + static const struct attr_info attrs[] = { PARTICLE_COMMON_ATTRS, - }, + }; + + return conf_verify_dict(chain, node, attrs); +} + +const struct particle_info plugin_info = { + .verify_conf = &verify_conf, + .from_conf = &from_conf, }; diff --git a/particles/list.c b/particles/list.c index c95f8d9..fadac6f 100644 --- a/particles/list.c +++ b/particles/list.c @@ -198,13 +198,21 @@ from_conf(const struct yml_node *node, const struct font *parent_font, on_click_template); } -const struct particle_info plugin_info = { - .from_conf = &from_conf, - .attrs = { +static bool +verify_conf(keychain_t *chain, const struct yml_node *node) +{ + static const struct attr_info attrs[] = { {"items", true, &conf_verify_particle_list_items}, {"spacing", false, &conf_verify_int}, {"left-spacing", false, &conf_verify_int}, {"right-spacing", false, &conf_verify_int}, PARTICLE_COMMON_ATTRS, - }, + }; + + return conf_verify_dict(chain, node, attrs); +} + +const struct particle_info plugin_info = { + .verify_conf = &verify_conf, + .from_conf = &from_conf, }; diff --git a/particles/map.c b/particles/map.c index d616034..fda5a02 100644 --- a/particles/map.c +++ b/particles/map.c @@ -228,12 +228,20 @@ from_conf(const struct yml_node *node, const struct font *parent_font, default_particle, left_margin, right_margin, on_click_template); } -const struct particle_info plugin_info = { - .from_conf = &from_conf, - .attrs = { +static bool +verify_conf(keychain_t *chain, const struct yml_node *node) +{ + static const struct attr_info attrs[] = { {"tag", true, &conf_verify_string}, {"values", true, &verify_map_values}, {"default", false, &conf_verify_particle}, PARTICLE_COMMON_ATTRS, - }, + }; + + return conf_verify_dict(chain, node, attrs); +} + +const struct particle_info plugin_info = { + .verify_conf = &verify_conf, + .from_conf = &from_conf, }; diff --git a/particles/progress-bar.c b/particles/progress-bar.c index 2526a49..2f110fd 100644 --- a/particles/progress-bar.c +++ b/particles/progress-bar.c @@ -257,9 +257,10 @@ from_conf(const struct yml_node *node, const struct font *parent_font, left_margin, right_margin, on_click_template); } -const struct particle_info plugin_info = { - .from_conf = &from_conf, - .attrs = { +static bool +verify_conf(keychain_t *chain, const struct yml_node *node) +{ + static const struct attr_info attrs[] = { {"tag", true, &conf_verify_string}, {"length", true, &conf_verify_int}, /* TODO: make these optional? Default to empty */ @@ -269,5 +270,12 @@ const struct particle_info plugin_info = { {"empty", true, &conf_verify_particle}, {"indicator", true, &conf_verify_particle}, PARTICLE_COMMON_ATTRS, - }, + }; + + return conf_verify_dict(chain, node, attrs); +} + +const struct particle_info plugin_info = { + .verify_conf = &verify_conf, + .from_conf = &from_conf, }; diff --git a/particles/ramp.c b/particles/ramp.c index 2d81bf1..e7ecaf3 100644 --- a/particles/ramp.c +++ b/particles/ramp.c @@ -178,11 +178,19 @@ from_conf(const struct yml_node *node, const struct font *parent_font, on_click_template); } -const struct particle_info plugin_info = { - .from_conf = &from_conf, - .attrs = { +static bool +verify_conf(keychain_t *chain, const struct yml_node *node) +{ + static const struct attr_info attrs[] = { {"tag", true, &conf_verify_string}, {"items", true, &conf_verify_particle_list_items}, - PARTICLE_COMMON_ATTRS, - }, + PARTICLE_COMMON_ATTRS, + }; + + return conf_verify_dict(chain, node, attrs); +} + +const struct particle_info plugin_info = { + .verify_conf = &verify_conf, + .from_conf = &from_conf, }; diff --git a/particles/string.c b/particles/string.c index 5e3863d..b008805 100644 --- a/particles/string.c +++ b/particles/string.c @@ -171,13 +171,21 @@ from_conf(const struct yml_node *node, const struct font *parent_font, fg_color, left_margin, right_margin, on_click_template); } -const struct particle_info plugin_info = { - .from_conf = &from_conf, - .attrs = { +static bool +verify_conf(keychain_t *chain, const struct yml_node *node) +{ + static const struct attr_info attrs[] = { {"text", true, &conf_verify_string}, {"max", false, &conf_verify_int}, {"font", false, &conf_verify_font}, {"foreground", false, &conf_verify_color}, PARTICLE_COMMON_ATTRS, - }, + }; + + return conf_verify_dict(chain, node, attrs); +} + +const struct particle_info plugin_info = { + .verify_conf = &verify_conf, + .from_conf = &from_conf, };