From 73b8bf13463bc6641925ccb72ea5a43179df9b87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sun, 13 Jan 2019 10:34:15 +0100 Subject: [PATCH] particle/map: expose info through the new struct particle_info struct --- config-verify.c | 39 ++-------------------- config.c | 36 ++------------------ particles/map.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++--- particles/map.h | 10 +----- 4 files changed, 88 insertions(+), 84 deletions(-) diff --git a/config-verify.c b/config-verify.c index b8659fa..1a99d9f 100644 --- a/config-verify.c +++ b/config-verify.c @@ -11,6 +11,7 @@ #include "particles/empty.h" #include "particles/list.h" +#include "particles/map.h" const char * conf_err_prefix(const keychain_t *chain, const struct yml_node *node) @@ -262,35 +263,6 @@ conf_verify_particle_list_items(keychain_t *chain, const struct yml_node *node) return true; } -static bool -verify_map_values(keychain_t *chain, const struct yml_node *node) -{ - if (!yml_is_dict(node)) { - LOG_ERR( - "%s: must be a dictionary of workspace-name: particle mappings", - conf_err_prefix(chain, node)); - return false; - } - - for (struct yml_dict_iter it = yml_dict_iter(node); - it.key != NULL; - yml_dict_next(&it)) - { - const char *key = yml_value_as_string(it.key); - if (key == NULL) { - LOG_ERR("%s: key must be a string", conf_err_prefix(chain, it.key)); - return false; - } - - if (!conf_verify_particle(chain_push(chain, key), it.value)) - return false; - - chain_pop(chain); - } - - return true; -} - static bool conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node) { @@ -318,13 +290,6 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node) {"right-margin", false, &conf_verify_int}, \ {"on-click", false, &conf_verify_string}, - static const struct attr_info map[] = { - {"tag", true, &conf_verify_string}, - {"values", true, &verify_map_values}, - {"default", false, &conf_verify_particle}, - COMMON_ATTRS - }; - static const struct attr_info progress_bar[] = { {"tag", true, &conf_verify_string}, {"length", true, &conf_verify_int}, @@ -360,6 +325,7 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node) } particles_v2[] = { {"empty", &particle_empty}, {"list", &particle_list}, + {"map", &particle_map}, }; static const struct { @@ -367,7 +333,6 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node) const struct attr_info *attrs; size_t count; } particles[] = { - {"map", map, sizeof(map) / sizeof(map[0])}, {"progress-bar", progress_bar, sizeof(progress_bar) / sizeof(progress_bar[0])}, {"ramp", ramp, sizeof(ramp) / sizeof(ramp[0])}, {"string", string, sizeof(string) / sizeof(string[0])}, diff --git a/config.c b/config.c index 3ddfca3..c2845fc 100644 --- a/config.c +++ b/config.c @@ -161,36 +161,6 @@ particle_string_from_config(const struct yml_node *node, } -static struct particle * -particle_map_from_config(const struct yml_node *node, - const struct font *parent_font, - int left_margin, int right_margin, - const char *on_click_template) -{ - const struct yml_node *tag = yml_get_value(node, "tag"); - const struct yml_node *values = yml_get_value(node, "values"); - const struct yml_node *def = yml_get_value(node, "default"); - - struct particle_map particle_map[yml_dict_length(values)]; - - size_t idx = 0; - for (struct yml_dict_iter it = yml_dict_iter(values); - it.key != NULL; - yml_dict_next(&it), idx++) - { - particle_map[idx].tag_value = yml_value_as_string(it.key); - particle_map[idx].particle = conf_to_particle(it.value, parent_font); - } - - struct particle *default_particle = def != NULL - ? conf_to_particle(def, parent_font) - : NULL; - - return particle_map_new( - yml_value_as_string(tag), particle_map, yml_dict_length(values), - default_particle, left_margin, right_margin, on_click_template); -} - static struct particle * particle_ramp_from_config(const struct yml_node *node, const struct font *parent_font, @@ -288,13 +258,13 @@ conf_to_particle(const struct yml_node *node, const struct font *parent_font) else if (strcmp(type, "list") == 0) ret = particle_list.from_conf( pair.value, parent_font, left, right, on_click_template); + else if (strcmp(type, "map") == 0) + ret = particle_map.from_conf( + pair.value, parent_font, left, right, on_click_template); else if (strcmp(type, "string") == 0) ret = particle_string_from_config( pair.value, parent_font, left, right, on_click_template); - else if (strcmp(type, "map") == 0) - ret = particle_map_from_config( - pair.value, parent_font, left, right, on_click_template); else if (strcmp(type, "ramp") == 0) ret = particle_ramp_from_config( pair.value, parent_font, left, right, on_click_template); diff --git a/particles/map.c b/particles/map.c index 5094fb2..fee606b 100644 --- a/particles/map.c +++ b/particles/map.c @@ -4,6 +4,15 @@ #include #include +#define LOG_MODULE "map" +#include "../log.h" +#include "../config.h" + +struct particle_map { + const char *tag_value; + struct particle *particle; +}; + struct private { char *tag; struct particle *default_particle; @@ -136,11 +145,11 @@ particle_destroy(struct particle *particle) particle_default_destroy(particle); } -struct particle * -particle_map_new(const char *tag, const struct particle_map *particle_map, - size_t count, struct particle *default_particle, - int left_margin, int right_margin, - const char *on_click_template) +static struct particle * +map_new(const char *tag, const struct particle_map *particle_map, + size_t count, struct particle *default_particle, + int left_margin, int right_margin, + const char *on_click_template) { struct particle *particle = particle_common_new( left_margin, right_margin, on_click_template); @@ -161,3 +170,71 @@ particle_map_new(const char *tag, const struct particle_map *particle_map, particle->private = priv; return particle; } + +static bool +verify_map_values(keychain_t *chain, const struct yml_node *node) +{ + if (!yml_is_dict(node)) { + LOG_ERR( + "%s: must be a dictionary of workspace-name: particle mappings", + conf_err_prefix(chain, node)); + return false; + } + + for (struct yml_dict_iter it = yml_dict_iter(node); + it.key != NULL; + yml_dict_next(&it)) + { + const char *key = yml_value_as_string(it.key); + if (key == NULL) { + LOG_ERR("%s: key must be a string", conf_err_prefix(chain, it.key)); + return false; + } + + if (!conf_verify_particle(chain_push(chain, key), it.value)) + return false; + + chain_pop(chain); + } + + return true; +} + +static 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 yml_node *tag = yml_get_value(node, "tag"); + const struct yml_node *values = yml_get_value(node, "values"); + const struct yml_node *def = yml_get_value(node, "default"); + + struct particle_map particle_map[yml_dict_length(values)]; + + size_t idx = 0; + for (struct yml_dict_iter it = yml_dict_iter(values); + it.key != NULL; + yml_dict_next(&it), idx++) + { + particle_map[idx].tag_value = yml_value_as_string(it.key); + particle_map[idx].particle = conf_to_particle(it.value, parent_font); + } + + struct particle *default_particle = def != NULL + ? conf_to_particle(def, parent_font) + : NULL; + + return map_new( + yml_value_as_string(tag), particle_map, yml_dict_length(values), + default_particle, left_margin, right_margin, on_click_template); +} + +const struct particle_info particle_map = { + .from_conf = &from_conf, + .attr_count = PARTICLE_COMMON_ATTRS_COUNT + 3, + .attrs = { + {"tag", true, &conf_verify_string}, + {"values", true, &verify_map_values}, + {"default", false, &conf_verify_particle}, + PARTICLE_COMMON_ATTRS, + }, +}; diff --git a/particles/map.h b/particles/map.h index 77dcee2..c9ccb13 100644 --- a/particles/map.h +++ b/particles/map.h @@ -1,12 +1,4 @@ #pragma once #include "../particle.h" -struct particle_map { - const char *tag_value; - struct particle *particle; -}; - -struct particle *particle_map_new( - const char *tag, const struct particle_map *particle_map, size_t count, - struct particle *default_particle, int left_margin, int right_margin, - const char *on_click_template); +extern const struct particle_info particle_map;