Skip to content

Commit

Permalink
Support BTreeMap
Browse files Browse the repository at this point in the history
  • Loading branch information
lepton committed Feb 22, 2024
1 parent 830a5be commit 22ba563
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
5 changes: 4 additions & 1 deletion src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub(crate) trait TestConfigExt: ConfigSource + Sized + 'static {
impl<C: ConfigSource + 'static> TestConfigExt for C {}

type R<V> = Result<V, ConfigError>;
use std::collections::HashMap;
use std::collections::{BTreeMap, HashMap};

#[derive(Debug, FromConfig)]
struct ConfigSuit {
Expand All @@ -21,6 +21,8 @@ struct ConfigSuit {
brr: Vec<Vec<String>>,
#[config(name = "val")]
map: HashMap<String, usize>,
#[config(name = "val")]
bmap: BTreeMap<String, usize>,
#[config(name = "map")]
bap: HashMap<String, Vec<bool>>,
crr: Vec<FloatSuit>,
Expand Down Expand Up @@ -50,6 +52,7 @@ pub(crate) fn source_test_suit(src: impl ConfigSource + 'static) -> Result<(), C
assert_eq!(vec![brr], v.brr);
for i in 1..=3 {
assert_eq!(Some(&i), v.map.get(&format!("v{}", i)));
assert_eq!(Some(&i), v.bmap.get(&format!("v{}", i)));
}
assert_eq!(1, v.int.v1);
assert_eq!(2, v.int.v2);
Expand Down
45 changes: 32 additions & 13 deletions src/value.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
any::Any,
cmp::Ordering,
collections::{HashMap, HashSet},
collections::{BTreeMap, HashMap, HashSet},
ffi::OsString,
net::{IpAddr, Ipv4Addr, Ipv6Addr, Shutdown, SocketAddr, SocketAddrV4, SocketAddrV6},
path::PathBuf,
Expand Down Expand Up @@ -196,21 +196,28 @@ impl<V: FromConfig> FromConfig for Vec<V> {
}
}

impl<V: FromConfig> FromConfig for HashMap<String, V> {
#[inline]
fn from_config(
context: &mut ConfigContext<'_>,
_: Option<ConfigValue<'_>>,
) -> Result<Self, ConfigError> {
let mut vs = HashMap::new();
let list = context.collect_keys();
for k in list.str_key {
vs.insert(k.to_string(), context.parse_config(k, None)?);
macro_rules! impl_map {
($name:ident) => {
impl<V: FromConfig> FromConfig for $name<String, V> {
#[inline]
fn from_config(
context: &mut ConfigContext<'_>,
_: Option<ConfigValue<'_>>,
) -> Result<Self, ConfigError> {
let mut vs = $name::new();
let list = context.collect_keys();
for k in list.str_key {
vs.insert(k.to_string(), context.parse_config(k, None)?);
}
Ok(vs)
}
}
Ok(vs)
}
};
}

impl_map!(HashMap);
impl_map!(BTreeMap);

#[doc(hidden)]
pub trait FromValue: Sized {
fn from_value(
Expand Down Expand Up @@ -735,4 +742,16 @@ mod test {
let v: String = context.read(true).unwrap();
assert_eq!("true", v);
}

#[test]
#[allow(unused_qualifications)]
fn map_test() {
let mut context = TestContext::new();
let x: Result<BTreeMap<String, bool>, ConfigError> = context.read("val");
assert!(x.is_ok());
assert!(x.unwrap().is_empty());
let x: Result<HashMap<String, bool>, ConfigError> = context.read("val");
assert!(x.is_ok());
assert!(x.unwrap().is_empty());
}
}

0 comments on commit 22ba563

Please sign in to comment.