Always preserve order

This commit is contained in:
David Tolnay 2017-01-27 20:50:52 -08:00
parent 562eed85a4
commit 316eecbbdc
4 changed files with 21 additions and 21 deletions

View file

@ -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"

View file

@ -278,7 +278,6 @@ fn need_quotes(string: &str) -> bool {
#[cfg(test)]
mod tests {
use super::*;
use yaml::*;
#[test]
fn test_emit_simple() {

View file

@ -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;

View file

@ -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.
@ -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),
@ -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>,
@ -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());
}
}