toml-rs / toml

Rust TOML Parser
https://docs.rs/toml
Apache License 2.0
730 stars 107 forks source link

Dotted key ordering isn't always preserved #163

Open epage opened 3 years ago

epage commented 3 years ago
hello.world = "a"
goodbye = "b"
hello.moon = "c"

gets turned into

hello.world = "a"
hello.moon = "c"
goodbye = "b"
epage commented 3 years ago

If/when we add position information, a next step is to see if we can infer an inserted values position based on the parent dotted table. This would have us automatically organize new keys as is encouraged in the TOML spec

ofek commented 1 year ago

Has there been any progress on this?

epage commented 1 year ago

Generally, if there were, they'd already be posted here or in a linked issue

Overall, the use of dotted keys seems small and alt ordering being even smaller as its non-idiomatic, that this hasn't been a priority for me. If someone else wants to pick up the work, they are welcome to.

ofek commented 1 year ago

Thanks, I was just curious as I'm thinking about making Python bindings.

fenollp commented 8 months ago

Hi! Drive by question: What/where is the struct or tree holding the collection of values dotted and otherwise and how can we make that type and its underlying values types "sort" in insertion order?

epage commented 8 months ago

As this is isn't a focus area / priority of mine, it will be up to you to do some leg work on investigating this.

crazymerlyn commented 3 months ago

Looked into it and the cause seems to be the recursive way the the structure is stored. KeyValuePairs is an IndexMap so it should already maintain the order. But for dotted keys, hello.moon's top level key is just hello, the same as hello.world. As a result, when traversing, hello.world and hello.moon are bunched together before goodbye.

mxndtaylor commented 1 week ago

I've just raised a PR to fix this - #808 I won't pretend its the most elegant solution, but it does seem to work.

Feedback welcome, I don't mind taking a new approach; I'm honestly not the most experienced with rust.