mirror of
https://github.com/OMGeeky/confique.git
synced 2026-02-23 15:38:30 +01:00
Allow "yes" & "no" for bool env vars and do case-insensitive matching
Closes #34
This commit is contained in:
@@ -6,6 +6,9 @@ All notable changes to this project will be documented in this file.
|
|||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
- Add `#[config(partial_attr(...))]` struct attribute to specify attributes for
|
- Add `#[config(partial_attr(...))]` struct attribute to specify attributes for
|
||||||
the partial type.
|
the partial type.
|
||||||
|
- Allow "yes" and "no" as values when deserializing `bool` from env. Also, the
|
||||||
|
match is done completely case insensitive now, such that e.g. "True", "tRuE"
|
||||||
|
are accepted now.
|
||||||
|
|
||||||
## [0.2.4] - 2023-07-02
|
## [0.2.4] - 2023-07-02
|
||||||
- Fixed enum deserialization from env values
|
- Fixed enum deserialization from env values
|
||||||
|
|||||||
14
src/env/mod.rs
vendored
14
src/env/mod.rs
vendored
@@ -85,10 +85,16 @@ impl<'de> serde::Deserializer<'de> for Deserializer {
|
|||||||
where
|
where
|
||||||
V: serde::de::Visitor<'de>,
|
V: serde::de::Visitor<'de>,
|
||||||
{
|
{
|
||||||
let v = match self.value.trim() {
|
let s = self.value.trim();
|
||||||
"1" | "true" | "TRUE" => true,
|
let v = match () {
|
||||||
"0" | "false" | "FALSE" => false,
|
() if s == "1"
|
||||||
other => return Err(DeError(format!("invalid value for bool: '{other}'"))),
|
|| s.eq_ignore_ascii_case("true")
|
||||||
|
|| s.eq_ignore_ascii_case("yes") => true,
|
||||||
|
|
||||||
|
() if s == "0"
|
||||||
|
|| s.eq_ignore_ascii_case("false")
|
||||||
|
|| s.eq_ignore_ascii_case("no") => false,
|
||||||
|
_ => return Err(DeError(format!("invalid value for bool: '{s}'"))),
|
||||||
};
|
};
|
||||||
|
|
||||||
visitor.visit_bool(v)
|
visitor.visit_bool(v)
|
||||||
|
|||||||
9
src/env/tests.rs
vendored
9
src/env/tests.rs
vendored
@@ -9,10 +9,19 @@ fn de<'de, T: serde::Deserialize<'de>>(v: &'static str) -> Result<T, DeError> {
|
|||||||
fn boolean() {
|
fn boolean() {
|
||||||
assert_eq!(de("1"), Ok(true));
|
assert_eq!(de("1"), Ok(true));
|
||||||
assert_eq!(de("true "), Ok(true));
|
assert_eq!(de("true "), Ok(true));
|
||||||
|
assert_eq!(de(" True "), Ok(true));
|
||||||
assert_eq!(de(" TRUE"), Ok(true));
|
assert_eq!(de(" TRUE"), Ok(true));
|
||||||
|
assert_eq!(de("yes"), Ok(true));
|
||||||
|
assert_eq!(de(" Yes"), Ok(true));
|
||||||
|
assert_eq!(de("YES "), Ok(true));
|
||||||
|
|
||||||
assert_eq!(de("0 "), Ok(false));
|
assert_eq!(de("0 "), Ok(false));
|
||||||
assert_eq!(de(" false"), Ok(false));
|
assert_eq!(de(" false"), Ok(false));
|
||||||
|
assert_eq!(de(" False "), Ok(false));
|
||||||
assert_eq!(de("FALSE "), Ok(false));
|
assert_eq!(de("FALSE "), Ok(false));
|
||||||
|
assert_eq!(de("no"), Ok(false));
|
||||||
|
assert_eq!(de(" No"), Ok(false));
|
||||||
|
assert_eq!(de("NO "), Ok(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
Reference in New Issue
Block a user