Skip to content

Commit

Permalink
RUST-1243 Handle enum keys when deserializing a map from binary (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
abr-egn committed Mar 28, 2022
1 parent 9c481ac commit 69fa4e2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
23 changes: 23 additions & 0 deletions serde-tests/test.rs
Expand Up @@ -393,6 +393,29 @@ fn hashmap() {
run_test(&v, &doc, "hashmap");
}

#[test]
fn hashmap_enum_key() {
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
struct Foo {
map: BTreeMap<Bar, String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash, PartialOrd, Ord)]
enum Bar {
Baz,
}

let obj = Foo {
map: BTreeMap::from_iter([(Bar::Baz, "2".to_owned())]),
};
let doc = doc! {
"map": {
"Baz": "2",
},
};
run_test(&obj, &doc, "hashmap_enum_key");
}

#[test]
fn tuple_struct() {
#[derive(Serialize, Deserialize, PartialEq, Debug)]
Expand Down
18 changes: 17 additions & 1 deletion src/de/raw.rs
Expand Up @@ -642,6 +642,22 @@ impl<'d, 'de> serde::de::Deserializer<'de> for DocumentKeyDeserializer<'d, 'de>
}
}

fn deserialize_enum<V>(
self,
_name: &str,
_variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value>
where
V: serde::de::Visitor<'de>,
{
visitor.visit_enum(
self.root_deserializer
.deserialize_cstr()?
.into_deserializer(),
)
}

fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
where
V: serde::de::Visitor<'de>,
Expand All @@ -655,7 +671,7 @@ impl<'d, 'de> serde::de::Deserializer<'de> for DocumentKeyDeserializer<'d, 'de>

forward_to_deserialize_any! {
bool char str bytes byte_buf option unit unit_struct string
identifier seq tuple tuple_struct struct map enum
identifier seq tuple tuple_struct struct map
ignored_any i8 i16 i32 i64 u8 u16 u32 u64 f32 f64
}
}
Expand Down

0 comments on commit 69fa4e2

Please sign in to comment.