diff options
author | Daniel Eades <danieleades@hotmail.com> | 2022-01-29 12:14:37 +0100 |
---|---|---|
committer | Daniel Eades <danieleades@hotmail.com> | 2022-01-29 14:01:49 +0100 |
commit | 6ecfeec624ae9e145b66431188bae31c17bcd154 (patch) | |
tree | 82a552f664dd98c5e55007aff914632834810e33 | |
parent | 53e43fbcf96b5c2a661d052a6e3d55fc3709f1e1 (diff) |
use 'Self' to refer to own type
-rw-r--r-- | src/builder.rs | 4 | ||||
-rw-r--r-- | src/config.rs | 14 | ||||
-rw-r--r-- | src/de.rs | 4 | ||||
-rw-r--r-- | src/env.rs | 6 | ||||
-rw-r--r-- | src/error.rs | 20 | ||||
-rw-r--r-- | src/file/format/json5.rs | 4 | ||||
-rw-r--r-- | src/file/mod.rs | 10 | ||||
-rw-r--r-- | src/file/source/file.rs | 4 | ||||
-rw-r--r-- | src/file/source/string.rs | 2 | ||||
-rw-r--r-- | src/path/mod.rs | 36 | ||||
-rw-r--r-- | src/source.rs | 2 | ||||
-rw-r--r-- | src/value.rs | 68 | ||||
-rw-r--r-- | tests/async_builder.rs | 2 | ||||
-rw-r--r-- | 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<St: BuilderState> ConfigBuilder<St> { /// # Errors /// /// Fails if `Expression::from_str(key)` fails. - pub fn set_default<S, T>(mut self, key: S, value: T) -> Result<ConfigBuilder<St>> + pub fn set_default<S, T>(mut self, key: S, value: T) -> Result<Self> where S: AsRef<str>, T: Into<Value>, @@ -164,7 +164,7 @@ impl<St: BuilderState> ConfigBuilder<St> { /// # Errors /// /// Fails if `Expression::from_str(key)` fails. - pub fn set_override<S, T>(mut self, key: S, value: T) -> Result<ConfigBuilder<St>> + pub fn set_override<S, T>(mut self, key: S, value: T) -> Result<Self> where S: AsRef<str>, T: Into<Value>, 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<T>(&mut self, source: T) -> Result<&mut Config> + pub fn merge<T>(&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::<String, Value>::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<T>(&mut self, key: &str, value: T) -> Result<&mut Config> + pub fn set_default<T>(&mut self, key: &str, value: T) -> Result<&mut Self> where T: Into<Value>, { @@ -125,7 +125,7 @@ impl Config { /// /// Errors if config is frozen #[deprecated(since = "0.12.0", note = "please use 'ConfigBuilder' instead")] - pub fn set<T>(&mut self, key: &str, value: T) -> Result<&mut Config> + pub fn set<T>(&mut self, key: &str, value: T) -> Result<&mut Self> where T: Into<Value>, { @@ -168,7 +168,7 @@ struct SeqAccess { impl SeqAccess { fn new(elements: Vec<Value>) -> Self { - SeqAccess { + Self { elements: elements.into_iter().enumerate(), } } @@ -204,7 +204,7 @@ struct MapAccess { impl MapAccess { fn new(table: Map<String, Value>) -> Self { - MapAccess { + Self { elements: table.into_iter().collect(), } } @@ -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<Self> { - 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<T: fmt::Display>(msg: T) -> Self { - ConfigError::Message(msg.to_string()) + Self::Message(msg.to_string()) } } impl ser::Error for ConfigError { fn custom<T: fmt::Display>(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<Val>), - Object(Map<String, Val>), + Array(Vec<Self>), + Object(Map<String, Self>), } 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<source::file::FileSourceFile, FileFormat> { /// 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<source::file::FileSourceFile, FileFormat> { impl<'a> From<&'a Path> for File<source::file::FileSourceFile, FileFormat> { 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<source::file::FileSourceFile, FileFormat> { impl From<PathBuf> for File<source::file::FileSourceFile, FileFormat> { 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<F>( 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<Expression>, String), - Subscript(Box<Expression>, isize), + Child(Box<Self>, String), + Subscript(Box<Self>, isize), } impl FromStr for Expression { type Err = ConfigError; - fn from_str(s: &str) -> Result<Expression> { + fn from_str(s: &str) -> Result<Self> { 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::<String, Value>::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<dyn AsyncSource + Send + Sync> { } impl Clone for Box<dyn Source + Send + Sync> { - fn clone(&self) -> Box<dyn Source + Send + Sync> { + 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<String, Value>; impl Default for ValueKind { fn default() -> Self { - ValueKind::Nil + Self::Nil } } impl<T> From<Option<T>> for ValueKind where - T: Into<ValueKind>, + T: Into<Self>, { fn from(value: Option<T>) -> Self { match value { Some(value) => value.into(), - None => ValueKind::Nil, + None => Self::Nil, } } } impl From<String> 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<i8> for ValueKind { fn from(value: i8) -> Self { - ValueKind::I64(value.into()) + Self::I64(value.into()) } } impl From<i16> for ValueKind { fn from(value: i16) -> Self { - ValueKind::I64(value.into()) + Self::I64(value.into()) } } impl From<i32> for ValueKind { fn from(value: i32) -> Self { - ValueKind::I64(value.into()) + Self::I64(value.into()) } } impl From<i64> for ValueKind { fn from(value: i64) -> Self { - ValueKind::I64(value) + Self::I64(value) } } impl From<i128> for ValueKind { fn from(value: i128) -> Self { - ValueKind::I128(value) + Self::I128(value) } } impl From<u8> for ValueKind { fn from(value: u8) -> Self { - ValueKind::U64(value.into()) + Self::U64(value.into()) } } impl From<u16> for ValueKind { fn from(value: u16) -> Self { - ValueKind::U64(value.into()) + Self::U64(value.into()) } } impl From<u32> for ValueKind { fn from(value: u32) -> Self { - ValueKind::U64(value.into()) + Self::U64(value.into()) } } impl From<u64> for ValueKind { fn from(value: u64) -> Self { - ValueKind::U64(value) + Self::U64(value) } } impl From<u128> for ValueKind { fn from(value: u128) -> Self { - ValueKind::U128(value) + Self::U128(value) } } impl From<f64> for ValueKind { fn from(value: f64) -> Self { - ValueKind::Float(value) + Self::Float(value) } } impl From<bool> for ValueKind { fn from(value: bool) -> Self { - ValueKind::Boolean(value) + Self::Boolean(value) } } @@ -136,7 +136,7 @@ where { fn from(values: Map<String, T>) -> 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<Value>, { fn from(values: Vec<T>) -> 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::<String>() }), - ValueKind::Array(ref array) => write!(f, "{:?}", { + Self::Array(ref array) => write!(f, "{:?}", { array.iter().map(|e| format!("{}, ", e)).collect::<String>() }), } @@ -204,7 +204,7 @@ impl Value { where V: Into<ValueKind>, { - 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<Vec<Value>> { + pub fn into_array(self) -> Result<Vec<Self>> { 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<Map<String, Value>> { + pub fn into_table(self) -> Result<Map<String, Self>> { match self.kind { ValueKind::Table(value) => Ok(value), @@ -678,7 +678,7 @@ impl Value { impl<'de> Deserialize<'de> for Value { #[inline] - fn deserialize<D>(deserializer: D) -> ::std::result::Result<Value, D::Error> + fn deserialize<D>(deserializer: D) -> ::std::result::Result<Self, D::Error> where D: Deserializer<'de>, { @@ -821,7 +821,7 @@ where T: Into<ValueKind>, { 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"), } } |