From 14224be23dc2f253a240b85214927d97e1160669 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Sun, 30 Jul 2017 13:20:36 -0700 Subject: Remove ConfigResult; close #36 --- src/config.rs | 268 ++++++---------------------------------------------------- 1 file changed, 28 insertions(+), 240 deletions(-) (limited to 'src/config.rs') 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(&mut self, source: T) -> ConfigResult - where T: 'static, - T: Source + Send + Sync + pub fn merge(&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(&mut self, key: &str, value: T) -> ConfigResult - where T: Into + pub fn set_default(&mut self, key: &str, value: T) -> Result<&mut Config> + where + T: Into, { 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(&mut self, key: &str, value: T) -> ConfigResult - where T: Into + pub fn set(&mut self, key: &str, value: T) -> Result<&mut Config> + where + T: Into, { 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(msg: &str, error: E) -> ! { - panic!("{}: {:?}", msg, error) -} - -impl<'a> ConfigResult<'a> { - /// Forwards `Config::merge` - pub fn merge(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(self, key: &str, value: T) -> ConfigResult<'a> - where T: Into, - 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(self, key: &str, value: T) -> ConfigResult<'a> - where T: Into, - 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 { - self.and_then(|instance| instance.deserialize()) - } - - /// Creates a `Result` out of this `ConfigResult` - #[inline] - pub fn to_result(self) -> Result { - 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 { - match self { - ConfigResult::Ok(x) => Some(x.clone()), - ConfigResult::Err(_) => None, - } - } - - /// Forwards `Result::err` - #[inline] - pub fn err(self) -> Option { - match self { - ConfigResult::Ok(_) => None, - ConfigResult::Err(x) => Some(x), - } - } - - /// Forwards `Result::map` - #[inline] - pub fn map(self, op: F) -> Result - 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(self, op: O) -> ::std::result::Result - 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(self, res: Result) -> Result - { - match self { - ConfigResult::Ok(_) => res, - ConfigResult::Err(error) => Err(error), - } - } - - /// Forwards `Result::and_then` - #[inline] - pub fn and_then(self, op: F) -> Result - where - F: FnOnce(Config) -> Result, - { - match self { - ConfigResult::Ok(value) => op(value.clone()), - ConfigResult::Err(error) => Err(error), - } - } - - /// Forwards `Result::or` - #[inline] - pub fn or(self, res: ::std::result::Result) -> ::std::result::Result - { - match self { - ConfigResult::Ok(value) => Ok(value.clone()), - ConfigResult::Err(_) => res, - } - } - - /// Forwards `Result::or_else` - #[inline] - pub fn or_else(self, op: O) -> ::std::result::Result - where - O: FnOnce(ConfigError) -> ::std::result::Result, - { - 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, - } - } -} -- cgit v1.2.3