Skip to content

Commit c0d2eb0

Browse files
authored
Merge pull request #45 from twihno/master
Migrate to syn v2
2 parents e889dcc + 6c543d3 commit c0d2eb0

File tree

11 files changed

+230
-113
lines changed

11 files changed

+230
-113
lines changed

Cargo.toml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
[workspace]
2-
members = [
3-
"envconfig",
4-
"envconfig_derive",
5-
"test_suite"
6-
]
2+
resolver = "2"
3+
members = ["envconfig", "envconfig_derive", "test_suite"]
4+
5+
[workspace.package]
6+
edition = "2021"
7+
8+
[workspace.lints.rust]
9+
unsafe_code = "forbid"
10+
11+
[workspace.lints.clippy]
12+
pedantic = "warn"

envconfig/Cargo.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ categories = ["config", "web-programming"]
77
keywords = ["config", "env", "macro", "configuration", "environment"]
88
license = "MIT"
99
repository = "https://github.com/greyblake/envconfig-rs"
10-
homepage = "https://github.com/greyblake/envconfig-rs"
11-
documentation = "https://docs.rs/envconfig"
1210
readme = "README.md"
13-
edition = "2018"
11+
edition.workspace = true
1412

15-
[dev-dependencies]
13+
[lints]
14+
workspace = true
1615

1716
[dependencies]
1817
envconfig_derive = { version = "0.10.0", path = "../envconfig_derive" }

envconfig/src/error.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use std::error::Error as StdError;
2-
use std::fmt;
1+
//! Errors resulting from calling functions in this crate
2+
3+
use std::{error::Error as StdError, fmt};
34

45
/// Represents an error, that may be returned by `fn init_from_env()` of trait `Envconfig`.
56
#[derive(Debug, PartialEq)]
@@ -11,9 +12,11 @@ pub enum Error {
1112
impl fmt::Display for Error {
1213
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1314
match self {
14-
Error::EnvVarMissing { name } => write!(f, "Environment variable {} is missing", name),
15+
Error::EnvVarMissing { name } => {
16+
write!(f, "Environment variable {name} is missing")
17+
}
1518
Error::ParseError { name } => {
16-
write!(f, "Failed to parse environment variable {}", name)
19+
write!(f, "Failed to parse environment variable {name}")
1720
}
1821
}
1922
}

envconfig/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
//! env::set_var("DB_PORT", "5432");
2626
//!
2727
//! // Initialize config from environment variables
28-
//! let config = Config::init().unwrap();
28+
//! let config = Config::init_from_env().unwrap();
2929
//!
3030
//! assert_eq!(config.db_host, "localhost");
3131
//! assert_eq!(config.db_port, Some(5432));

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: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@ use std::str::FromStr;
44
use crate::error::Error;
55
use std::collections::HashMap;
66

7-
/// Load a nenvironment variable by name and parse it into type `T`.
8-
/// The function is used by `envconfig_derive` to implement `init()`.
7+
/// Load an environment variable by name and parse it into type `T`.
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: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ categories = ["config", "web-programming"]
77
keywords = ["config", "env", "macro", "configuration", "environment"]
88
license = "MIT"
99
repository = "https://github.com/greyblake/envconfig-rs"
10-
homepage = "https://github.com/greyblake/envconfig-rs"
11-
documentation = "https://docs.rs/envconfig_derive"
1210
readme = "README.md"
13-
edition = "2018"
11+
edition.workspace = true
12+
13+
[lints]
14+
workspace = true
1415

1516
[lib]
1617
proc-macro = true
1718

1819
[dependencies]
19-
syn = "1.0.17"
20-
quote = "1.0.3"
21-
proc-macro2 = "1.0.9"
20+
syn = { version = "2.0.77", features = ["parsing", "derive"] }
21+
quote = { version = "1.0.37", features = [] }
22+
proc-macro2 = { version = "1.0.86", features = [] }

0 commit comments

Comments
 (0)