From 6ecfeec624ae9e145b66431188bae31c17bcd154 Mon Sep 17 00:00:00 2001 From: Daniel Eades Date: Sat, 29 Jan 2022 12:14:37 +0100 Subject: use 'Self' to refer to own type --- src/builder.rs | 4 +-- src/config.rs | 14 +++++----- src/de.rs | 4 +-- src/env.rs | 6 ++--- src/error.rs | 20 +++++++------- src/file/format/json5.rs | 4 +-- src/file/mod.rs | 10 +++---- src/file/source/file.rs | 4 +-- src/file/source/string.rs | 2 +- src/path/mod.rs | 36 ++++++++++++------------- src/source.rs | 2 +- src/value.rs | 68 +++++++++++++++++++++++------------------------ tests/async_builder.rs | 2 +- tests/defaults.rs | 2 +- 14 files changed, 89 insertions(+), 89 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index b641560..018713c 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -147,7 +147,7 @@ impl ConfigBuilder { /// # Errors /// /// Fails if `Expression::from_str(key)` fails. - pub fn set_default(mut self, key: S, value: T) -> Result> + pub fn set_default(mut self, key: S, value: T) -> Result where S: AsRef, T: Into, @@ -164,7 +164,7 @@ impl ConfigBuilder { /// # Errors /// /// Fails if `Expression::from_str(key)` fails. - pub fn set_override(mut self, key: S, value: T) -> Result> + pub fn set_override(mut self, key: S, value: T) -> Result where S: AsRef, T: Into, diff --git a/src/config.rs b/src/config.rs index e003daa..27b318d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -26,7 +26,7 @@ pub struct Config { impl Default for Config { fn default() -> Self { - Config { + Self { defaults: Default::default(), overrides: Default::default(), sources: Default::default(), @@ -37,9 +37,9 @@ impl Default for Config { impl Config { pub(crate) fn new(value: Value) -> Self { - Config { + Self { cache: value, - ..Default::default() + ..Self::default() } } @@ -50,7 +50,7 @@ impl Config { /// Merge in a configuration property source. #[deprecated(since = "0.12.0", note = "please use 'ConfigBuilder' instead")] - pub fn merge(&mut self, source: T) -> Result<&mut Config> + pub fn merge(&mut self, source: T) -> Result<&mut Self> where T: 'static, T: Source + Send + Sync, @@ -81,7 +81,7 @@ impl Config { /// Configuration is automatically refreshed after a mutation /// operation (`set`, `merge`, `set_default`, etc.). #[deprecated(since = "0.12.0", note = "please use 'ConfigBuilder' instead")] - pub fn refresh(&mut self) -> Result<&mut Config> { + pub fn refresh(&mut self) -> Result<&mut Self> { self.cache = { let mut cache: Value = Map::::new().into(); @@ -106,7 +106,7 @@ impl Config { /// Set a default `value` at `key` #[deprecated(since = "0.12.0", note = "please use 'ConfigBuilder' instead")] - pub fn set_default(&mut self, key: &str, value: T) -> Result<&mut Config> + pub fn set_default(&mut self, key: &str, value: T) -> Result<&mut Self> where T: Into, { @@ -125,7 +125,7 @@ impl Config { /// /// Errors if config is frozen #[deprecated(since = "0.12.0", note = "please use 'ConfigBuilder' instead")] - pub fn set(&mut self, key: &str, value: T) -> Result<&mut Config> + pub fn set(&mut self, key: &str, value: T) -> Result<&mut Self> where T: Into, { diff --git a/src/de.rs b/src/de.rs index 2d5722d..9df347f 100644 --- a/src/de.rs +++ b/src/de.rs @@ -168,7 +168,7 @@ struct SeqAccess { impl SeqAccess { fn new(elements: Vec) -> Self { - SeqAccess { + Self { elements: elements.into_iter().enumerate(), } } @@ -204,7 +204,7 @@ struct MapAccess { impl MapAccess { fn new(table: Map) -> Self { - MapAccess { + Self { elements: table.into_iter().collect(), } } diff --git a/src/env.rs b/src/env.rs index 17c706f..ef91ef8 100644 --- a/src/env.rs +++ b/src/env.rs @@ -67,13 +67,13 @@ pub struct Environment { impl Environment { #[deprecated(since = "0.12.0", note = "please use 'Environment::default' instead")] pub fn new() -> Self { - Environment::default() + Self::default() } pub fn with_prefix(s: &str) -> Self { - Environment { + Self { prefix: Some(s.into()), - ..Environment::default() + ..Self::default() } } diff --git a/src/error.rs b/src/error.rs index daeb081..f955a28 100644 --- a/src/error.rs +++ b/src/error.rs @@ -92,7 +92,7 @@ impl ConfigError { unexpected: Unexpected, expected: &'static str, ) -> Self { - ConfigError::Type { + Self::Type { origin, unexpected, expected, @@ -104,8 +104,8 @@ impl ConfigError { // TODO: for now only json5 checked, need to finish others #[doc(hidden)] pub fn invalid_root(origin: Option<&String>, unexpected: Unexpected) -> Box { - Box::new(ConfigError::Type { - origin: origin.map(|s| s.to_owned()), + Box::new(Self::Type { + origin: origin.cloned(), unexpected, expected: "a map", key: None, @@ -117,12 +117,12 @@ impl ConfigError { #[must_use] pub fn extend_with_key(self, key: &str) -> Self { match self { - ConfigError::Type { + Self::Type { origin, unexpected, expected, .. - } => ConfigError::Type { + } => Self::Type { origin, unexpected, expected, @@ -145,18 +145,18 @@ impl ConfigError { format!("{}{}{}", segment, dot, key) }; match self { - ConfigError::Type { + Self::Type { origin, unexpected, expected, key, - } => ConfigError::Type { + } => Self::Type { origin, unexpected, expected, key: Some(concat(key)), }, - ConfigError::NotFound(key) => ConfigError::NotFound(concat(Some(key))), + Self::NotFound(key) => Self::NotFound(concat(Some(key))), _ => self, } } @@ -233,12 +233,12 @@ impl Error for ConfigError {} impl de::Error for ConfigError { fn custom(msg: T) -> Self { - ConfigError::Message(msg.to_string()) + Self::Message(msg.to_string()) } } impl ser::Error for ConfigError { fn custom(msg: T) -> Self { - ConfigError::Message(msg.to_string()) + Self::Message(msg.to_string()) } } diff --git a/src/file/format/json5.rs b/src/file/format/json5.rs index 8f9f605..92aaa8f 100644 --- a/src/file/format/json5.rs +++ b/src/file/format/json5.rs @@ -12,8 +12,8 @@ pub enum Val { Integer(i64), Float(f64), String(String), - Array(Vec), - Object(Map), + Array(Vec), + Object(Map), } pub fn parse( diff --git a/src/file/mod.rs b/src/file/mod.rs index d4f3e1a..65f3fd6 100644 --- a/src/file/mod.rs +++ b/src/file/mod.rs @@ -43,7 +43,7 @@ where F: FileStoredFormat + 'static, { pub fn from_str(s: &str, format: F) -> Self { - File { + Self { format: Some(format), required: true, source: s.into(), @@ -56,7 +56,7 @@ where F: FileStoredFormat + 'static, { pub fn new(name: &str, format: F) -> Self { - File { + Self { format: Some(format), required: true, source: source::file::FileSourceFile::new(name.into()), @@ -68,7 +68,7 @@ impl File { /// Given the basename of a file, will attempt to locate a file by setting its /// extension to a registered format. pub fn with_name(name: &str) -> Self { - File { + Self { format: None, required: true, source: source::file::FileSourceFile::new(name.into()), @@ -78,7 +78,7 @@ impl File { impl<'a> From<&'a Path> for File { fn from(path: &'a Path) -> Self { - File { + Self { format: None, required: true, source: source::file::FileSourceFile::new(path.to_path_buf()), @@ -88,7 +88,7 @@ impl<'a> From<&'a Path> for File { impl From for File { fn from(path: PathBuf) -> Self { - File { + Self { format: None, required: true, source: source::file::FileSourceFile::new(path), diff --git a/src/file/source/file.rs b/src/file/source/file.rs index eaa771b..403f3f2 100644 --- a/src/file/source/file.rs +++ b/src/file/source/file.rs @@ -16,8 +16,8 @@ pub struct FileSourceFile { } impl FileSourceFile { - pub fn new(name: PathBuf) -> FileSourceFile { - FileSourceFile { name } + pub fn new(name: PathBuf) -> Self { + Self { name } } fn find_file( diff --git a/src/file/source/string.rs b/src/file/source/string.rs index 577c1e7..89300d4 100644 --- a/src/file/source/string.rs +++ b/src/file/source/string.rs @@ -12,7 +12,7 @@ pub struct FileSourceString(String); impl<'a> From<&'a str> for FileSourceString { fn from(s: &'a str) -> Self { - FileSourceString(s.into()) + Self(s.into()) } } diff --git a/src/path/mod.rs b/src/path/mod.rs index baed000..8d59999 100644 --- a/src/path/mod.rs +++ b/src/path/mod.rs @@ -9,14 +9,14 @@ mod parser; #[derive(Debug, Eq, PartialEq, Clone, Hash)] pub enum Expression { Identifier(String), - Child(Box, String), - Subscript(Box, isize), + Child(Box, String), + Subscript(Box, isize), } impl FromStr for Expression { type Err = ConfigError; - fn from_str(s: &str) -> Result { + fn from_str(s: &str) -> Result { parser::from_str(s).map_err(ConfigError::PathParse) } } @@ -32,7 +32,7 @@ fn sindex_to_uindex(index: isize, len: usize) -> usize { impl Expression { pub fn get(self, root: &Value) -> Option<&Value> { match self { - Expression::Identifier(id) => { + Self::Identifier(id) => { match root.kind { // `x` access on a table is equivalent to: map[x] ValueKind::Table(ref map) => map.get(&id), @@ -42,7 +42,7 @@ impl Expression { } } - Expression::Child(expr, key) => { + Self::Child(expr, key) => { match expr.get(root) { Some(value) => { match value.kind { @@ -58,7 +58,7 @@ impl Expression { } } - Expression::Subscript(expr, index) => match expr.get(root) { + Self::Subscript(expr, index) => match expr.get(root) { Some(value) => match value.kind { ValueKind::Array(ref array) => { let index = sindex_to_uindex(index, array.len()); @@ -80,13 +80,13 @@ impl Expression { pub fn get_mut<'a>(&self, root: &'a mut Value) -> Option<&'a mut Value> { match *self { - Expression::Identifier(ref id) => match root.kind { + Self::Identifier(ref id) => match root.kind { ValueKind::Table(ref mut map) => map.get_mut(id), _ => None, }, - Expression::Child(ref expr, ref key) => match expr.get_mut(root) { + Self::Child(ref expr, ref key) => match expr.get_mut(root) { Some(value) => match value.kind { ValueKind::Table(ref mut map) => map.get_mut(key), @@ -96,7 +96,7 @@ impl Expression { _ => None, }, - Expression::Subscript(ref expr, index) => match expr.get_mut(root) { + Self::Subscript(ref expr, index) => match expr.get_mut(root) { Some(value) => match value.kind { ValueKind::Array(ref mut array) => { let index = sindex_to_uindex(index, array.len()); @@ -118,7 +118,7 @@ impl Expression { pub fn get_mut_forcibly<'a>(&self, root: &'a mut Value) -> Option<&'a mut Value> { match *self { - Expression::Identifier(ref id) => match root.kind { + Self::Identifier(ref id) => match root.kind { ValueKind::Table(ref mut map) => Some( map.entry(id.clone()) .or_insert_with(|| Value::new(None, ValueKind::Nil)), @@ -127,7 +127,7 @@ impl Expression { _ => None, }, - Expression::Child(ref expr, ref key) => match expr.get_mut_forcibly(root) { + Self::Child(ref expr, ref key) => match expr.get_mut_forcibly(root) { Some(value) => { if let ValueKind::Table(ref mut map) = value.kind { Some( @@ -151,7 +151,7 @@ impl Expression { _ => None, }, - Expression::Subscript(ref expr, index) => match expr.get_mut_forcibly(root) { + Self::Subscript(ref expr, index) => match expr.get_mut_forcibly(root) { Some(value) => { match value.kind { ValueKind::Array(_) => (), @@ -180,7 +180,7 @@ impl Expression { pub fn set(&self, root: &mut Value, value: Value) { match *self { - Expression::Identifier(ref id) => { + Self::Identifier(ref id) => { // Ensure that root is a table match root.kind { ValueKind::Table(_) => {} @@ -202,7 +202,7 @@ impl Expression { // Continue the deep merge for (key, val) in incoming_map { - Expression::Identifier(key.clone()).set(target, val.clone()); + Self::Identifier(key.clone()).set(target, val.clone()); } } @@ -219,20 +219,20 @@ impl Expression { } } - Expression::Child(ref expr, ref key) => { + Self::Child(ref expr, ref key) => { if let Some(parent) = expr.get_mut_forcibly(root) { if let ValueKind::Table(_) = parent.kind { - Expression::Identifier(key.clone()).set(parent, value); + Self::Identifier(key.clone()).set(parent, value); } else { // Didn't find a table. Oh well. Make a table and do this anyway *parent = Map::::new().into(); - Expression::Identifier(key.clone()).set(parent, value); + Self::Identifier(key.clone()).set(parent, value); } } } - Expression::Subscript(ref expr, index) => { + Self::Subscript(ref expr, index) => { if let Some(parent) = expr.get_mut_forcibly(root) { match parent.kind { ValueKind::Array(_) => (), diff --git a/src/source.rs b/src/source.rs index 060657b..94f1c35 100644 --- a/src/source.rs +++ b/src/source.rs @@ -76,7 +76,7 @@ impl Clone for Box { } impl Clone for Box { - fn clone(&self) -> Box { + fn clone(&self) -> Self { self.clone_into_box() } } diff --git a/src/value.rs b/src/value.rs index e403192..c3dad10 100644 --- a/src/value.rs +++ b/src/value.rs @@ -30,103 +30,103 @@ pub type Table = Map; impl Default for ValueKind { fn default() -> Self { - ValueKind::Nil + Self::Nil } } impl From> for ValueKind where - T: Into, + T: Into, { fn from(value: Option) -> Self { match value { Some(value) => value.into(), - None => ValueKind::Nil, + None => Self::Nil, } } } impl From for ValueKind { fn from(value: String) -> Self { - ValueKind::String(value) + Self::String(value) } } impl<'a> From<&'a str> for ValueKind { fn from(value: &'a str) -> Self { - ValueKind::String(value.into()) + Self::String(value.into()) } } impl From for ValueKind { fn from(value: i8) -> Self { - ValueKind::I64(value.into()) + Self::I64(value.into()) } } impl From for ValueKind { fn from(value: i16) -> Self { - ValueKind::I64(value.into()) + Self::I64(value.into()) } } impl From for ValueKind { fn from(value: i32) -> Self { - ValueKind::I64(value.into()) + Self::I64(value.into()) } } impl From for ValueKind { fn from(value: i64) -> Self { - ValueKind::I64(value) + Self::I64(value) } } impl From for ValueKind { fn from(value: i128) -> Self { - ValueKind::I128(value) + Self::I128(value) } } impl From for ValueKind { fn from(value: u8) -> Self { - ValueKind::U64(value.into()) + Self::U64(value.into()) } } impl From for ValueKind { fn from(value: u16) -> Self { - ValueKind::U64(value.into()) + Self::U64(value.into()) } } impl From for ValueKind { fn from(value: u32) -> Self { - ValueKind::U64(value.into()) + Self::U64(value.into()) } } impl From for ValueKind { fn from(value: u64) -> Self { - ValueKind::U64(value) + Self::U64(value) } } impl From for ValueKind { fn from(value: u128) -> Self { - ValueKind::U128(value) + Self::U128(value) } } impl From for ValueKind { fn from(value: f64) -> Self { - ValueKind::Float(value) + Self::Float(value) } } impl From for ValueKind { fn from(value: bool) -> Self { - ValueKind::Boolean(value) + Self::Boolean(value) } } @@ -136,7 +136,7 @@ where { fn from(values: Map) -> Self { let t = values.into_iter().map(|(k, v)| (k, v.into())).collect(); - ValueKind::Table(t) + Self::Table(t) } } @@ -145,28 +145,28 @@ where T: Into, { fn from(values: Vec) -> Self { - ValueKind::Array(values.into_iter().map(T::into).collect()) + Self::Array(values.into_iter().map(T::into).collect()) } } impl Display for ValueKind { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { - ValueKind::String(ref value) => write!(f, "{}", value), - ValueKind::Boolean(value) => write!(f, "{}", value), - ValueKind::I64(value) => write!(f, "{}", value), - ValueKind::I128(value) => write!(f, "{}", value), - ValueKind::U64(value) => write!(f, "{}", value), - ValueKind::U128(value) => write!(f, "{}", value), - ValueKind::Float(value) => write!(f, "{}", value), - ValueKind::Nil => write!(f, "nil"), - ValueKind::Table(ref table) => write!(f, "{{ {} }}", { + Self::String(ref value) => write!(f, "{}", value), + Self::Boolean(value) => write!(f, "{}", value), + Self::I64(value) => write!(f, "{}", value), + Self::I128(value) => write!(f, "{}", value), + Self::U64(value) => write!(f, "{}", value), + Self::U128(value) => write!(f, "{}", value), + Self::Float(value) => write!(f, "{}", value), + Self::Nil => write!(f, "nil"), + Self::Table(ref table) => write!(f, "{{ {} }}", { table .iter() .map(|(k, v)| format!("{} => {}, ", k, v)) .collect::() }), - ValueKind::Array(ref array) => write!(f, "{:?}", { + Self::Array(ref array) => write!(f, "{:?}", { array.iter().map(|e| format!("{}, ", e)).collect::() }), } @@ -204,7 +204,7 @@ impl Value { where V: Into, { - Value { + Self { origin: origin.cloned(), kind: kind.into(), } @@ -567,7 +567,7 @@ impl Value { /// Returns `self` into an array, if possible // FIXME: Should this not be `try_into_*` ? - pub fn into_array(self) -> Result> { + pub fn into_array(self) -> Result> { match self.kind { ValueKind::Array(value) => Ok(value), @@ -622,7 +622,7 @@ impl Value { /// If the `Value` is a Table, returns the associated Map. // FIXME: Should this not be `try_into_*` ? - pub fn into_table(self) -> Result> { + pub fn into_table(self) -> Result> { match self.kind { ValueKind::Table(value) => Ok(value), @@ -678,7 +678,7 @@ impl Value { impl<'de> Deserialize<'de> for Value { #[inline] - fn deserialize(deserializer: D) -> ::std::result::Result + fn deserialize(deserializer: D) -> ::std::result::Result where D: Deserializer<'de>, { @@ -821,7 +821,7 @@ where T: Into, { fn from(value: T) -> Self { - Value { + Self { origin: None, kind: value.into(), } diff --git a/tests/async_builder.rs b/tests/async_builder.rs index 51be9ca..bead9d3 100644 --- a/tests/async_builder.rs +++ b/tests/async_builder.rs @@ -12,7 +12,7 @@ struct AsyncFile { /// This is a test only implementation to be used in tests impl AsyncFile { pub fn new(path: String, format: FileFormat) -> Self { - AsyncFile { path, format } + Self { path, format } } } diff --git a/tests/defaults.rs b/tests/defaults.rs index 4539097..f740259 100644 --- a/tests/defaults.rs +++ b/tests/defaults.rs @@ -10,7 +10,7 @@ pub struct Settings { impl Default for Settings { fn default() -> Self { - Settings { + Self { db_host: String::from("default"), } } -- cgit v1.2.3