tomllib
tomllib copied to clipboard
Can I access and reconstruct all parsed information (including commends and whitespace)?
Toml representation in src/types.rs seems to be simplified and not enough to constuct a tree of toml objects that produces original toml document with original formatting.
Is tomllib a proper library for the following application idea:
Convert toml to line-based format and back, like xml2, but for TOML instead of XML. Something like in this imaginary session:
$ cat test.toml
[section1]
var1="qwe"
var2=456
# A comment
var3 = [ "qqq" ]
$ cat test.toml | toml2lines
/section1/var1="qwe
/section1/#=
/section1/var2=456
/section1/#=# A comment
/section1/var3/0=qqq
$ cat test.toml | toml2lines | lines2toml
[section1]
var1="qwe"
var2=456
# A comment
var3 = [ "qqq" ]
$ cat test.toml | toml2lines | grep var3 | lines2toml
[section1]
var3 = [ "qqq" ]
Maybe making all things in src/internals/ast reachable by Serde and/or making them public API should make it possible?
The internal AST has been abstracted away to provide a simpler and more stable interface. It currently only gives access to key/value pairs with no ordering. The idea is to eventually allow access to and modification of all parts of the document including whitespaces and comments.
So currently you can't do this:
$ cat test.toml | toml2lines
/section1/var1="qwe
/section1/#=
/section1/var2=456
/section1/#=# A comment
/section1/var3/0=qqq
$ cat test.toml | toml2lines | lines2toml
[section1]
var1="qwe"
var2=456
# A comment
var3 = [ "qqq" ]
because there's no API access to comments and whitespace, there's no concept of "lines" in the API and there's no order to keys/values, but want to eventually add all of this ability in. It will take some time though.
Maybe making all things in src/internals/ast reachable by Serde and/or making them public API should make it possible?
I don't want to allow access to the underlying AST because it is messy right now and very likely to change with every release. I don't know about Serde, can you tell me about it and/or how I could allow access to internals to Serde?
I hasn't tried Serde as well so far...
Do you think tools like toml2lines should use nom directly, as API surface between the tool and toml is too wide to make a good library?