summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyan Leckey <leckey.ryan@gmail.com>2017-02-07 17:17:50 -0800
committerRyan Leckey <leckey.ryan@gmail.com>2017-02-07 17:17:50 -0800
commit010b2d6759a8d21de04a86c9babf470c8ed77a6a (patch)
tree7ac02ba80fb57a5e50ef2ebe77288ee45d6bb3ab
parent788a08756ab88b48c117257685ad7b93b8fab641 (diff)
:shirt:
-rw-r--r--README.md5
-rw-r--r--src/config.rs31
-rw-r--r--src/file/yaml.rs13
-rw-r--r--src/value.rs10
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<bool> for Value {
// }
// }
-impl<T> From<HashMap<String, T>> for Value where T: Into<Value> {
+impl<T> From<HashMap<String, T>> for Value
+ where T: Into<Value>
+{
fn from(values: HashMap<String, T>) -> Value {
let mut r = HashMap::new();
@@ -140,7 +142,9 @@ impl<T> From<HashMap<String, T>> for Value where T: Into<Value> {
}
}
-impl<T> From<Vec<T>> for Value where T: Into<Value> {
+impl<T> From<Vec<T>> for Value
+ where T: Into<Value>
+{
fn from(values: Vec<T>) -> Value {
let mut l = Vec::new();