From fb30d87cf096d0816abc3c40c1ff3f1b8440ee74 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Sat, 3 Jun 2017 01:22:04 -0700 Subject: Sources collect to HashMap instead of Value --- src/file/format/mod.rs | 3 ++- src/file/format/toml.rs | 13 +++++++++---- src/file/mod.rs | 3 ++- 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> { + pub fn parse(&self, uri: Option<&String>, text: &str, namespace: Option<&String>) -> Result, Box> { 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> { +pub fn parse(uri: Option<&String>, text: &str, namespace: Option<&String>) -> Result, Box> { // 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 File { } impl Source for File { - fn collect(&self) -> Result { + fn collect(&self) -> Result> { // 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; + /// a HashMap. + fn collect(&self) -> Result>; } -- cgit v1.2.3