Skip to content

added toml_kind_* to inspect item types#92

Draft
jakergrossman wants to merge 4 commits intocktan:masterfrom
jakergrossman:toml-kind
Draft

added toml_kind_* to inspect item types#92
jakergrossman wants to merge 4 commits intocktan:masterfrom
jakergrossman:toml-kind

Conversation

@jakergrossman
Copy link
Copy Markdown

@jakergrossman jakergrossman commented Mar 16, 2024

Rationale

While using this for something I wanted to allow multiple types for an item, and more to the point, check that an item was one of a set of types. I found myself using an ugly idiom
where I was blindly trying toml_*_in and checking the result to know when it was the right type.

With access to the innards of the opaque types, this set of fairly straightforward functions allow a more elegant idiom:

# example.toml
name = "example"
major_version = 0
// prog.c
#include <toml.h>

int main()
{
    // ... omitted for brevity ...
    toml_table_t* conf = toml_parse_file("example.toml", errbuf, sizeof(errbuf));
    // ... omitted for brevity ...

    printf("%c\n", toml_kind_in(conf, "name")); // 's'
    printf("%c\n", toml_kind_in(conf, "major_version")); // 'i'

    printf("%d\n", toml_kind_one_of_in(conf, "name", "sid"); // 1
    printf("%d\n", toml_kind_one_of_in(conf, "name", "id"); // 0
    
}

The Solution

Adds a new toml_kind_t type:

enum toml_kind_t {
  TOML_KIND_UNKNOWN = 'u',
  TOML_KIND_STRING = 's',
  TOML_KIND_INT    = 'i',
  TOML_KIND_BOOL   = 'b',
  TOML_KIND_DOUBLE = 'd',
  TOML_KIND_TABLE  = 'B', // changed from previous version
  TOML_KIND_ARRAY  = 'a',
  TOML_KIND_DATE   = 'D',
  TOML_KIND_TIME   = 't',
  TOML_KIND_TIMESTAMP = 'T',
  TOML_KIND_MIXED = 'm',
  TOML_KIND_VALUE = 'v',
};

Unfortunately TOML_KIND_TABLE had to be disambiguated with TOML_KIND_TIME, and now functions that used to return 't' for table (like toml_array_kind()) now return 'B'. I am not sure if this is the best solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant