improved error messages in TomlTable_get*
This commit is contained in:
parent
3e21766514
commit
bdcc838bb8
@ -27,11 +27,10 @@ void tlibtoml_deinit();
|
|||||||
|
|
||||||
typedef enum TlibtomlError {
|
typedef enum TlibtomlError {
|
||||||
TLIBTOML_OK,
|
TLIBTOML_OK,
|
||||||
TLIBTOML_ERR,
|
|
||||||
TLIBTOML_ERR_OS,
|
|
||||||
TLIBTOML_ERR_NOMEM,
|
|
||||||
TLIBTOML_ERR_SYNTAX,
|
TLIBTOML_ERR_SYNTAX,
|
||||||
TLIBTOML_ERR_UNICODE
|
TLIBTOML_ERR_UNICODE,
|
||||||
|
TLIBTOML_ERR_NOT_FOUND,
|
||||||
|
TLIBTOML_ERR_UNEXPECTED_TYPE,
|
||||||
} TlibtomlError;
|
} TlibtomlError;
|
||||||
ErrorCodePage_declare(TLIBTOML);
|
ErrorCodePage_declare(TLIBTOML);
|
||||||
|
|
||||||
@ -91,6 +90,8 @@ typedef enum TomlType {
|
|||||||
TLIBTOML_BOOLEAN,
|
TLIBTOML_BOOLEAN,
|
||||||
} TomlType;
|
} TomlType;
|
||||||
|
|
||||||
|
str TomlType_toStr(TomlType t);
|
||||||
|
|
||||||
typedef struct TomlValue {
|
typedef struct TomlValue {
|
||||||
TomlType type;
|
TomlType type;
|
||||||
union {
|
union {
|
||||||
|
|||||||
@ -19,12 +19,31 @@ void TomlTable_free(TomlTable* self)
|
|||||||
free(self);
|
free(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#define try_assert_value_found(VAL, KEY) \
|
||||||
|
if(VAL == NULL){ \
|
||||||
|
Return RESULT_ERROR_CODE_FMT( \
|
||||||
|
TLIBTOML, TLIBTOML_ERR_NOT_FOUND, \
|
||||||
|
"can't find '"FMT_str"'", \
|
||||||
|
str_expand(KEY)); \
|
||||||
|
} \
|
||||||
|
|
||||||
|
#define try_assert_value_type(VAL, KEY, TYPE) \
|
||||||
|
if((VAL)->type != TYPE){ \
|
||||||
|
str t_expected_s = TomlType_toStr(TYPE); \
|
||||||
|
str t_got_s = TomlType_toStr((VAL)->type); \
|
||||||
|
Return RESULT_ERROR_CODE_FMT( \
|
||||||
|
TLIBTOML, TLIBTOML_ERR_UNEXPECTED_TYPE, \
|
||||||
|
"expected '"FMT_str"' of type '"FMT_str"', but got '"FMT_str"'", \
|
||||||
|
str_expand(KEY), str_expand(t_expected_s), str_expand(t_got_s)); \
|
||||||
|
} \
|
||||||
|
|
||||||
Result(TomlTable*) TomlTable_get_table(const TomlTable* self, str key)
|
Result(TomlTable*) TomlTable_get_table(const TomlTable* self, str key)
|
||||||
{
|
{
|
||||||
Deferral(1);
|
Deferral(1);
|
||||||
TomlValue* v = TomlTable_get(self, key);
|
TomlValue* v = TomlTable_get(self, key);
|
||||||
try_assert(v != NULL);
|
try_assert_value_found(v, key);
|
||||||
try_assert(v->type == TLIBTOML_TABLE);
|
try_assert_value_type(v, key, TLIBTOML_TABLE);
|
||||||
Return RESULT_VALUE(p, v->value.table);
|
Return RESULT_VALUE(p, v->value.table);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,8 +51,8 @@ Result(TomlArray*) TomlTable_get_array(const TomlTable* self, str key)
|
|||||||
{
|
{
|
||||||
Deferral(1);
|
Deferral(1);
|
||||||
TomlValue* v = TomlTable_get(self, key);
|
TomlValue* v = TomlTable_get(self, key);
|
||||||
try_assert(v != NULL);
|
try_assert_value_found(v, key);
|
||||||
try_assert(v->type == TLIBTOML_ARRAY);
|
try_assert_value_type(v, key, TLIBTOML_ARRAY);
|
||||||
Return RESULT_VALUE(p, v->value.array);
|
Return RESULT_VALUE(p, v->value.array);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,8 +60,8 @@ Result(str*) TomlTable_get_str(const TomlTable* self, str key)
|
|||||||
{
|
{
|
||||||
Deferral(1);
|
Deferral(1);
|
||||||
TomlValue* v = TomlTable_get(self, key);
|
TomlValue* v = TomlTable_get(self, key);
|
||||||
try_assert(v != NULL);
|
try_assert_value_found(v, key);
|
||||||
try_assert(v->type == TLIBTOML_STRING);
|
try_assert_value_type(v, key, TLIBTOML_STRING);
|
||||||
Return RESULT_VALUE(p, v->value.s);
|
Return RESULT_VALUE(p, v->value.s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,8 +69,8 @@ Result(i64) TomlTable_get_integer(const TomlTable* self, str key)
|
|||||||
{
|
{
|
||||||
Deferral(1);
|
Deferral(1);
|
||||||
TomlValue* v = TomlTable_get(self, key);
|
TomlValue* v = TomlTable_get(self, key);
|
||||||
try_assert(v != NULL);
|
try_assert_value_found(v, key);
|
||||||
try_assert(v->type == TLIBTOML_INTEGER);
|
try_assert_value_type(v, key, TLIBTOML_INTEGER);
|
||||||
Return RESULT_VALUE(i, v->value.i);
|
Return RESULT_VALUE(i, v->value.i);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,8 +78,8 @@ Result(f64) TomlTable_get_float(const TomlTable* self, str key)
|
|||||||
{
|
{
|
||||||
Deferral(1);
|
Deferral(1);
|
||||||
TomlValue* v = TomlTable_get(self, key);
|
TomlValue* v = TomlTable_get(self, key);
|
||||||
try_assert(v != NULL);
|
try_assert_value_found(v, key);
|
||||||
try_assert(v->type == TLIBTOML_FLOAT);
|
try_assert_value_type(v, key, TLIBTOML_FLOAT);
|
||||||
Return RESULT_VALUE(f, v->value.f);
|
Return RESULT_VALUE(f, v->value.f);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,8 +87,8 @@ Result(TomlDateTime*) TomlTable_get_datetime(const TomlTable* self, str key)
|
|||||||
{
|
{
|
||||||
Deferral(1);
|
Deferral(1);
|
||||||
TomlValue* v = TomlTable_get(self, key);
|
TomlValue* v = TomlTable_get(self, key);
|
||||||
try_assert(v != NULL);
|
try_assert_value_found(v, key);
|
||||||
try_assert(v->type == TLIBTOML_DATETIME);
|
try_assert_value_type(v, key, TLIBTOML_DATETIME);
|
||||||
Return RESULT_VALUE(p, v->value.dt);
|
Return RESULT_VALUE(p, v->value.dt);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,7 +96,7 @@ Result(bool) TomlTable_get_bool(const TomlTable* self, str key)
|
|||||||
{
|
{
|
||||||
Deferral(1);
|
Deferral(1);
|
||||||
TomlValue* v = TomlTable_get(self, key);
|
TomlValue* v = TomlTable_get(self, key);
|
||||||
try_assert(v != NULL);
|
try_assert_value_found(v, key);
|
||||||
try_assert(v->type == TLIBTOML_BOOLEAN);
|
try_assert_value_type(v, key, TLIBTOML_BOOLEAN);
|
||||||
Return RESULT_VALUE(i, v->value.b);
|
Return RESULT_VALUE(i, v->value.b);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,6 +4,25 @@
|
|||||||
|
|
||||||
#include "toml_internal.h"
|
#include "toml_internal.h"
|
||||||
|
|
||||||
|
Array_declare(str);
|
||||||
|
|
||||||
|
static Array(str) _TomlType_str_array = ARRAY(str, {
|
||||||
|
STR("INVALID_TYPE"),
|
||||||
|
STR("TABLE"),
|
||||||
|
STR("ARRAY"),
|
||||||
|
STR("STRING"),
|
||||||
|
STR("INTEGER"),
|
||||||
|
STR("FLOAT"),
|
||||||
|
STR("DATETIME"),
|
||||||
|
STR("BOOLEAN"),
|
||||||
|
});
|
||||||
|
|
||||||
|
str TomlType_toStr(TomlType t){
|
||||||
|
if((u32)t >= _TomlType_str_array.len)
|
||||||
|
return STR("!! ERROR: INVALID TomlType !!");
|
||||||
|
return _TomlType_str_array.data[t];
|
||||||
|
}
|
||||||
|
|
||||||
TomlValue TomlValue_new(TomlType type)
|
TomlValue TomlValue_new(TomlType type)
|
||||||
{
|
{
|
||||||
TomlValue value = {0};
|
TomlValue value = {0};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user