Skip to content

Commit

Permalink
Fix clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
yukinarit committed Jul 20, 2024
1 parent 7e93f89 commit db944bb
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 52 deletions.
6 changes: 2 additions & 4 deletions econf-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}))
})
}
Expand All @@ -44,9 +44,7 @@ fn find_renaming(f: &Field) -> Option<String> {
})
.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 {
Expand Down
70 changes: 31 additions & 39 deletions econf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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`.
Expand Down Expand Up @@ -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<T>(data: T, prefix: &str) -> T
Expand All @@ -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)
}
}
24 changes: 15 additions & 9 deletions econf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ pub struct Loader {
names: HashSet<String>,
}

impl Default for Loader {
fn default() -> Self {
Self::new()
}
}

impl Loader {
/// Create the instance.
pub fn new() -> Self {
Expand Down Expand Up @@ -56,9 +62,9 @@ impl Loader {
/// ```
///
pub fn load_and_map<T, F, E>(&mut self, fallback: T, name: &str, map: F) -> T
where
F: FnOnce(&str) -> Result<T, E>,
E: Display,
where
F: FnOnce(&str) -> Result<T, E>,
E: Display,
{
let name = name.to_uppercase();

Expand Down Expand Up @@ -108,15 +114,15 @@ impl Loader {
/// ```
///
pub fn load_from_yaml<T>(&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;
Expand All @@ -135,9 +141,9 @@ impl Loader {
/// ```
///
pub fn load_from_str<T>(&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))
}
Expand Down

0 comments on commit db944bb

Please sign in to comment.