samuelcolvin / rtoml

A fast TOML library for python implemented in rust.

Home Page:https://pypi.org/project/rtoml/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Preserving order

manfredlotz opened this issue · comments

When I read in a TOML file and then dump it the order is not preserved. Is this intentional? It would be nice if order could be preserved either by default or by specifying an option.

Example:
I read

[section]
z = 'some'
a = 1

[other]
b = 41
a = "aval"

Dump it gives:

[other]
a = 1
b = 41

[section]
a = 1
z = 1

I believe this is currently a limitation of toml-rs which rtoml is built on.

See toml-rs/toml-rs#380 or a discussion of preserving order in toml-rs.

Once toml-rs supports order it should be just a matter of updating the dendency and rtoml will be able to preserve order too.

Humm, it looks like toml-rs might already have a preserve_order feature, albeit undocumented toml-rs/toml-rs#300.

PR welcome to try and use that feature in this library.

Still an open question over whether the above issue means preserve_order is [partially] broken.

Yes, I also saw it. I could make it work by just adding this to Cargo.toml:

toml = {version = "0.5.8", features = ["preserve_order"]}
indexmap = { version = "1.6", optional = true }

Not sure about optional = true though.

I tested by running:

./toml_test.py < ../Cargo.toml

I think no need for the optional = true, please create a PR and add tests.

I think I don't even need indexmap at all as this is taken care of by toml.

I will create a PR...

For the PR: shall I also commit Cargo.lock?

PR done. Sorry, that I forgot to run make lint before creating the PR. Hope, that otherwise things are ok.

Thanks for taking over the PR and for the constructive discussions.

commented

toml can reserver the order, like this:

toml.loads(content, _dict=OrderedDict)

Sorry, this was fixed in #19, forgot to close the issue.

No need for OrderedDict anymore since dicts always retain order.