Skip to content

Feature request: preserve order in value #129

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

Closed
matklad opened this issue Aug 14, 2018 · 9 comments · Fixed by #172
Closed

Feature request: preserve order in value #129

matklad opened this issue Aug 14, 2018 · 9 comments · Fixed by #172

Comments

@matklad
Copy link
Contributor

matklad commented Aug 14, 2018

Currently, value is based on BTree map, which loses the original order of ron document. I think it makes sense to add an option to preserve order. I'd even say that if ron's primary purpose is to be a human-editable format, then order should be preserved by default. https://crates.io/crates/indexmap might come in handy do actually implement this.

Here's my specific use-case for ordering: I use ron to generate the AST for Rust. As such, I group related things, like types or expressions, together, and ideally I'd love to see the same order in the generated code. Alas, as I use a map for some part of my input, it doesn't work. I can swtich to a list of pairs for this case, but a mapping makes more sense.

@kvark
Copy link
Collaborator

kvark commented Aug 14, 2018

@matklad this sounds very reasonable

@CinchBlue
Copy link

If support is added for an ordered map parsing mode, how would you like this functionality to be enabled? A macro? An object-format flag? One frustrating thing that could occur now is that the serialized format doesn't show whether it is meant to be parsed in "preserved order mode" or "ordered key mode." This could be problematic if we ever want to provide syntax highlighting support for RON. Should we consider schemas for this?

@kvark
Copy link
Collaborator

kvark commented Oct 1, 2018

@VermillionAzure as I understand this issue, it's not about parsing, it's about serialization only. The parser should work regardless, and no new extensions are needed. I think it can just be an option of the serializer (or a compile feature that enables it and adds dependency on indexmap).

@torkleyy
Copy link
Contributor

@VermillionAzure Are you still interested in fixing this issue?

@lykhouzov
Copy link

Hello, does this will be implemented. I am not sure if i have the same issue, but when I Deserizlise, i do not have the same structure order.
I am using hashmap as

(
tasks: {
    "debug message": Dbg(
      msg: "test message. some text after it."
    ),
    "shell command": Shell(
      command: "ls",
      args: Some([
        "-l",
        "-h",
      ]),
      ch_dir: Some("/")
    ),
}
)

and it does not preserve order of it, so that sometimes shell command goes first.

@kvark
Copy link
Collaborator

kvark commented Feb 13, 2019

@lykhouzov if your tasks is a HashMap, then yes, you are hitting the same issue.
I don't know if you need to have a hashmap there though, might easily do tasks: [...] as a list.

@lykhouzov
Copy link

Hi, @kvark. So yes, it is HashMap.
I already was thinking about use Vec instead, but this construction looks more readable for me.
Porbably i will convert it to something like following if this issue is not gonna be fixed

[
    ("debug message", Dbg(
      msg: "test message. some text after it."
    )),
    ("shell command",Shell(
      command: "ls",
      args: Some([
        "-l",
        "-h",
      ]),
      ch_dir: Some("/")
    ))
]

or maybe move a name of a task to task itself.
Thanks

@torkleyy
Copy link
Contributor

@lykhouzov You could also just replace the HashMap type by IndexMap.

@torkleyy
Copy link
Contributor

Blocked by indexmap-rs/indexmap#91

bors bot added a commit that referenced this issue Jun 10, 2019
172: Use indexmap to preserve order (optional) r=torkleyy a=torkleyy

Introduces an optional `indexmap` feature which uses `IndexMap` in `Value::Map` to preserve the order of deserialized elements.

Fixes #129 

Co-authored-by: Thomas Schaller <[email protected]>
@bors bors bot closed this as completed in #172 Jun 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants