Skip to content

Commit

Permalink
Merge pull request #286 from danieleades/refactor/code-quality
Browse files Browse the repository at this point in the history
improve code quality
  • Loading branch information
matthiasbeyer authored Jan 29, 2022
2 parents 53e43fb + aaa1601 commit 68e0025
Show file tree
Hide file tree
Showing 29 changed files with 123 additions and 127 deletions.
2 changes: 1 addition & 1 deletion examples/global/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ fn try_main() -> Result<(), Box<dyn Error>> {
}

fn main() {
try_main().unwrap()
try_main().unwrap();
}
4 changes: 2 additions & 2 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand All @@ -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>,
Expand Down
14 changes: 7 additions & 7 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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()
}
}

Expand All @@ -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,
Expand Down Expand Up @@ -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();

Expand All @@ -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>,
{
Expand All @@ -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>,
{
Expand Down
4 changes: 2 additions & 2 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ struct SeqAccess {

impl SeqAccess {
fn new(elements: Vec<Value>) -> Self {
SeqAccess {
Self {
elements: elements.into_iter().enumerate(),
}
}
Expand Down Expand Up @@ -204,7 +204,7 @@ struct MapAccess {

impl MapAccess {
fn new(table: Map<String, Value>) -> Self {
MapAccess {
Self {
elements: table.into_iter().collect(),
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl ConfigError {
unexpected: Unexpected,
expected: &'static str,
) -> Self {
ConfigError::Type {
Self::Type {
origin,
unexpected,
expected,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
}
}
Expand Down Expand Up @@ -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())
}
}
4 changes: 2 additions & 2 deletions src/file/format/json5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 5 additions & 5 deletions src/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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()),
Expand All @@ -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()),
Expand All @@ -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()),
Expand All @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions src/file/source/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>(
Expand Down
2 changes: 1 addition & 1 deletion src/file/source/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

Expand Down
Loading

0 comments on commit 68e0025

Please sign in to comment.