diff --git a/econf-derive/src/lib.rs b/econf-derive/src/lib.rs index d9c5839..4d98f4d 100644 --- a/econf-derive/src/lib.rs +++ b/econf-derive/src/lib.rs @@ -29,7 +29,7 @@ fn is_skip(f: &Field) -> bool { f.attrs.iter().any(|a| { a.path.is_ident("econf") && matches!(a.parse_meta().unwrap(), Meta::List(meta) if meta.nested.iter().any(|nm| { - matches!(nm, NestedMeta::Meta(Meta::Word(word)) if word.to_string() == "skip") + matches!(nm, NestedMeta::Meta(Meta::Word(word)) if *word == "skip") })) }) } @@ -44,9 +44,7 @@ fn find_renaming(f: &Field) -> Option { }) .flat_map(|nested| nested.into_iter()) .filter_map(|nested| match nested { - NestedMeta::Meta(Meta::NameValue(value)) if value.ident.to_string() == "rename" => { - Some(value) - } + NestedMeta::Meta(Meta::NameValue(value)) if value.ident == "rename" => Some(value), _ => None, }) .find_map(|value| match value.lit { diff --git a/econf/src/lib.rs b/econf/src/lib.rs index 52c7e23..dc325d0 100644 --- a/econf/src/lib.rs +++ b/econf/src/lib.rs @@ -18,16 +18,14 @@ //! y: u64, //! } //! -//! fn main() { -//! let a = A { -//! x: true, -//! y: 42, -//! }; -//! println!("Before: {:?}", a); -//! -//! let a = econf::load(a, "PREFIX"); -//! println!("After: {:?}", a); -//! } +//! let a = A { +//! x: true, +//! y: 42, +//! }; +//! println!("Before: {:?}", a); +//! +//! let a = econf::load(a, "PREFIX"); +//! println!("After: {:?}", a); //! ``` //! //! ```sh @@ -86,17 +84,15 @@ //! v2: usize, //! } //! -//! fn main() { -//! let a = A { -//! v1: 1, -//! v2: B { -//! v1: 2, -//! v2: 3, -//! }, -//! }; +//! let a = A { +//! v1: 1, +//! v2: B { +//! v1: 2, +//! v2: 3, +//! }, +//! }; //! -//! let a = econf::load(a, "PREFIX"); -//! } +//! let a = econf::load(a, "PREFIX"); //! ``` //! //! In this example, @@ -122,17 +118,15 @@ //! v2: usize, //! } //! -//! fn main() { -//! let a = A { -//! v2_v1: 1, -//! v2: B { -//! v1: 2, -//! v2: 3, -//! }, -//! }; +//! let a = A { +//! v2_v1: 1, +//! v2: B { +//! v1: 2, +//! v2: 3, +//! }, +//! }; //! -//! let a = econf::load(a, "PREFIX"); -//! } +//! let a = econf::load(a, "PREFIX"); //! ``` //! //! Here `PREFIX_V2_V1` corresponds to both `a.v2_v1` and `a.v2.v1`. In this case, `econf` prints warning through [`log facade`](https://docs.rs/log/latest/log/) and the value is loaded to both `a.v2_v1` and `a.v2.v1`. @@ -313,15 +307,13 @@ impl_load_env_tuples! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, } /// y: u64, /// } /// -/// fn main() { -/// let a = A { -/// x: true, -/// y: 42, -/// }; +/// let a = A { +/// x: true, +/// y: 42, +/// }; /// -/// let a = econf::load(a, "FOO"); -/// // Here we get `A` with some members overridden by environment variables. -/// } +/// let a = econf::load(a, "FOO"); +/// // Here we get `A` with some members overridden by environment variables. /// ``` /// pub fn load(data: T, prefix: &str) -> T @@ -334,6 +326,6 @@ where impl LoadEnv for std::time::Duration { fn load(self, path: &str, loader: &mut Loader) -> Self { - loader.load_and_map(self, path, |s| humantime::parse_duration(s)) + loader.load_and_map(self, path, humantime::parse_duration) } } diff --git a/econf/src/loader.rs b/econf/src/loader.rs index db569fb..2be3341 100644 --- a/econf/src/loader.rs +++ b/econf/src/loader.rs @@ -10,6 +10,12 @@ pub struct Loader { names: HashSet, } +impl Default for Loader { + fn default() -> Self { + Self::new() + } +} + impl Loader { /// Create the instance. pub fn new() -> Self { @@ -56,9 +62,9 @@ impl Loader { /// ``` /// pub fn load_and_map(&mut self, fallback: T, name: &str, map: F) -> T - where - F: FnOnce(&str) -> Result, - E: Display, + where + F: FnOnce(&str) -> Result, + E: Display, { let name = name.to_uppercase(); @@ -108,15 +114,15 @@ impl Loader { /// ``` /// pub fn load_from_yaml(&mut self, fallback: T, name: &str) -> T - where - T: DeserializeOwned, + where + T: DeserializeOwned, { self.load_and_map(fallback, name, |s| serde_yaml::from_str(s)) } /// Loads an environment variable then converts it to a specific type using [`from_str`](std::str::FromStr::from_str). /// - /// If loading/conversion is successful, the function returns the new value loaded. Otherwise, returns `fallback`. + /// If loading/conversion is successful, the function returns the value loaded. Otherwise, returns `fallback`. /// /// ``` /// # use econf::Loader; @@ -135,9 +141,9 @@ impl Loader { /// ``` /// pub fn load_from_str(&mut self, fallback: T, name: &str) -> T - where - T: FromStr, - T::Err: Display, + where + T: FromStr, + T::Err: Display, { self.load_and_map(fallback, name, |s| T::from_str(s)) }