config: verify: break out err_prefix(), and rename

This commit is contained in:
Daniel Eklöf 2019-01-12 12:11:59 +01:00
parent 550d4ad534
commit d2af5b2c84
2 changed files with 30 additions and 27 deletions

View file

@ -13,8 +13,8 @@
#include "modules/battery/battery.h" #include "modules/battery/battery.h"
#include "modules/clock/clock.h" #include "modules/clock/clock.h"
static const char * const char *
err_prefix(const keychain_t *chain, const struct yml_node *node) conf_err_prefix(const keychain_t *chain, const struct yml_node *node)
{ {
static char msg[4096]; static char msg[4096];
int idx = 0; int idx = 0;
@ -35,7 +35,7 @@ conf_verify_string(keychain_t *chain, const struct yml_node *node)
{ {
const char *s = yml_value_as_string(node); const char *s = yml_value_as_string(node);
if (s == NULL) { if (s == NULL) {
LOG_ERR("%s: value must be a string", err_prefix(chain, node)); LOG_ERR("%s: value must be a string", conf_err_prefix(chain, node));
return false; return false;
} }
@ -49,7 +49,7 @@ conf_verify_int(keychain_t *chain, const struct yml_node *node)
return true; return true;
LOG_ERR("%s: value is not an integer: '%s'", LOG_ERR("%s: value is not an integer: '%s'",
err_prefix(chain, node), yml_value_as_string(node)); conf_err_prefix(chain, node), yml_value_as_string(node));
return false; return false;
} }
@ -59,7 +59,7 @@ conf_verify_enum(keychain_t *chain, const struct yml_node *node,
{ {
const char *s = yml_value_as_string(node); const char *s = yml_value_as_string(node);
if (s == NULL) { if (s == NULL) {
LOG_ERR("%s: value must be a string", err_prefix(chain, node)); LOG_ERR("%s: value must be a string", conf_err_prefix(chain, node));
return false; return false;
} }
@ -68,7 +68,7 @@ conf_verify_enum(keychain_t *chain, const struct yml_node *node,
return true; return true;
} }
LOG_ERR("%s: value must be one of:", err_prefix(chain, node)); LOG_ERR("%s: value must be one of:", conf_err_prefix(chain, node));
for (size_t i = 0; i < count; i++) for (size_t i = 0; i < count; i++)
LOG_ERR(" %s", values[i]); LOG_ERR(" %s", values[i]);
@ -80,7 +80,7 @@ conf_verify_dict(keychain_t *chain, const struct yml_node *node,
const struct attr_info info[], size_t count) const struct attr_info info[], size_t count)
{ {
if (!yml_is_dict(node)) { if (!yml_is_dict(node)) {
LOG_ERR("%s: must be a dictionary", err_prefix(chain, node)); LOG_ERR("%s: must be a dictionary", conf_err_prefix(chain, node));
return false; return false;
} }
@ -93,7 +93,7 @@ conf_verify_dict(keychain_t *chain, const struct yml_node *node,
{ {
const char *key = yml_value_as_string(it.key); const char *key = yml_value_as_string(it.key);
if (key == NULL) { if (key == NULL) {
LOG_ERR("%s: key must be a string", err_prefix(chain, it.key)); LOG_ERR("%s: key must be a string", conf_err_prefix(chain, it.key));
return false; return false;
} }
@ -107,7 +107,7 @@ conf_verify_dict(keychain_t *chain, const struct yml_node *node,
} }
if (attr == NULL) { if (attr == NULL) {
LOG_ERR("%s: invalid key: %s", err_prefix(chain, it.key), key); LOG_ERR("%s: invalid key: %s", conf_err_prefix(chain, it.key), key);
return false; return false;
} }
@ -123,7 +123,7 @@ conf_verify_dict(keychain_t *chain, const struct yml_node *node,
if (!info[i].required || exists[i]) if (!info[i].required || exists[i])
continue; continue;
LOG_ERR("%s: missing required key: %s", err_prefix(chain, node), info[i].name); LOG_ERR("%s: missing required key: %s", conf_err_prefix(chain, node), info[i].name);
return false; return false;
} }
@ -135,7 +135,7 @@ conf_verify_color(keychain_t *chain, const struct yml_node *node)
{ {
const char *s = yml_value_as_string(node); const char *s = yml_value_as_string(node);
if (s == NULL) { if (s == NULL) {
LOG_ERR("%s: value must be a string", err_prefix(chain, node)); LOG_ERR("%s: value must be a string", conf_err_prefix(chain, node));
return false; return false;
} }
@ -144,7 +144,7 @@ conf_verify_color(keychain_t *chain, const struct yml_node *node)
if (strlen(s) != 8 || v != 4) { if (strlen(s) != 8 || v != 4) {
LOG_ERR("%s: value must be a color ('rrggbbaa', e.g ff00ffff)", LOG_ERR("%s: value must be a color ('rrggbbaa', e.g ff00ffff)",
err_prefix(chain, node)); conf_err_prefix(chain, node));
return false; return false;
} }
@ -168,7 +168,7 @@ static bool
verify_decoration_stack(keychain_t *chain, const struct yml_node *node) verify_decoration_stack(keychain_t *chain, const struct yml_node *node)
{ {
if (!yml_is_list(node)) { if (!yml_is_list(node)) {
LOG_ERR("%s: must be a list of decorations", err_prefix(chain, node)); LOG_ERR("%s: must be a list of decorations", conf_err_prefix(chain, node));
return false; return false;
} }
@ -190,7 +190,7 @@ verify_decoration(keychain_t *chain, const struct yml_node *node)
if (yml_dict_length(node) != 1) { if (yml_dict_length(node) != 1) {
LOG_ERR("%s: decoration must be a dictionary with a single key; " LOG_ERR("%s: decoration must be a dictionary with a single key; "
"the name of the particle", err_prefix(chain, node)); "the name of the particle", conf_err_prefix(chain, node));
return false; return false;
} }
@ -200,7 +200,7 @@ verify_decoration(keychain_t *chain, const struct yml_node *node)
const char *deco_name = yml_value_as_string(deco); const char *deco_name = yml_value_as_string(deco);
if (deco_name == NULL) { if (deco_name == NULL) {
LOG_ERR("%s: decoration name must be a string", err_prefix(chain, deco)); LOG_ERR("%s: decoration name must be a string", conf_err_prefix(chain, deco));
return false; return false;
} }
@ -243,7 +243,7 @@ verify_decoration(keychain_t *chain, const struct yml_node *node)
} }
LOG_ERR( LOG_ERR(
"%s: invalid decoration name: %s", err_prefix(chain, deco), deco_name); "%s: invalid decoration name: %s", conf_err_prefix(chain, deco), deco_name);
return false; return false;
} }
@ -269,7 +269,7 @@ verify_map_values(keychain_t *chain, const struct yml_node *node)
if (!yml_is_dict(node)) { if (!yml_is_dict(node)) {
LOG_ERR( LOG_ERR(
"%s: must be a dictionary of workspace-name: particle mappings", "%s: must be a dictionary of workspace-name: particle mappings",
err_prefix(chain, node)); conf_err_prefix(chain, node));
return false; return false;
} }
@ -279,8 +279,7 @@ verify_map_values(keychain_t *chain, const struct yml_node *node)
{ {
const char *key = yml_value_as_string(it.key); const char *key = yml_value_as_string(it.key);
if (key == NULL) { if (key == NULL) {
LOG_ERR("%s: key must be a string (a i3 workspace name)", LOG_ERR("%s: key must be a string", conf_err_prefix(chain, it.key));
err_prefix(chain, it.key));
return false; return false;
} }
@ -300,7 +299,7 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node)
if (yml_dict_length(node) != 1) { if (yml_dict_length(node) != 1) {
LOG_ERR("%s: particle must be a dictionary with a single key; " LOG_ERR("%s: particle must be a dictionary with a single key; "
"the name of the particle", err_prefix(chain, node)); "the name of the particle", conf_err_prefix(chain, node));
return false; return false;
} }
@ -310,7 +309,7 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node)
const char *particle_name = yml_value_as_string(particle); const char *particle_name = yml_value_as_string(particle);
if (particle_name == NULL) { if (particle_name == NULL) {
LOG_ERR("%s: particle name must be a string", err_prefix(chain, particle)); LOG_ERR("%s: particle name must be a string", conf_err_prefix(chain, particle));
return false; return false;
} }
@ -396,7 +395,7 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node)
} }
LOG_ERR( LOG_ERR(
"%s: invalid particle name: %s", err_prefix(chain, particle), particle_name); "%s: invalid particle name: %s", conf_err_prefix(chain, particle), particle_name);
return false; return false;
} }
@ -409,7 +408,7 @@ conf_verify_particle(keychain_t *chain, const struct yml_node *node)
return verify_list_items(chain, node); return verify_list_items(chain, node);
else { else {
LOG_ERR("%s: particle must be either a dictionary or a list", LOG_ERR("%s: particle must be either a dictionary or a list",
err_prefix(chain, node)); conf_err_prefix(chain, node));
return false; return false;
} }
} }
@ -449,7 +448,7 @@ verify_module(keychain_t *chain, const struct yml_node *node)
{ {
if (!yml_is_dict(node) || yml_dict_length(node) != 1) { if (!yml_is_dict(node) || yml_dict_length(node) != 1) {
LOG_ERR("%s: module must be a dictionary with a single key; " LOG_ERR("%s: module must be a dictionary with a single key; "
"the name of the module", err_prefix(chain, node)); "the name of the module", conf_err_prefix(chain, node));
return false; return false;
} }
@ -459,7 +458,7 @@ verify_module(keychain_t *chain, const struct yml_node *node)
const char *mod_name = yml_value_as_string(module); const char *mod_name = yml_value_as_string(module);
if (mod_name == NULL) { if (mod_name == NULL) {
LOG_ERR("%s: module name must be a string", err_prefix(chain, module)); LOG_ERR("%s: module name must be a string", conf_err_prefix(chain, module));
return false; return false;
} }
@ -562,7 +561,7 @@ verify_module(keychain_t *chain, const struct yml_node *node)
return true; return true;
} }
LOG_ERR("%s: invalid module name: %s", err_prefix(chain, module), mod_name); LOG_ERR("%s: invalid module name: %s", conf_err_prefix(chain, module), mod_name);
return false; return false;
} }
@ -570,7 +569,7 @@ static bool
verify_module_list(keychain_t *chain, const struct yml_node *node) verify_module_list(keychain_t *chain, const struct yml_node *node)
{ {
if (!yml_is_list(node)) { if (!yml_is_list(node)) {
LOG_ERR("%s: must be a list of modules", err_prefix(chain, node)); LOG_ERR("%s: must be a list of modules", conf_err_prefix(chain, node));
return false; return false;
} }

View file

@ -26,6 +26,10 @@ chain_pop(keychain_t *chain)
tll_pop_back(*chain); tll_pop_back(*chain);
} }
const char *conf_err_prefix(
const keychain_t *chain, const struct yml_node *node);
bool conf_verify_string(keychain_t *chain, const struct yml_node *node); bool conf_verify_string(keychain_t *chain, const struct yml_node *node);
bool conf_verify_int(keychain_t *chain, const struct yml_node *node); bool conf_verify_int(keychain_t *chain, const struct yml_node *node);