Skip to content

Commit bb57441

Browse files
committed
fix: 🚨 Fix pedantic clippy warnings
1 parent 5003df5 commit bb57441

File tree

6 files changed

+137
-61
lines changed

6 files changed

+137
-61
lines changed

envconfig/src/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ impl fmt::Display for Error {
1313
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1414
match self {
1515
Error::EnvVarMissing { name } => {
16-
write!(f, "Environment variable {} is missing", name)
16+
write!(f, "Environment variable {name} is missing")
1717
}
1818
Error::ParseError { name } => {
19-
write!(f, "Failed to parse environment variable {}", name)
19+
write!(f, "Failed to parse environment variable {name}")
2020
}
2121
}
2222
}

envconfig/src/traits.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ use std::collections::HashMap;
44
/// Indicates that structure can be initialize from environment variables.
55
pub trait Envconfig {
66
/// Initialize structure from environment variables.
7-
/// Deprecated in favor of init_from_env().
7+
/// Deprecated in favor of [`::init_from_env()`].
8+
///
9+
/// # Errors
10+
/// - Environment variable is missing.
11+
/// - Failed to parse environment variable.
812
#[deprecated(
913
since = "0.9.0",
1014
note = "Function init() is deprecated. Please use init_from_env() instead."
@@ -14,11 +18,19 @@ pub trait Envconfig {
1418
Self: Sized;
1519

1620
/// Initialize structure from environment variables.
21+
///
22+
/// # Errors
23+
/// - Environment variable is missing.
24+
/// - Failed to parse environment variable.
1725
fn init_from_env() -> Result<Self, Error>
1826
where
1927
Self: Sized;
2028

2129
/// Initialize structure from a hashmap.
30+
///
31+
/// # Errors
32+
/// - Environment variable is missing.
33+
/// - Failed to parse environment variable.
2234
fn init_from_hashmap(hashmap: &HashMap<String, String>) -> Result<Self, Error>
2335
where
2436
Self: Sized;

envconfig/src/utils.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@ use crate::error::Error;
55
use std::collections::HashMap;
66

77
/// Load an environment variable by name and parse it into type `T`.
8-
/// The function is used by `envconfig_derive` to implement `init()`.
98
///
10-
/// It returns `Error` in the following cases:
9+
/// This function can also use a hashmap as a fallback or for testing purposes.
10+
///
11+
/// # Errors
1112
/// - Environment variable is not present
1213
/// - Parsing failed
13-
pub fn load_var<T: FromStr>(
14+
pub fn load_var<T: FromStr, S: ::std::hash::BuildHasher>(
1415
var_name: &'static str,
15-
hashmap: Option<&HashMap<String, String>>,
16+
hashmap: Option<&HashMap<String, String, S>>,
1617
) -> Result<T, Error> {
1718
match hashmap {
1819
None => env::var(var_name).ok(),
19-
Some(hashmap) => hashmap.get(var_name).map(|val| val.to_string()),
20+
Some(hashmap) => hashmap.get(var_name).map(std::string::ToString::to_string),
2021
}
2122
.ok_or(Error::EnvVarMissing { name: var_name })
2223
.and_then(|string_value| {
@@ -26,14 +27,21 @@ pub fn load_var<T: FromStr>(
2627
})
2728
}
2829

29-
pub fn load_var_with_default<T: FromStr>(
30+
/// Tries to load an environment variable by name and parse it into type `T`.
31+
/// If the environment variable is not present, it returns a default value.
32+
///
33+
/// This function can also use a hashmap as a fallback or for testing purposes.
34+
///
35+
/// # Errors
36+
/// - Parsing failed
37+
pub fn load_var_with_default<T: FromStr, S: ::std::hash::BuildHasher>(
3038
var_name: &'static str,
31-
hashmap: Option<&HashMap<String, String>>,
39+
hashmap: Option<&HashMap<String, String, S>>,
3240
default: &'static str,
3341
) -> Result<T, Error> {
3442
let opt_var = match hashmap {
3543
None => env::var(var_name).ok(),
36-
Some(hashmap) => hashmap.get(var_name).map(|val| val.to_string()),
44+
Some(hashmap) => hashmap.get(var_name).map(std::string::ToString::to_string),
3745
};
3846

3947
let string_value = match opt_var {
@@ -46,13 +54,20 @@ pub fn load_var_with_default<T: FromStr>(
4654
.map_err(|_| Error::ParseError { name: var_name })
4755
}
4856

49-
pub fn load_optional_var<T: FromStr>(
57+
/// Tries to load an environment variable by name and parse it into type `T`.
58+
/// If the environment variable is not present, it returns `None`.
59+
///
60+
/// This function can also use a hashmap as a fallback or for testing purposes.
61+
///
62+
/// # Errors
63+
/// - Parsing failed
64+
pub fn load_optional_var<T: FromStr, S: ::std::hash::BuildHasher>(
5065
var_name: &'static str,
51-
hashmap: Option<&HashMap<String, String>>,
66+
hashmap: Option<&HashMap<String, String, S>>,
5267
) -> Result<Option<T>, Error> {
5368
let opt_var = match hashmap {
5469
None => env::var(var_name).ok(),
55-
Some(hashmap) => hashmap.get(var_name).map(|val| val.to_string()),
70+
Some(hashmap) => hashmap.get(var_name).map(std::string::ToString::to_string),
5671
};
5772

5873
match opt_var {

envconfig_derive/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ workspace = true
1717
proc-macro = true
1818

1919
[dependencies]
20-
syn = { version = "2.0.76", features = ["parsing", "derive"] }
20+
syn = { version = "2.0.77", features = ["parsing", "derive"] }
2121
quote = { version = "1.0.37", features = [] }
2222
proc-macro2 = { version = "1.0.86", features = [] }

0 commit comments

Comments
 (0)