Skip to content

Commit

Permalink
Always preserve order
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Jan 28, 2017
1 parent bbe0696 commit 27e6bad
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ license = "MIT/Apache-2.0"
description = "The missing YAML 1.2 parser for rust"
repository = "https://github.com/chyh1990/yaml-rust"

[features]
preserve_order = ["linked-hash-map"]

[dependencies]
clippy = { version = "^0.*", optional = true }
linked-hash-map = { version = ">=0.0.9, <0.4", optional = true }
linked-hash-map = ">=0.0.9, <0.4"
1 change: 0 additions & 1 deletion src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ fn need_quotes(string: &str) -> bool {
#[cfg(test)]
mod tests {
use super::*;
use yaml::*;

#[test]
fn test_emit_simple() {
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
#![cfg_attr(feature="clippy", warn(cyclomatic_complexity))]
#![cfg_attr(feature="clippy", allow(match_same_arms))]

#[cfg(feature = "preserve_order")]
extern crate linked_hash_map;

pub mod yaml;
Expand Down
35 changes: 20 additions & 15 deletions src/yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use std::collections::BTreeMap;
use std::ops::Index;
use std::string;
use std::i64;
use std::str::FromStr;
use std::mem;
use std::vec;
use parser::*;
use scanner::{TScalarStyle, ScanError, TokenType, Marker};
use linked_hash_map::LinkedHashMap;

/// A YAML node is stored as this `Yaml` enumeration, which provides an easy way to
/// access your YAML document.
Expand Down Expand Up @@ -37,16 +37,9 @@ pub enum Yaml {
Boolean(bool),
/// YAML array, can be accessed as a `Vec`.
Array(self::Array),
/// YAML hash, can be accessed as a `BTreeMap`.
/// YAML hash, can be accessed as a `LinkedHashMap`.
///
/// If the order of keys is meaningful, enable the `preserve_order` feature to
/// store hashes as a `LinkedHashMap` intead of `BTreeMap`. When using a
/// `LinkedHashMap`, the itertion order will match the order of insertion into
/// the map.
///
/// ```toml
/// yaml-rust = { version = "*", features = ["preserve_order"] }
/// ```
/// Itertion order will match the order of insertion into the map.
Hash(self::Hash),
/// Alias, not fully supported yet.
Alias(usize),
Expand All @@ -59,11 +52,7 @@ pub enum Yaml {
}

pub type Array = Vec<Yaml>;

#[cfg(not(feature = "preserve_order"))]
pub type Hash = BTreeMap<Yaml, Yaml>;
#[cfg(feature = "preserve_order")]
pub type Hash = ::linked_hash_map::LinkedHashMap<Yaml, Yaml>;
pub type Hash = LinkedHashMap<Yaml, Yaml>;

pub struct YamlLoader {
docs: Vec<Yaml>,
Expand Down Expand Up @@ -587,4 +576,20 @@ a1: &DEFAULT
assert_eq!(doc.next().unwrap().into_i64().unwrap(), 63);
assert_eq!(doc.next().unwrap().into_i64().unwrap(), 12345);
}

#[test]
fn test_hash_order() {
let s = "---
b: ~
a: ~
c: ~
";
let out = YamlLoader::load_from_str(&s).unwrap();
let first = out.into_iter().next().unwrap();
let mut iter = first.into_hash().unwrap().into_iter();
assert_eq!(Some((Yaml::String("b".to_owned()), Yaml::Null)), iter.next());
assert_eq!(Some((Yaml::String("a".to_owned()), Yaml::Null)), iter.next());
assert_eq!(Some((Yaml::String("c".to_owned()), Yaml::Null)), iter.next());
assert_eq!(None, iter.next());
}
}

0 comments on commit 27e6bad

Please sign in to comment.