Skip to content
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

Preserve order of items in maps #234

Merged
merged 4 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 84 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,18 @@ serde-hjson = "0.9.1"
serde-protobuf = "0.8.1"
serde_cbor = "0.11.2"
serde_json = "1.0.81"
serde_yaml = "0.8.24"
serde_yaml = "0.9.16"
structopt = "0.3.26"
toml = "0.5.9"
yaml-rust = "0.4.5"

[dependencies.avro-rs]
version = "0.6.5"
features = ["snappy"]

[dependencies.toml]
version = "0.6.0"
features = ["preserve_order"]

[profile.release]
lto = true
codegen-units = 1
24 changes: 16 additions & 8 deletions src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::error;
use ordered_float;
use serde;
use serde_json;
use std::collections;
use std::fmt;
use std::io;

Expand All @@ -17,7 +16,7 @@ pub mod raw;
pub mod toml;
pub mod yaml;

#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
#[derive(Debug)]
pub enum Value {
Unit,
Bool(bool),
Expand All @@ -40,8 +39,10 @@ pub enum Value {
Bytes(Vec<u8>),

Sequence(Vec<Value>),
// TODO: Use a container that preserves insertion order
Map(collections::BTreeMap<Value, Value>),

// A sequence of pairs is appropriate for representing an object/a map:
// there is no need to deduplicate keys, and it is nice to preserve order.
Map(Vec<(Value, Value)>),
}

pub trait Source {
Expand Down Expand Up @@ -159,7 +160,14 @@ impl serde::ser::Serialize for Value {
Self::Bytes(ref v) => v.serialize(s),

Self::Sequence(ref v) => v.serialize(s),
Self::Map(ref v) => v.serialize(s),
Self::Map(ref v) => {
use serde::ser::SerializeMap;
let mut s = s.serialize_map(Some(v.len()))?;
for (key, value) in v {
SerializeMap::serialize_entry(&mut s, key, value)?;
}
SerializeMap::end(s)
}
}
}
}
Expand Down Expand Up @@ -353,10 +361,10 @@ impl<'de> serde::de::Visitor<'de> for ValueVisitor {
where
V: serde::de::MapAccess<'de>,
{
let mut values = collections::BTreeMap::new();
let mut values = Vec::new();

while let Some((key, value)) = v.next_entry()? {
values.insert(key, value);
while let Some(entry) = v.next_entry()? {
values.push(entry);
}

Ok(Value::Map(values))
Expand Down
8 changes: 4 additions & 4 deletions src/value/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ impl value::Source for Source {
fn read(&mut self) -> error::Result<Option<value::Value>> {
match self.0.take() {
Some(v) => {
let mut de = toml::de::Deserializer::new(v.as_str());
match serde::Deserialize::deserialize(&mut de) {
let de = toml::de::Deserializer::new(v.as_str());
match serde::Deserialize::deserialize(de) {
Ok(v) => Ok(Some(v)),
Err(e) => Err(error::Error::from(e)),
}
Expand All @@ -54,8 +54,8 @@ where
fn write(&mut self, value: value::Value) -> error::Result<()> {
let mut string = String::new();
{
let mut ser = toml::ser::Serializer::new(&mut string);
serde::Serialize::serialize(&value, &mut ser)?;
let ser = toml::ser::Serializer::new(&mut string);
serde::Serialize::serialize(&value, ser)?;
}

self.0.write_all(string.as_bytes())?;
Expand Down