Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clippy warnings #27

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 24 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@ struct A {
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 @@ -87,17 +85,15 @@ struct B {
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 @@ struct B {
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
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)
}
}
22 changes: 14 additions & 8 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,8 +114,8 @@ 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))
}
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
Loading