diff options
author | Ryan Leckey <leckey.ryan@gmail.com> | 2017-07-30 13:20:36 -0700 |
---|---|---|
committer | Ryan Leckey <leckey.ryan@gmail.com> | 2017-07-30 13:20:36 -0700 |
commit | 14224be23dc2f253a240b85214927d97e1160669 (patch) | |
tree | 6f5b02b26aef5cf37bb14f32b9048165b67109ce /src/file | |
parent | 71f4b182d1e56febda64bd620ae0e0f65de333cd (diff) |
Remove ConfigResult; close #36
Diffstat (limited to 'src/file')
-rw-r--r-- | src/file/format/json.rs | 21 | ||||
-rw-r--r-- | src/file/format/mod.rs | 9 | ||||
-rw-r--r-- | src/file/format/toml.rs | 7 | ||||
-rw-r--r-- | src/file/format/yaml.rs | 5 | ||||
-rw-r--r-- | src/file/mod.rs | 23 | ||||
-rw-r--r-- | src/file/source/file.rs | 71 | ||||
-rw-r--r-- | src/file/source/mod.rs | 7 | ||||
-rw-r--r-- | src/file/source/string.rs | 15 |
8 files changed, 91 insertions, 67 deletions
diff --git a/src/file/format/json.rs b/src/file/format/json.rs index ead99f8..caf62f5 100644 --- a/src/file/format/json.rs +++ b/src/file/format/json.rs @@ -4,7 +4,10 @@ use std::collections::HashMap; use std::error::Error; use value::{Value, ValueKind}; -pub fn parse(uri: Option<&String>, text: &str) -> Result<HashMap<String, Value>, Box<Error + Send + Sync>> { +pub fn parse( + uri: Option<&String>, + text: &str, +) -> Result<HashMap<String, Value>, Box<Error + Send + Sync>> { // Parse a JSON object value from the text // TODO: Have a proper error fire if the root of a file is ever not a Table let value = from_json_value(uri, &serde_json::from_str(text)?); @@ -19,15 +22,13 @@ fn from_json_value(uri: Option<&String>, value: &serde_json::Value) -> Value { match *value { serde_json::Value::String(ref value) => Value::new(uri, ValueKind::String(value.clone())), - serde_json::Value::Number(ref value) => { - if let Some(value) = value.as_i64() { - Value::new(uri, ValueKind::Integer(value)) - } else if let Some(value) = value.as_f64() { - Value::new(uri, ValueKind::Float(value)) - } else { - unreachable!(); - } - } + serde_json::Value::Number(ref value) => if let Some(value) = value.as_i64() { + Value::new(uri, ValueKind::Integer(value)) + } else if let Some(value) = value.as_f64() { + Value::new(uri, ValueKind::Float(value)) + } else { + unreachable!(); + }, serde_json::Value::Bool(value) => Value::new(uri, ValueKind::Boolean(value)), diff --git a/src/file/format/mod.rs b/src/file/format/mod.rs index 5aa1acb..a90dfda 100644 --- a/src/file/format/mod.rs +++ b/src/file/format/mod.rs @@ -63,10 +63,11 @@ impl FileFormat { // TODO: pub(crate) #[doc(hidden)] #[allow(unused_variables)] - pub fn parse(&self, - uri: Option<&String>, - text: &str) - -> Result<HashMap<String, Value>, Box<Error + Send + Sync>> { + pub fn parse( + &self, + uri: Option<&String>, + text: &str, + ) -> Result<HashMap<String, Value>, Box<Error + Send + Sync>> { match *self { #[cfg(feature = "toml")] FileFormat::Toml => toml::parse(uri, text), diff --git a/src/file/format/toml.rs b/src/file/format/toml.rs index 4307e48..9977126 100644 --- a/src/file/format/toml.rs +++ b/src/file/format/toml.rs @@ -1,10 +1,13 @@ use toml; use source::Source; -use std::collections::{HashMap, BTreeMap}; +use std::collections::{BTreeMap, HashMap}; use std::error::Error; use value::{Value, ValueKind}; -pub fn parse(uri: Option<&String>, text: &str) -> Result<HashMap<String, Value>, Box<Error + Send + Sync>> { +pub fn parse( + uri: Option<&String>, + text: &str, +) -> Result<HashMap<String, Value>, Box<Error + Send + Sync>> { // Parse a TOML value from the provided text // TODO: Have a proper error fire if the root of a file is ever not a Table let value = from_toml_value(uri, &toml::from_str(text)?); diff --git a/src/file/format/yaml.rs b/src/file/format/yaml.rs index 9bcf9d4..87240ac 100644 --- a/src/file/format/yaml.rs +++ b/src/file/format/yaml.rs @@ -6,7 +6,10 @@ use std::collections::HashMap; use std::mem; use value::{Value, ValueKind}; -pub fn parse(uri: Option<&String>, text: &str) -> Result<HashMap<String, Value>, Box<Error + Send + Sync>> { +pub fn parse( + uri: Option<&String>, + text: &str, +) -> Result<HashMap<String, Value>, Box<Error + Send + Sync>> { // Parse a YAML object from file let mut docs = yaml::YamlLoader::load_from_str(text)?; let root = match docs.len() { diff --git a/src/file/mod.rs b/src/file/mod.rs index 45deb92..6586ede 100644 --- a/src/file/mod.rs +++ b/src/file/mod.rs @@ -12,7 +12,8 @@ pub use self::format::FileFormat; #[derive(Clone, Debug)] pub struct File<T> - where T: FileSource +where + T: FileSource, { source: T, @@ -86,8 +87,9 @@ impl<T: FileSource> File<T> { } impl<T: FileSource> Source for File<T> - where T: 'static, - T: Sync + Send +where + T: 'static, + T: Sync + Send, { fn clone_into_box(&self) -> Box<Source + Send + Sync> { Box::new((*self).clone()) @@ -96,8 +98,9 @@ impl<T: FileSource> Source for File<T> fn collect(&self) -> Result<HashMap<String, Value>> { // Coerce the file contents to a string let (uri, contents, format) = match self.source - .resolve(self.format) - .map_err(|err| ConfigError::Foreign(err)) { + .resolve(self.format) + .map_err(|err| ConfigError::Foreign(err)) + { Ok((uri, contents, format)) => (uri, contents, format), Err(error) => { @@ -111,10 +114,10 @@ impl<T: FileSource> Source for File<T> // Parse the string using the given format format.parse(uri.as_ref(), &contents).map_err(|cause| { - ConfigError::FileParse { - uri: uri, - cause: cause, - } - }) + ConfigError::FileParse { + uri: uri, + cause: cause, + } + }) } } diff --git a/src/file/source/file.rs b/src/file/source/file.rs index 7bf29be..80cd1dd 100644 --- a/src/file/source/file.rs +++ b/src/file/source/file.rs @@ -2,7 +2,7 @@ use std::str::FromStr; use std::result; use std::error::Error; -use std::path::{PathBuf, Path}; +use std::path::{Path, PathBuf}; use file::format::ALL_EXTENSIONS; use std::io::{self, Read}; use std::fs; @@ -24,9 +24,10 @@ impl FileSourceFile { FileSourceFile { name: name } } - fn find_file(&self, - format_hint: Option<FileFormat>) - -> Result<(PathBuf, FileFormat), Box<Error + Send + Sync>> { + fn find_file( + &self, + format_hint: Option<FileFormat>, + ) -> Result<(PathBuf, FileFormat), Box<Error + Send + Sync>> { // First check for an _exact_ match let mut filename = env::current_dir()?.as_path().join(self.name.clone()); if filename.is_file() { @@ -35,55 +36,61 @@ impl FileSourceFile { None => { for (format, extensions) in ALL_EXTENSIONS.iter() { if extensions.contains(&filename - .extension() - .unwrap_or_default() - .to_string_lossy() - .as_ref()) { + .extension() + .unwrap_or_default() + .to_string_lossy() + .as_ref()) + { return Ok((filename, *format)); } } - Err(Box::new(io::Error::new(io::ErrorKind::NotFound, - format!("configuration file \"{}\" is not of a registered file format", - filename.to_string_lossy())))) + Err(Box::new(io::Error::new( + io::ErrorKind::NotFound, + format!( + "configuration file \"{}\" is not of a registered file format", + filename.to_string_lossy() + ), + ))) } }; } match format_hint { - Some(format) => { - for ext in format.extensions() { - filename.set_extension(ext); + Some(format) => for ext in format.extensions() { + filename.set_extension(ext); - if filename.is_file() { - return Ok((filename, format)); - } + if filename.is_file() { + return Ok((filename, format)); } - } + }, - None => { - for (format, extensions) in ALL_EXTENSIONS.iter() { - for ext in format.extensions() { - filename.set_extension(ext); + None => for (format, extensions) in ALL_EXTENSIONS.iter() { + for ext in format.extensions() { + filename.set_extension(ext); - if filename.is_file() { - return Ok((filename, *format)); - } + if filename.is_file() { + return Ok((filename, *format)); } } - } + }, } - Err(Box::new(io::Error::new(io::ErrorKind::NotFound, - format!("configuration file \"{}\" not found", - self.name.to_string_lossy())))) + Err(Box::new(io::Error::new( + io::ErrorKind::NotFound, + format!( + "configuration file \"{}\" not found", + self.name.to_string_lossy() + ), + ))) } } impl FileSource for FileSourceFile { - fn resolve(&self, - format_hint: Option<FileFormat>) - -> Result<(Option<String>, String, FileFormat), Box<Error + Send + Sync>> { + fn resolve( + &self, + format_hint: Option<FileFormat>, + ) -> Result<(Option<String>, String, FileFormat), Box<Error + Send + Sync>> { // Find file let (filename, format) = self.find_file(format_hint)?; diff --git a/src/file/source/mod.rs b/src/file/source/mod.rs index f164d27..4d9950f 100644 --- a/src/file/source/mod.rs +++ b/src/file/source/mod.rs @@ -9,7 +9,8 @@ use super::FileFormat; /// Describes where the file is sourced pub trait FileSource: Debug + Clone { - fn resolve(&self, - format_hint: Option<FileFormat>) - -> Result<(Option<String>, String, FileFormat), Box<Error + Send + Sync>>; + fn resolve( + &self, + format_hint: Option<FileFormat>, + ) -> Result<(Option<String>, String, FileFormat), Box<Error + Send + Sync>>; } diff --git a/src/file/source/string.rs b/src/file/source/string.rs index 5dbd1b9..9c89231 100644 --- a/src/file/source/string.rs +++ b/src/file/source/string.rs @@ -3,7 +3,7 @@ use std::result; use std::error::Error; use source::Source; -use super::{FileSource, FileFormat}; +use super::{FileFormat, FileSource}; /// Describes a file sourced from a string #[derive(Clone, Debug)] @@ -16,9 +16,14 @@ impl<'a> From<&'a str> for FileSourceString { } impl FileSource for FileSourceString { - fn resolve(&self, - format_hint: Option<FileFormat>) - -> Result<(Option<String>, String, FileFormat), Box<Error + Send + Sync>> { - Ok((None, self.0.clone(), format_hint.expect("from_str requires a set file format"))) + fn resolve( + &self, + format_hint: Option<FileFormat>, + ) -> Result<(Option<String>, String, FileFormat), Box<Error + Send + Sync>> { + Ok(( + None, + self.0.clone(), + format_hint.expect("from_str requires a set file format"), + )) } } |