Skip to content
This repository has been archived by the owner on Jan 29, 2023. It is now read-only.

Commit

Permalink
Add support for enum as keys in maps
Browse files Browse the repository at this point in the history
  • Loading branch information
mredaelli authored and mockersf committed May 10, 2020
1 parent bc3e167 commit 8330c95
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/serde/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,17 @@ impl<'de, 'a, R: Read> serde::de::Deserializer<'de> for &'a mut Deserializer<R>
})?
.clone();

if let Index::String(ref s) = self.current_field {
for v in _variants {
if s == v {
let reader = HoconRead::new(hc);
let deserializer = &mut Deserializer::new(reader);
deserializer.current_field = Index::String(String::from(s));
return visitor.visit_enum(UnitVariantAccess::new(deserializer));
}
}
}

match &hc {
Hocon::String(name) => {
let index = Index::String(String::from(name));
Expand Down Expand Up @@ -933,6 +944,39 @@ mod tests {
assert_eq!(res.expect("during test").item.get("Hello"), Some(&7));
}

#[test]
fn map_with_enum_keys() {
#[derive(Deserialize, Debug, Hash, PartialEq, Eq)]
enum E {
A,
B,
}

let mut hm = HashMap::new();
hm.insert(String::from("A"), Hocon::Integer(1));
hm.insert(String::from("B"), Hocon::Integer(2));
let doc = Hocon::Hash(hm);

let res: super::Result<HashMap<E, u8>> = dbg!(super::from_hocon(dbg!(doc)));
assert!(res.is_ok());
assert_eq!(res.expect("during test").get(&E::A), Some(&1));

#[derive(Deserialize, Debug)]
struct S {
s: u8,
}

let mut hm = HashMap::new();
let mut hm_sub = HashMap::new();
hm_sub.insert(String::from("s"), Hocon::Integer(7));
hm.insert(String::from("A"), Hocon::Hash(hm_sub));
let doc = Hocon::Hash(hm);

let res: super::Result<HashMap<E, S>> = dbg!(super::from_hocon(dbg!(doc)));
assert!(res.is_ok());
assert_eq!(res.expect("during test").get(&E::A).unwrap().s, 7);
}

#[derive(Deserialize, Debug, PartialEq)]
enum MyEnum {
UnitVariant,
Expand Down

0 comments on commit 8330c95

Please sign in to comment.