summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRyan Leckey <leckey.ryan@gmail.com>2017-07-30 13:20:36 -0700
committerRyan Leckey <leckey.ryan@gmail.com>2017-07-30 13:20:36 -0700
commit14224be23dc2f253a240b85214927d97e1160669 (patch)
tree6f5b02b26aef5cf37bb14f32b9048165b67109ce /src
parent71f4b182d1e56febda64bd620ae0e0f65de333cd (diff)
Remove ConfigResult; close #36
Diffstat (limited to 'src')
-rw-r--r--src/config.rs268
-rw-r--r--src/de.rs23
-rw-r--r--src/env.rs6
-rw-r--r--src/error.rs65
-rw-r--r--src/file/format/json.rs21
-rw-r--r--src/file/format/mod.rs9
-rw-r--r--src/file/format/toml.rs7
-rw-r--r--src/file/format/yaml.rs5
-rw-r--r--src/file/mod.rs23
-rw-r--r--src/file/source/file.rs71
-rw-r--r--src/file/source/mod.rs7
-rw-r--r--src/file/source/string.rs15
-rw-r--r--src/lib.rs1
-rw-r--r--src/path/mod.rs121
-rw-r--r--src/source.rs7
-rw-r--r--src/value.rs262
16 files changed, 396 insertions, 515 deletions
diff --git a/src/config.rs b/src/config.rs
index 5b669a0..0ff2601 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -51,17 +51,20 @@ impl Config {
}
/// Merge in a configuration property source.
- pub fn merge<T>(&mut self, source: T) -> ConfigResult
- where T: 'static,
- T: Source + Send + Sync
+ pub fn merge<T>(&mut self, source: T) -> Result<&mut Config>
+ where
+ T: 'static,
+ T: Source + Send + Sync,
{
match self.kind {
- ConfigKind::Mutable { ref mut sources, .. } => {
+ ConfigKind::Mutable {
+ ref mut sources, ..
+ } => {
sources.push(Box::new(source));
}
ConfigKind::Frozen => {
- return ConfigResult::Err(ConfigError::Frozen);
+ return Err(ConfigError::Frozen);
}
}
@@ -73,7 +76,7 @@ impl Config {
///
/// Configuration is automatically refreshed after a mutation
/// operation (`set`, `merge`, `set_default`, etc.).
- pub fn refresh(&mut self) -> ConfigResult {
+ pub fn refresh(&mut self) -> Result<&mut Config> {
self.cache = match self.kind {
// TODO: We need to actually merge in all the stuff
ConfigKind::Mutable {
@@ -89,9 +92,7 @@ impl Config {
}
// Add sources
- if let Err(error) = sources.collect_to(&mut cache) {
- return ConfigResult::Err(error);
- }
+ sources.collect_to(&mut cache)?;
// Add overrides
for (key, val) in overrides {
@@ -102,11 +103,11 @@ impl Config {
}
ConfigKind::Frozen => {
- return ConfigResult::Err(ConfigError::Frozen);
+ return Err(ConfigError::Frozen);
}
};
- ConfigResult::Ok(self)
+ Ok(self)
}
/// Deserialize the entire configuration.
@@ -114,41 +115,35 @@ impl Config {
T::deserialize(self.cache.clone())
}
- pub fn set_default<T>(&mut self, key: &str, value: T) -> ConfigResult
- where T: Into<Value>
+ pub fn set_default<T>(&mut self, key: &str, value: T) -> Result<&mut Config>
+ where
+ T: Into<Value>,
{
match self.kind {
- ConfigKind::Mutable { ref mut defaults, .. } => {
- defaults.insert(match key.to_lowercase().parse() {
- Ok(expr) => expr,
- Err(error) => {
- return ConfigResult::Err(error);
- }
- },
- value.into());
+ ConfigKind::Mutable {
+ ref mut defaults, ..
+ } => {
+ defaults.insert(key.to_lowercase().parse()?, value.into());
}
- ConfigKind::Frozen => return ConfigResult::Err(ConfigError::Frozen),
+ ConfigKind::Frozen => return Err(ConfigError::Frozen),
};
self.refresh()
}
- pub fn set<T>(&mut self, key: &str, value: T) -> ConfigResult
- where T: Into<Value>
+ pub fn set<T>(&mut self, key: &str, value: T) -> Result<&mut Config>
+ where
+ T: Into<Value>,
{
match self.kind {
- ConfigKind::Mutable { ref mut overrides, .. } => {
- overrides.insert(match key.to_lowercase().parse() {
- Ok(expr) => expr,
- Err(error) => {
- return ConfigResult::Err(error);
- }
- },
- value.into());
+ ConfigKind::Mutable {
+ ref mut overrides, ..
+ } => {
+ overrides.insert(key.to_lowercase().parse()?, value.into());
}
- ConfigKind::Frozen => return ConfigResult::Err(ConfigError::Frozen),
+ ConfigKind::Frozen => return Err(ConfigError::Frozen),
};
self.refresh()
@@ -195,210 +190,3 @@ impl Config {
self.get(key).and_then(Value::into_array)
}
}
-
-/// Holds the result of configuration alteration functions.
-/// A manual alias of Result to enable a chained API and error forwarding.
-pub enum ConfigResult<'a> {
- Ok(&'a mut Config),
- Err(ConfigError),
-}
-
-#[inline]
-fn unwrap_failed<E: Debug>(msg: &str, error: E) -> ! {
- panic!("{}: {:?}", msg, error)
-}
-
-impl<'a> ConfigResult<'a> {
- /// Forwards `Config::merge`
- pub fn merge<T>(self, source: T) -> ConfigResult<'a>
- where T: 'static,
- T: Source + Send + Sync
- {
- match self {
- // If OK, Proceed to nested method
- ConfigResult::Ok(instance) => instance.merge(source),
-
- // Else, Forward the error
- error => error,
- }
- }
-
- /// Forwards `Config::set_default`
- pub fn set_default<T>(self, key: &str, value: T) -> ConfigResult<'a>
- where T: Into<Value>,
- T: 'static
- {
- match self {
- // If OK, Proceed to nested method
- ConfigResult::Ok(instance) => instance.set_default(key, value),
-
- // Else, Forward the error
- error => error,
- }
- }
-
- /// Forwards `Config::set`
- pub fn set<T>(self, key: &str, value: T) -> ConfigResult<'a>
- where T: Into<Value>,
- T: 'static
- {
- match self {
- // If OK, Proceed to nested method
- ConfigResult::Ok(instance) => instance.set(key, value),
-
- // Else, Forward the error
- error => error,
- }
- }
-
- /// Deserialize the entire configuration.
- pub fn deserialize<'de, T: Deserialize<'de>>(self) -> Result<T> {
- self.and_then(|instance| instance.deserialize())
- }
-
- /// Creates a `Result` out of this `ConfigResult`
- #[inline]
- pub fn to_result(self) -> Result<Config> {
- match self {
- ConfigResult::Ok(value) => Ok(value.clone()),
- ConfigResult::Err(error) => Err(error),
- }
- }
-
- /// Forwards `Result::is_ok`
- #[inline]
- pub fn is_ok(&self) -> bool {
- match *self {
- ConfigResult::Ok(_) => true,
- ConfigResult::Err(_) => false,
- }
- }
-
- /// Forwards `Result::is_err`
- #[inline]
- pub fn is_err(&self) -> bool {
- !self.is_ok()
- }
-
- /// Forwards `Result::ok`
- #[inline]
- pub fn ok(self) -> Option<Config> {
- match self {
- ConfigResult::Ok(x) => Some(x.clone()),
- ConfigResult::Err(_) => None,
- }
- }
-
- /// Forwards `Result::err`
- #[inline]
- pub fn err(self) -> Option<ConfigError> {
- match self {
- ConfigResult::Ok(_) => None,
- ConfigResult::Err(x) => Some(x),
- }
- }
-
- /// Forwards `Result::map`
- #[inline]
- pub fn map<U, F>(self, op: F) -> Result<U>
- where
- F: FnOnce(Config) -> U,
- {
- match self {
- ConfigResult::Ok(x) => Ok(op(x.clone())),
- ConfigResult::Err(error) => Err(error),
- }
- }
-
- /// Forwards `Result::map_err`
- #[inline]
- pub fn map_err<F, O>(self, op: O) -> ::std::result::Result<Config, F>
- where
- O: FnOnce(ConfigError) -> F,
- {
- match self {
- ConfigResult::Err(error) => Err(op(error)),
- ConfigResult::Ok(value) => Ok(value.clone()),
- }
- }
-
- /// Forwards `Result::and`
- #[inline]
- pub fn and<U>(self, res: Result<U>) -> Result<U>
- {
- match self {
- ConfigResult::Ok(_) => res,
- ConfigResult::Err(error) => Err(error),
- }
- }
-
- /// Forwards `Result::and_then`
- #[inline]
- pub fn and_then<U, F>(self, op: F) -> Result<U>
- where
- F: FnOnce(Config) -> Result<U>,
- {
- match self {
- ConfigResult::Ok(value) => op(value.clone()),
- ConfigResult::Err(error) => Err(error),
- }
- }
-
- /// Forwards `Result::or`
- #[inline]
- pub fn or<F>(self, res: ::std::result::Result<Config, F>) -> ::std::result::Result<Config, F>
- {
- match self {
- ConfigResult::Ok(value) => Ok(value.clone()),
- ConfigResult::Err(_) => res,
- }
- }
-
- /// Forwards `Result::or_else`
- #[inline]
- pub fn or_else<F, O>(self, op: O) -> ::std::result::Result<Config, F>
- where
- O: FnOnce(ConfigError) -> ::std::result::Result<Config, F>,
- {
- match self {
- ConfigResult::Ok(value) => Ok(value.clone()),
- ConfigResult::Err(error) => op(error),
- }
- }
-
- /// Forwards `Result::unwrap`
- #[inline]
- pub fn unwrap(self) -> Config {
- match self {
- ConfigResult::Ok(instance) => instance.clone(),
- ConfigResult::Err(error) => unwrap_failed("called `ConfigResult::unwrap()` on an `Err` value", error),
- }
- }
-
- /// Forwards `Result::expect`
- #[inline]
- pub fn expect(self, msg: &str) -> Config {
- match self {
- ConfigResult::Ok(instance) => instance.clone(),
- ConfigResult::Err(error) => unwrap_failed(msg, error),
- }
- }
-
- /// Forwards `Result::unwrap_err`
- #[inline]
- pub fn unwrap_err(self) -> ConfigError {
- match self {
- ConfigResult::Ok(t) => unwrap_failed("called `ConfigResult::unwrap_err()` on an `Ok` value", t),
- ConfigResult::Err(e) => e,
- }
- }
-
- /// Forwards `Result::expect_err`
- #[inline]
- pub fn expect_err(self, msg: &str) -> ConfigError {
- match self {
- ConfigResult::Ok(t) => unwrap_failed(msg, t),
- ConfigResult::Err(e) => e,
- }
- }
-}
diff --git a/src/de.rs b/src/de.rs
index 6487f8b..2ff49e9 100644
--- a/src/de.rs
+++ b/src/de.rs
@@ -1,5 +1,5 @@
use serde::de;
-use value::{Value, ValueWithKey, ValueKind};
+use value::{Value, ValueKind, ValueWithKey};
use error::*;
use std::borrow::Cow;
use std::iter::Peekable;
@@ -13,7 +13,8 @@ impl<'de> de::Deserializer<'de> for ValueWithKey<'de> {
#[inline]
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
- where V: de::Visitor<'de>
+ where
+ V: de::Visitor<'de>,
{
// Deserialize based on the underlying type
match self.0.kind {
@@ -101,7 +102,8 @@ impl<'de> de::Deserializer<'de> for ValueWithKey<'de> {
#[inline]
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
- where V: de::Visitor<'de>
+ where
+ V: de::Visitor<'de>,
{
// Match an explicit nil as None and everything else as Some
match self.0.kind {
@@ -122,7 +124,8 @@ impl<'de> de::Deserializer<'de> for Value {
#[inline]
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
- where V: de::Visitor<'de>
+ where
+ V: de::Visitor<'de>,
{
// Deserialize based on the underlying type
match self.kind {
@@ -210,7 +213,8 @@ impl<'de> de::Deserializer<'de> for Value {
#[inline]
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
- where V: de::Visitor<'de>
+ where
+ V: de::Visitor<'de>,
{
// Match an explicit nil as None and everything else as Some
match self.kind {
@@ -265,7 +269,8 @@ impl<'de> de::SeqAccess<'de> for SeqAccess {
type Error = ConfigError;
fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
- where T: de::DeserializeSeed<'de>
+ where
+ T: de::DeserializeSeed<'de>,
{
match self.elements.next() {
Some(value) => seed.deserialize(value).map(Some),
@@ -299,7 +304,8 @@ impl<'de> de::MapAccess<'de> for MapAccess {
type Error = ConfigError;
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>>
- where K: de::DeserializeSeed<'de>
+ where
+ K: de::DeserializeSeed<'de>,
{
if self.index >= self.elements.len() {
return Ok(None);
@@ -313,7 +319,8 @@ impl<'de> de::MapAccess<'de> for MapAccess {
}
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value>
- where V: de::DeserializeSeed<'de>
+ where
+ V: de::DeserializeSeed<'de>,
{
de::DeserializeSeed::deserialize(seed, self.elements.remove(0).1)
}
diff --git a/src/env.rs b/src/env.rs
index abadea2..aec4de4 100644
--- a/src/env.rs
+++ b/src/env.rs
@@ -88,8 +88,10 @@ impl Source for Environment {
// Replace `separator` with `.`
key = key.replace(&self.separator, ".");
- m.insert(key.to_lowercase(),
- Value::new(Some(&uri), ValueKind::String(value)));
+ m.insert(
+ key.to_lowercase(),
+ Value::new(Some(&uri), ValueKind::String(value)),
+ );
}
Ok(m)
diff --git a/src/error.rs b/src/error.rs
index 514a5e2..eff3c7c 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -13,7 +13,7 @@ pub enum Unexpected {
Str(String),
Unit,
Seq,
- Map
+ Map,
}
impl fmt::Display for Unexpected {
@@ -50,7 +50,7 @@ pub enum ConfigError {
/// The captured error from attempting to parse the file in its desired format.
/// This is the actual error object from the library used for the parsing.
- cause: Box<Error + Send + Sync>
+ cause: Box<Error + Send + Sync>,
},
/// Value could not be converted into the requested type.
@@ -81,27 +81,34 @@ pub enum ConfigError {
impl ConfigError {
// FIXME: pub(crate)
#[doc(hidden)]
- pub fn invalid_type(origin: Option<String>, unexpected: Unexpected, expected: &'static str) -> Self {
+ pub fn invalid_type(
+ origin: Option<String>,
+ unexpected: Unexpected,
+ expected: &'static str,
+ ) -> Self {
ConfigError::Type {
origin: origin,
unexpected: unexpected,
expected: expected,
key: None,
- }
+ }
}
// FIXME: pub(crate)
#[doc(hidden)]
pub fn extend_with_key(self, key: &str) -> Self {
match self {
- ConfigError::Type { origin, unexpected, expected, .. } => {
- ConfigError::Type {
- origin: origin,
- unexpected: unexpected,
- expected: expected,
- key: Some(key.into()),
- }
- }
+ ConfigError::Type {
+ origin,
+ unexpected,
+ expected,
+ ..
+ } => ConfigError::Type {
+ origin: origin,
+ unexpected: unexpected,
+ expected: expected,
+ key: Some(key.into()),
+ },
_ => self,
}
@@ -113,7 +120,7 @@ pub type Result<T> = result::Result<T, ConfigError>;
// Forward Debug to Display for readable panic! messages
impl fmt::Debug for ConfigError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", *self)
}
}
@@ -121,25 +128,23 @@ impl fmt::Debug for ConfigError {
impl fmt::Display for ConfigError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
- ConfigError::Frozen | ConfigError::PathParse(_) => {
- write!(f, "{}", self.description())
- }
+ ConfigError::Frozen | ConfigError::PathParse(_) => write!(f, "{}", self.description()),
- ConfigError::Message(ref s) => {
- write!(f, "{}", s)
- }
+ ConfigError::Message(ref s) => write!(f, "{}", s),
- ConfigError::Foreign(ref cause) => {
- write!(f, "{}", cause)
- }
+ ConfigError::Foreign(ref cause) => write!(f, "{}", cause),
ConfigError::NotFound(ref key) => {
write!(f, "configuration property {:?} not found", key)
}
- ConfigError::Type { ref origin, ref unexpected, expected, ref key } => {
- write!(f, "invalid type: {}, expected {}",
- unexpected, expected)?;
+ ConfigError::Type {
+ ref origin,
+ ref unexpected,
+ expected,
+ ref key,
+ } => {
+ write!(f, "invalid type: {}, expected {}", unexpected, expected)?;
if let Some(ref key) = *key {
write!(f, " for key `{}`", key)?;
@@ -171,7 +176,9 @@ impl Error for ConfigError {
ConfigError::Frozen => "configuration is frozen",
ConfigError::NotFound(_) => "configuration property not found",
ConfigError::Type { .. } => "invalid type",
- ConfigError::Foreign(ref cause) | ConfigError::FileParse { ref cause, .. } => cause.description(),
+ ConfigError::Foreign(ref cause) | ConfigError::FileParse { ref cause, .. } => {
+ cause.description()
+ }
ConfigError::PathParse(ref kind) => kind.description(),
_ => "configuration error",
@@ -180,9 +187,11 @@ impl Error for ConfigError {
fn cause(&self) -> Option<&Error> {
match *self {
- ConfigError::Foreign(ref cause) | ConfigError::FileParse { ref cause, .. } => Some(cause.as_ref()),
+ ConfigError::Foreign(ref cause) | ConfigError::FileParse { ref cause, .. } => {
+ Some(cause.as_ref())
+ }
- _ => None
+ _ => None,
}
}
}
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"),
+ ))
}
}
diff --git a/src/lib.rs b/src/lib.rs
index c1fb07b..bed60b4 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -15,7 +15,6 @@
//!
//! See the [examples](https://github.com/mehcode/config-rs/tree/master/examples