From 57fb2610ad274dff1d2f2e4d43dfce05ca9c4835 Mon Sep 17 00:00:00 2001 From: Federico Pasqua Date: Sat, 14 Mar 2020 15:46:36 +0100 Subject: General upgrade for clippy fix and remove of deprecated methods for errors --- src/config.rs | 4 ++-- src/de.rs | 10 +++++----- src/env.rs | 2 +- src/error.rs | 46 ++++++++++++---------------------------------- src/file/format/hjson.rs | 2 +- src/file/format/ini.rs | 2 +- src/file/format/json.rs | 2 +- src/file/format/mod.rs | 10 +++++----- src/file/format/toml.rs | 2 +- src/file/format/yaml.rs | 2 +- src/file/mod.rs | 6 +++--- src/file/source/file.rs | 8 ++++---- src/file/source/mod.rs | 2 +- src/file/source/string.rs | 2 +- src/path/mod.rs | 23 +++++++++-------------- src/ser.rs | 2 +- src/source.rs | 12 ++++++------ src/value.rs | 24 ++++++++++++------------ 18 files changed, 67 insertions(+), 94 deletions(-) diff --git a/src/config.rs b/src/config.rs index e54daa2..c093dae 100644 --- a/src/config.rs +++ b/src/config.rs @@ -18,7 +18,7 @@ enum ConfigKind { Mutable { defaults: HashMap, overrides: HashMap, - sources: Vec>, + sources: Vec>, }, // A frozen configuration. @@ -212,7 +212,7 @@ impl Config { } impl Source for Config { - fn clone_into_box(&self) -> Box { + fn clone_into_box(&self) -> Box { Box::new((*self).clone()) } diff --git a/src/de.rs b/src/de.rs index 3c9e210..2776634 100644 --- a/src/de.rs +++ b/src/de.rs @@ -125,7 +125,7 @@ impl<'de> de::Deserializer<'de> for Value { where V: de::Visitor<'de>, { - visitor.visit_enum(EnumAccess{ value: self, name: name, variants: variants }) + visitor.visit_enum(EnumAccess{ value: self, name, variants }) } forward_to_deserialize_any! { @@ -241,12 +241,12 @@ struct EnumAccess { } impl EnumAccess { - fn variant_deserializer(&self, name: &String) -> Result { + fn variant_deserializer(&self, name: &str) -> Result { self.variants .iter() - .find(|&s| s == name) + .find(|&&s| s == name) .map(|&s| StrDeserializer(s)) - .ok_or(self.no_constructor_error(name)) + .ok_or_else(|| self.no_constructor_error(name)) } fn table_deserializer(&self, table: &Table) -> Result { @@ -448,7 +448,7 @@ impl<'de> de::Deserializer<'de> for Config { where V: de::Visitor<'de>, { - visitor.visit_enum(EnumAccess{ value: self.cache, name: name, variants: variants }) + visitor.visit_enum(EnumAccess{ value: self.cache, name, variants }) } forward_to_deserialize_any! { diff --git a/src/env.rs b/src/env.rs index ce67069..0617383 100644 --- a/src/env.rs +++ b/src/env.rs @@ -63,7 +63,7 @@ impl Default for Environment { } impl Source for Environment { - fn clone_into_box(&self) -> Box { + fn clone_into_box(&self) -> Box { Box::new((*self).clone()) } diff --git a/src/error.rs b/src/error.rs index d7beeaf..b6730e3 100644 --- a/src/error.rs +++ b/src/error.rs @@ -51,7 +51,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, + cause: Box, }, /// Value could not be converted into the requested type. @@ -76,7 +76,7 @@ pub enum ConfigError { Message(String), /// Unadorned error from a foreign origin. - Foreign(Box), + Foreign(Box), } impl ConfigError { @@ -88,9 +88,9 @@ impl ConfigError { expected: &'static str, ) -> Self { ConfigError::Type { - origin: origin, - unexpected: unexpected, - expected: expected, + origin, + unexpected, + expected, key: None, } } @@ -105,9 +105,9 @@ impl ConfigError { expected, .. } => ConfigError::Type { - origin: origin, - unexpected: unexpected, - expected: expected, + origin, + unexpected, + expected, key: Some(key.into()), }, @@ -166,7 +166,9 @@ 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 => write!(f, "configuration is frozen"), + + ConfigError::PathParse(ref kind) => write!(f, "{}", kind.description()), ConfigError::Message(ref s) => write!(f, "{}", s), @@ -208,31 +210,7 @@ impl fmt::Display for ConfigError { } } -impl Error for ConfigError { - fn description(&self) -> &str { - match *self { - 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::PathParse(ref kind) => kind.description(), - - _ => "configuration error", - } - } - - fn cause(&self) -> Option<&Error> { - match *self { - ConfigError::Foreign(ref cause) | ConfigError::FileParse { ref cause, .. } => { - Some(cause.as_ref()) - } - - _ => None, - } - } -} +impl Error for ConfigError { } impl de::Error for ConfigError { fn custom(msg: T) -> Self { diff --git a/src/file/format/hjson.rs b/src/file/format/hjson.rs index cb0a064..457cfbf 100644 --- a/src/file/format/hjson.rs +++ b/src/file/format/hjson.rs @@ -7,7 +7,7 @@ use value::{Value, ValueKind}; pub fn parse( uri: Option<&String>, text: &str, -) -> Result, Box> { +) -> Result, Box> { // 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_hjson_value(uri, &serde_hjson::from_str(text)?); diff --git a/src/file/format/ini.rs b/src/file/format/ini.rs index 845de3a..e5e5950 100644 --- a/src/file/format/ini.rs +++ b/src/file/format/ini.rs @@ -7,7 +7,7 @@ use value::{Value, ValueKind}; pub fn parse( uri: Option<&String>, text: &str, -) -> Result, Box> { +) -> Result, Box> { let mut map: HashMap = HashMap::new(); let i = Ini::load_from_str(text)?; for (sec, prop) in i.iter() { diff --git a/src/file/format/json.rs b/src/file/format/json.rs index 87240a3..0d15070 100644 --- a/src/file/format/json.rs +++ b/src/file/format/json.rs @@ -7,7 +7,7 @@ use value::{Value, ValueKind}; pub fn parse( uri: Option<&String>, text: &str, -) -> Result, Box> { +) -> Result, Box> { // 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)?); diff --git a/src/file/format/mod.rs b/src/file/format/mod.rs index 39b3813..f46ae13 100644 --- a/src/file/format/mod.rs +++ b/src/file/format/mod.rs @@ -72,22 +72,22 @@ lazy_static! { impl FileFormat { // TODO: pub(crate) #[doc(hidden)] - pub fn extensions(&self) -> &'static Vec<&'static str> { + pub fn extensions(self) -> &'static Vec<&'static str> { // It should not be possible for this to fail // A FileFormat would need to be declared without being added to the // ALL_EXTENSIONS map. - ALL_EXTENSIONS.get(self).unwrap() + ALL_EXTENSIONS.get(&self).unwrap() } // TODO: pub(crate) #[doc(hidden)] #[allow(unused_variables)] pub fn parse( - &self, + self, uri: Option<&String>, text: &str, - ) -> Result, Box> { - match *self { + ) -> Result, Box> { + 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 26dcb2a..a40104e 100644 --- a/src/file/format/toml.rs +++ b/src/file/format/toml.rs @@ -7,7 +7,7 @@ use value::{Value, ValueKind}; pub fn parse( uri: Option<&String>, text: &str, -) -> Result, Box> { +) -> Result, Box> { // 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 c2b26cb..c458c3c 100644 --- a/src/file/format/yaml.rs +++ b/src/file/format/yaml.rs @@ -9,7 +9,7 @@ use yaml_rust as yaml; pub fn parse( uri: Option<&String>, text: &str, -) -> Result, Box> { +) -> Result, Box> { // 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 342d09b..ebe93bc 100644 --- a/src/file/mod.rs +++ b/src/file/mod.rs @@ -94,7 +94,7 @@ where T: 'static, T: Sync + Send, { - fn clone_into_box(&self) -> Box { + fn clone_into_box(&self) -> Box { Box::new((*self).clone()) } @@ -120,8 +120,8 @@ where format .parse(uri.as_ref(), &contents) .map_err(|cause| ConfigError::FileParse { - uri: uri, - cause: cause, + uri, + cause, }) } } diff --git a/src/file/source/file.rs b/src/file/source/file.rs index a413a1f..57c251f 100644 --- a/src/file/source/file.rs +++ b/src/file/source/file.rs @@ -21,13 +21,13 @@ pub struct FileSourceFile { impl FileSourceFile { pub fn new(name: PathBuf) -> FileSourceFile { - FileSourceFile { name: name } + FileSourceFile { name } } fn find_file( &self, format_hint: Option, - ) -> Result<(PathBuf, FileFormat), Box> { + ) -> Result<(PathBuf, FileFormat), Box> { // First check for an _exact_ match let mut filename = env::current_dir()?.as_path().join(self.name.clone()); if filename.is_file() { @@ -91,7 +91,7 @@ impl FileSource for FileSourceFile { fn resolve( &self, format_hint: Option, - ) -> Result<(Option, String, FileFormat), Box> { + ) -> Result<(Option, String, FileFormat), Box> { // Find file let (filename, format) = self.find_file(format_hint)?; @@ -103,7 +103,7 @@ impl FileSource for FileSourceFile { }; // Read contents from file - let mut file = fs::File::open(filename.clone())?; + let mut file = fs::File::open(filename)?; let mut text = String::new(); file.read_to_string(&mut text)?; diff --git a/src/file/source/mod.rs b/src/file/source/mod.rs index 5da69f1..ab276d0 100644 --- a/src/file/source/mod.rs +++ b/src/file/source/mod.rs @@ -12,5 +12,5 @@ pub trait FileSource: Debug + Clone { fn resolve( &self, format_hint: Option, - ) -> Result<(Option, String, FileFormat), Box>; + ) -> Result<(Option, String, FileFormat), Box>; } diff --git a/src/file/source/string.rs b/src/file/source/string.rs index a2f66cb..3896cce 100644 --- a/src/file/source/string.rs +++ b/src/file/source/string.rs @@ -19,7 +19,7 @@ impl FileSource for FileSourceString { fn resolve( &self, format_hint: Option, - ) -> Result<(Option, String, FileFormat), Box> { + ) -> Result<(Option, String, FileFormat), Box> { Ok(( None, self.0.clone(), diff --git a/src/path/mod.rs b/src/path/mod.rs index f63deee..3916010 100644 --- a/src/path/mod.rs +++ b/src/path/mod.rs @@ -152,7 +152,6 @@ impl Expression { }, Expression::Subscript(ref expr, index) => { - let mut do_again = false; match expr.get_mut_forcibly(root) { Some(value) => { match value.kind { @@ -183,7 +182,7 @@ impl Expression { } } - pub fn set<'a>(&self, root: &'a mut Value, value: Value) { + pub fn set(&self, root: &mut Value, value: Value) { match *self { Expression::Identifier(ref id) => { // Ensure that root is a table @@ -244,20 +243,16 @@ impl Expression { _ => *parent = Vec::::new().into(), } - match parent.kind { - ValueKind::Array(ref mut array) => { - let uindex = sindex_to_uindex(index, array.len()); - if uindex >= array.len() { - array.resize( - (uindex + 1) as usize, - Value::new(None, ValueKind::Nil), - ); - } - - array[uindex] = value.clone(); + if let ValueKind::Array(ref mut array) = parent.kind { + let uindex = sindex_to_uindex(index, array.len()); + if uindex >= array.len() { + array.resize( + (uindex + 1) as usize, + Value::new(None, ValueKind::Nil), + ); } - _ => (), + array[uindex] = value; } } } diff --git a/src/ser.rs b/src/ser.rs index b5b19ed..c3b93d6 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -48,7 +48,7 @@ impl ConfigSerializer { self.keys .get_mut(len - 1) .map(|pair| pair.1 = pair.1.map(|i| i + 1).or(Some(0))) - .ok_or(ConfigError::Message(format!( + .ok_or_else(|| ConfigError::Message(format!( "last key is not found in {} keys", len ))) diff --git a/src/source.rs b/src/source.rs index e4c4972..4d1ba53 100644 --- a/src/source.rs +++ b/src/source.rs @@ -7,7 +7,7 @@ use value::{Value, ValueKind}; /// Describes a generic _source_ of configuration properties. pub trait Source: Debug { - fn clone_into_box(&self) -> Box; + fn clone_into_box(&self) -> Box; /// Collect all configuration properties available from this source and return /// a HashMap. @@ -35,14 +35,14 @@ pub trait Source: Debug { } } -impl Clone for Box { - fn clone(&self) -> Box { +impl Clone for Box { + fn clone(&self) -> Box { self.clone_into_box() } } -impl Source for Vec> { - fn clone_into_box(&self) -> Box { +impl Source for Vec> { + fn clone_into_box(&self) -> Box { Box::new((*self).clone()) } @@ -67,7 +67,7 @@ where T: Clone, T: 'static, { - fn clone_into_box(&self) -> Box { + fn clone_into_box(&self) -> Box { Box::new((*self).clone()) } diff --git a/src/value.rs b/src/value.rs index 2db195d..177acf7 100644 --- a/src/value.rs +++ b/src/value.rs @@ -39,7 +39,7 @@ where impl From for ValueKind { fn from(value: String) -> Self { - ValueKind::String(value.into()) + ValueKind::String(value) } } @@ -179,17 +179,17 @@ impl Value { // Unexpected type ValueKind::Nil => Err(ConfigError::invalid_type( - self.origin.clone(), + self.origin, Unexpected::Unit, "a boolean", )), ValueKind::Table(_) => Err(ConfigError::invalid_type( - self.origin.clone(), + self.origin, Unexpected::Map, "a boolean", )), ValueKind::Array(_) => Err(ConfigError::invalid_type( - self.origin.clone(), + self.origin, Unexpected::Seq, "a boolean", )), @@ -224,17 +224,17 @@ impl Value { // Unexpected type ValueKind::Nil => Err(ConfigError::invalid_type( - self.origin.clone(), + self.origin, Unexpected::Unit, "an integer", )), ValueKind::Table(_) => Err(ConfigError::invalid_type( - self.origin.clone(), + self.origin, Unexpected::Map, "an integer", )), ValueKind::Array(_) => Err(ConfigError::invalid_type( - self.origin.clone(), + self.origin, Unexpected::Seq, "an integer", )), @@ -269,17 +269,17 @@ impl Value { // Unexpected type ValueKind::Nil => Err(ConfigError::invalid_type( - self.origin.clone(), + self.origin, Unexpected::Unit, "a floating point", )), ValueKind::Table(_) => Err(ConfigError::invalid_type( - self.origin.clone(), + self.origin, Unexpected::Map, "a floating point", )), ValueKind::Array(_) => Err(ConfigError::invalid_type( - self.origin.clone(), + self.origin, Unexpected::Seq, "a floating point", )), @@ -500,7 +500,7 @@ impl<'de> Deserialize<'de> for Value { { let mut vec = Array::new(); - while let Some(elem) = try!(visitor.next_element()) { + while let Some(elem) = visitor.next_element()? { vec.push(elem); } @@ -513,7 +513,7 @@ impl<'de> Deserialize<'de> for Value { { let mut values = Table::new(); - while let Some((key, value)) = try!(visitor.next_entry()) { + while let Some((key, value)) = visitor.next_entry()? { values.insert(key, value); } -- cgit v1.2.3 From e84a39949c0c1625c1886b8f718b8165c7a8e831 Mon Sep 17 00:00:00 2001 From: Federico Pasqua Date: Sat, 14 Mar 2020 16:36:06 +0100 Subject: Clippy fix for examples --- examples/global/src/main.rs | 2 +- examples/hierarchical-env/src/settings.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/global/src/main.rs b/examples/global/src/main.rs index 4fe0864..8e0e068 100644 --- a/examples/global/src/main.rs +++ b/examples/global/src/main.rs @@ -11,7 +11,7 @@ lazy_static! { static ref SETTINGS: RwLock = RwLock::new(Config::default()); } -fn try_main() -> Result<(), Box> { +fn try_main() -> Result<(), Box> { // Set property SETTINGS.write()?.set("property", 42)?; diff --git a/examples/hierarchical-env/src/settings.rs b/examples/hierarchical-env/src/settings.rs index c908f46..ad23163 100644 --- a/examples/hierarchical-env/src/settings.rs +++ b/examples/hierarchical-env/src/settings.rs @@ -46,7 +46,7 @@ impl Settings { // Add in the current environment file // Default to 'development' env // Note that this file is _optional_ - let env = env::var("RUN_MODE").unwrap_or("development".into()); + let env = env::var("RUN_MODE").unwrap_or_else(|_| "development".into()); s.merge(File::with_name(&format!("config/{}", env)).required(false))?; // Add in a local configuration file -- cgit v1.2.3