diff options
author | Ryan Leckey <ryan@launchbadge.com> | 2017-06-03 01:22:04 -0700 |
---|---|---|
committer | Ryan Leckey <ryan@launchbadge.com> | 2017-06-03 01:22:04 -0700 |
commit | fb30d87cf096d0816abc3c40c1ff3f1b8440ee74 (patch) | |
tree | 8b59b9ac3a488ae3c246e7313f995c8033738571 | |
parent | a12d8e5992289bbb9c50bc2d734132e1cfc2798b (diff) |
Sources collect to HashMap instead of Value
-rw-r--r-- | src/file/format/mod.rs | 3 | ||||
-rw-r--r-- | src/file/format/toml.rs | 13 | ||||
-rw-r--r-- | src/file/mod.rs | 3 | ||||
-rw-r--r-- | src/source.rs | 5 |
4 files changed, 16 insertions, 8 deletions
diff --git a/src/file/format/mod.rs b/src/file/format/mod.rs index 5c97a7f..5c36815 100644 --- a/src/file/format/mod.rs +++ b/src/file/format/mod.rs @@ -1,6 +1,7 @@ use source::Source; use value::Value; use std::error::Error; +use std::collections::HashMap; #[cfg(feature = "toml")] mod toml; @@ -45,7 +46,7 @@ impl FileFormat { // TODO: pub(crate) #[doc(hidden)] #[allow(unused_variables)] - pub fn parse(&self, uri: Option<&String>, text: &str, namespace: Option<&String>) -> Result<Value, Box<Error>> { + pub fn parse(&self, uri: Option<&String>, text: &str, namespace: Option<&String>) -> Result<HashMap<String, Value>, Box<Error>> { match *self { #[cfg(feature = "toml")] FileFormat::Toml => toml::parse(uri, text, namespace), diff --git a/src/file/format/toml.rs b/src/file/format/toml.rs index bbe6aa6..f4a4196 100644 --- a/src/file/format/toml.rs +++ b/src/file/format/toml.rs @@ -2,9 +2,9 @@ use toml; use source::Source; use std::collections::{HashMap, BTreeMap}; use std::error::Error; -use value::Value; +use value::{Value, ValueKind}; -pub fn parse(uri: Option<&String>, text: &str, namespace: Option<&String>) -> Result<Value, Box<Error>> { +pub fn parse(uri: Option<&String>, text: &str, namespace: Option<&String>) -> Result<HashMap<String, Value>, Box<Error>> { // Parse a TOML value from the provided text let mut root: toml::Value = toml::from_str(text)?; @@ -25,10 +25,15 @@ pub fn parse(uri: Option<&String>, text: &str, namespace: Option<&String>) -> Re }); } - Ok(from_toml_value(uri, &root)) + // TODO: Have a proper error fire if the root of a file is ever not a Table + let value = from_toml_value(uri, &root); + match value.kind { + ValueKind::Table(map) => Ok(map), + + _ => Ok(HashMap::new()), + } } -// TODO: Extend value origin with line/column numbers when able fn from_toml_value(uri: Option<&String>, value: &toml::Value) -> Value { match *value { toml::Value::String(ref value) => Value::new(uri, value.to_string()), diff --git a/src/file/mod.rs b/src/file/mod.rs index 7534ddb..dbb80b6 100644 --- a/src/file/mod.rs +++ b/src/file/mod.rs @@ -4,6 +4,7 @@ pub mod source; use source::Source; use error::*; use value::Value; +use std::collections::HashMap; use self::source::FileSource; pub use self::format::FileFormat; @@ -58,7 +59,7 @@ impl<T: FileSource> File<T> { } impl<T: FileSource> Source for File<T> { - fn collect(&self) -> Result<Value> { + fn collect(&self) -> Result<HashMap<String, Value>> { // Coerce the file contents to a string let (uri, contents) = self.source.resolve(self.format).map_err(|err| { ConfigError::Foreign(err) diff --git a/src/source.rs b/src/source.rs index 7519438..5db362e 100644 --- a/src/source.rs +++ b/src/source.rs @@ -1,9 +1,10 @@ use error::*; use value::Value; +use std::collections::HashMap; /// Describes a generic _source_ of configuration properties. pub trait Source { /// Collect all configuration properties available from this source and return - /// a top-level Value (which we expected to be a Table). - fn collect(&self) -> Result<Value>; + /// a HashMap. + fn collect(&self) -> Result<HashMap<String, Value>>; } |