From 010b2d6759a8d21de04a86c9babf470c8ed77a6a Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Tue, 7 Feb 2017 17:17:50 -0800 Subject: :shirt: --- README.md | 5 +++-- src/config.rs | 31 ++++++++++++------------------- src/file/yaml.rs | 13 ++++++------- src/value.rs | 10 +++++++--- 4 files changed, 28 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 7a386d7..4dc3d7b 100644 --- a/README.md +++ b/README.md @@ -8,13 +8,14 @@ - Set defaults - Set explicit values (to programmatically override) - - Read from [JSON] and [TOML] files + - Read from [JSON], [TOML], and [YAML] files - Read from environment - Loosely typed — Configuration values may be read in any supported type, as long as there exists a reasonable conversion - - Access nested fields using a formatted path — Uses a subset of JSONPath. Currently supports the child ( `redis.port` ) and subscript operators ( `databases[0].name` ). + - Access nested fields using a formatted path — Uses a subset of JSONPath. Currently supports the child ( `redis.port` ) and subscript operators ( `databases[0].name` ) [JSON]: https://github.com/serde-rs/json [TOML]: https://github.com/toml-lang/toml +[YAML]: https://github.com/chyh1990/yaml-rust ## Install diff --git a/src/config.rs b/src/config.rs index 6c81244..cfe5ea5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -217,17 +217,13 @@ impl Config { // Child ( Child ( Identifier( "x" ), "y" ), "z" ) fn path_get<'a, 'b>(&'a self, expr: path::Expression) -> Option<&'a Value> { match expr { - path::Expression::Identifier(text) => { - self.cache.get(&text) - } + path::Expression::Identifier(text) => self.cache.get(&text), path::Expression::Child(expr, member) => { match self.path_get(*expr) { - Some(&Value::Table(ref table)) => { - table.get(&member) - } + Some(&Value::Table(ref table)) => table.get(&member), - _ => None + _ => None, } } @@ -247,7 +243,7 @@ impl Config { } } - _ => None + _ => None, } } } @@ -448,11 +444,9 @@ mod test { fn test_slice() { let mut c = Config::new(); - c.set("values", vec![ - Value::Integer(10), - Value::Integer(325), - Value::Integer(12), - ]).unwrap(); + c.set("values", + vec![Value::Integer(10), Value::Integer(325), Value::Integer(12)]) + .unwrap(); let values = c.get_slice("values").unwrap(); @@ -464,11 +458,8 @@ mod test { fn test_slice_into() { let mut c = Config::new(); - c.set("values", vec![ - 10, - 325, - 12, - ]).unwrap(); + c.set("values", vec![10, 325, 12]) + .unwrap(); let values = c.get_slice("values").unwrap(); @@ -526,7 +517,9 @@ mod test { [[databases]] name = "test_db" options = { trace = true } - "#, FileFormat::Toml)).unwrap(); + "#, + FileFormat::Toml)) + .unwrap(); assert_eq!(c.get_str("redis.address").unwrap(), "localhost:6379"); assert_eq!(c.get_str("databases[0].name").unwrap(), "test_db"); diff --git a/src/file/yaml.rs b/src/file/yaml.rs index 1c3ccbb..72a987a 100644 --- a/src/file/yaml.rs +++ b/src/file/yaml.rs @@ -7,10 +7,9 @@ use std::collections::{BTreeMap, HashMap}; use std::mem; use value::Value; - pub struct Content { // Root table of the YAML document - root: yaml::Yaml + root: yaml::Yaml, } impl Content { @@ -19,10 +18,8 @@ impl Content { match docs.len() { 0 => Ok(Box::new(Content { root: yaml::Yaml::Hash(BTreeMap::new()) })), - 1 => Ok(Box::new(Content { - root: mem::replace(&mut docs[0], yaml::Yaml::Null) - })), - n => Err(Box::new(MultipleDocumentsError(n))) + 1 => Ok(Box::new(Content { root: mem::replace(&mut docs[0], yaml::Yaml::Null) })), + n => Err(Box::new(MultipleDocumentsError(n))), } } @@ -52,7 +49,9 @@ fn from_yaml_value<'a>(value: &yaml::Yaml) -> Value { Value::Array(l) } // TODO: how should we handle Null and BadValue? - _ => { unimplemented!(); } + _ => { + unimplemented!(); + } } } diff --git a/src/value.rs b/src/value.rs index 1630d85..fa2f791 100644 --- a/src/value.rs +++ b/src/value.rs @@ -84,7 +84,7 @@ impl Value { pub fn as_slice(&self) -> Option<&[Value]> { match *self { Value::Array(ref value) => Some(value), - _ => None + _ => None, } } } @@ -128,7 +128,9 @@ impl From for Value { // } // } -impl From> for Value where T: Into { +impl From> for Value + where T: Into +{ fn from(values: HashMap) -> Value { let mut r = HashMap::new(); @@ -140,7 +142,9 @@ impl From> for Value where T: Into { } } -impl From> for Value where T: Into { +impl From> for Value + where T: Into +{ fn from(values: Vec) -> Value { let mut l = Vec::new(); -- cgit v1.2.3