sam701 / zig-toml

Zig TOML (v1.0.0) parser
MIT License
28 stars 7 forks source link

Can't parse HashMap #9

Open sirenkovladd opened 2 months ago

sirenkovladd commented 2 months ago

this is an example of toml

[tasks.a]
cmd = "echo 123"

[tasks.b]
cmd = "echo 123"

I need a possible way to parse this tasks, I can't predict the keys

Thank you

sam701 commented 2 months ago

This is a good point!

Arbitrary HashMaps are not supported yet, e.g. HashMap(string, MyValueType...). I do not know yet how to map toml.Value to any possible MyValueType. I'm open to suggestions.

I have just pushed a commit that enables structs to have a field of type toml.Table. Here is an example from the tests.

sirenkovladd commented 2 months ago

I was researching how the json parser works and I really liked the approach how it was done in the kernel here you can find an example of how to use https://github.com/ziglang/zig/blob/master/lib/std/json/hashmap_test.zig

and here is exactly how it checks what the hashmap should be https://github.com/ziglang/zig/blob/master/lib/std/json/static.zig#L321-L323

the main idea is that it checks whether the structure has a key function (jsonParse) and if so, simply passes it the parameters

I'm not one hundred percent sure yet, but with a quick glance, it seemed that if you wrote your own scanner, and you can pass it to json functions and most of the code from this library will simply disappear

sirenkovladd commented 2 months ago

Anyway, thank you for the quick fix

sam701 commented 2 months ago

Thanks for pointing me to the "official" json parser. 👍 I see they changed quite a bit. I will look into.