Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added toml_kind_* to inspect item types #92

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

jakergrossman
Copy link

@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