From bdcc838bb8cf55aa22d772340077266c23b83864 Mon Sep 17 00:00:00 2001 From: Timerix Date: Thu, 27 Nov 2025 01:24:27 +0500 Subject: [PATCH] improved error messages in TomlTable_get* --- include/tlibtoml.h | 9 +++++---- src/TomlTable.c | 47 ++++++++++++++++++++++++++++++++-------------- src/TomlValue.c | 19 +++++++++++++++++++ 3 files changed, 57 insertions(+), 18 deletions(-) diff --git a/include/tlibtoml.h b/include/tlibtoml.h index bce6280..4c009d9 100644 --- a/include/tlibtoml.h +++ b/include/tlibtoml.h @@ -27,11 +27,10 @@ void tlibtoml_deinit(); typedef enum TlibtomlError { TLIBTOML_OK, - TLIBTOML_ERR, - TLIBTOML_ERR_OS, - TLIBTOML_ERR_NOMEM, TLIBTOML_ERR_SYNTAX, - TLIBTOML_ERR_UNICODE + TLIBTOML_ERR_UNICODE, + TLIBTOML_ERR_NOT_FOUND, + TLIBTOML_ERR_UNEXPECTED_TYPE, } TlibtomlError; ErrorCodePage_declare(TLIBTOML); @@ -91,6 +90,8 @@ typedef enum TomlType { TLIBTOML_BOOLEAN, } TomlType; +str TomlType_toStr(TomlType t); + typedef struct TomlValue { TomlType type; union { diff --git a/src/TomlTable.c b/src/TomlTable.c index 29d3d93..036f255 100644 --- a/src/TomlTable.c +++ b/src/TomlTable.c @@ -19,12 +19,31 @@ void TomlTable_free(TomlTable* 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) { Deferral(1); TomlValue* v = TomlTable_get(self, key); - try_assert(v != NULL); - try_assert(v->type == TLIBTOML_TABLE); + try_assert_value_found(v, key); + try_assert_value_type(v, key, TLIBTOML_TABLE); Return RESULT_VALUE(p, v->value.table); } @@ -32,8 +51,8 @@ Result(TomlArray*) TomlTable_get_array(const TomlTable* self, str key) { Deferral(1); TomlValue* v = TomlTable_get(self, key); - try_assert(v != NULL); - try_assert(v->type == TLIBTOML_ARRAY); + try_assert_value_found(v, key); + try_assert_value_type(v, key, TLIBTOML_ARRAY); Return RESULT_VALUE(p, v->value.array); } @@ -41,8 +60,8 @@ Result(str*) TomlTable_get_str(const TomlTable* self, str key) { Deferral(1); TomlValue* v = TomlTable_get(self, key); - try_assert(v != NULL); - try_assert(v->type == TLIBTOML_STRING); + try_assert_value_found(v, key); + try_assert_value_type(v, key, TLIBTOML_STRING); Return RESULT_VALUE(p, v->value.s); } @@ -50,8 +69,8 @@ Result(i64) TomlTable_get_integer(const TomlTable* self, str key) { Deferral(1); TomlValue* v = TomlTable_get(self, key); - try_assert(v != NULL); - try_assert(v->type == TLIBTOML_INTEGER); + try_assert_value_found(v, key); + try_assert_value_type(v, key, TLIBTOML_INTEGER); Return RESULT_VALUE(i, v->value.i); } @@ -59,8 +78,8 @@ Result(f64) TomlTable_get_float(const TomlTable* self, str key) { Deferral(1); TomlValue* v = TomlTable_get(self, key); - try_assert(v != NULL); - try_assert(v->type == TLIBTOML_FLOAT); + try_assert_value_found(v, key); + try_assert_value_type(v, key, TLIBTOML_FLOAT); Return RESULT_VALUE(f, v->value.f); } @@ -68,8 +87,8 @@ Result(TomlDateTime*) TomlTable_get_datetime(const TomlTable* self, str key) { Deferral(1); TomlValue* v = TomlTable_get(self, key); - try_assert(v != NULL); - try_assert(v->type == TLIBTOML_DATETIME); + try_assert_value_found(v, key); + try_assert_value_type(v, key, TLIBTOML_DATETIME); Return RESULT_VALUE(p, v->value.dt); } @@ -77,7 +96,7 @@ Result(bool) TomlTable_get_bool(const TomlTable* self, str key) { Deferral(1); TomlValue* v = TomlTable_get(self, key); - try_assert(v != NULL); - try_assert(v->type == TLIBTOML_BOOLEAN); + try_assert_value_found(v, key); + try_assert_value_type(v, key, TLIBTOML_BOOLEAN); Return RESULT_VALUE(i, v->value.b); } diff --git a/src/TomlValue.c b/src/TomlValue.c index cef004d..52cb2ca 100644 --- a/src/TomlValue.c +++ b/src/TomlValue.c @@ -4,6 +4,25 @@ #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 value = {0};