summaryrefslogtreecommitdiffstats
path: root/src/config.rs
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/config.rs
parent71f4b182d1e56febda64bd620ae0e0f65de333cd (diff)
Remove ConfigResult; close #36
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs268
1 files changed, 28 insertions, 240 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,
- }
- }
-}