Skip to content

Commit

Permalink
Merge pull request #16 from dtolnay/order
Browse files Browse the repository at this point in the history
Add preserve_order feature to use LinkedHashMap instead of BTreeMap
  • Loading branch information
chenyh1990 committed Mar 21, 2016
2 parents 26d770d + a437d0b commit 64e6084
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ 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", optional = true }
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
#![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;
pub mod scanner;
pub mod parser;
Expand Down
15 changes: 14 additions & 1 deletion src/yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use scanner::{TScalarStyle, ScanError, TokenType};
/// assert!(v.as_i64().is_some());
/// }
/// ```
#[derive(Clone, PartialEq, PartialOrd, Debug, Eq, Ord)]
#[derive(Clone, PartialEq, PartialOrd, Debug, Eq, Ord, Hash)]
pub enum Yaml {
/// Float types are stored as String and parsed on demand.
/// Note that f64 does NOT implement Eq trait and can NOT be stored in BTreeMap.
Expand All @@ -37,6 +37,15 @@ pub enum Yaml {
/// YAML array, can be accessed as a `Vec`.
Array(self::Array),
/// YAML hash, can be accessed as a `BTreeMap`.
///
/// 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"] }
/// ```
Hash(self::Hash),
/// Alias, not fully supported yet.
Alias(usize),
Expand All @@ -49,7 +58,11 @@ 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 struct YamlLoader {
docs: Vec<Yaml>,
Expand Down

0 comments on commit 64e6084

Please sign in to comment.