commit
8755753ed7
4 changed files with 21 additions and 20 deletions
|
@ -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"
|
||||
|
|
|
@ -278,7 +278,6 @@ fn need_quotes(string: &str) -> bool {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use yaml::*;
|
||||
|
||||
#[test]
|
||||
fn test_emit_simple() {
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -6,6 +6,7 @@ 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.
|
||||
|
@ -36,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),
|
||||
|
@ -58,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>,
|
||||
|
@ -586,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());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue