Skip to content

Commit 23565b6

Browse files
committed
Allow deserializing an enum from any mapping
1 parent 7351dca commit 23565b6

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/de.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -269,21 +269,21 @@ impl<'a, 'py, 'de> de::Deserializer<'de> for &'a mut Depythonizer<'py> {
269269
V: de::Visitor<'de>,
270270
{
271271
let item = &self.input;
272-
if let Ok(d) = item.downcast::<PyDict>() {
273-
// Get the enum variant from the dict key
274-
if d.len() != 1 {
272+
if let Ok(s) = item.downcast::<PyString>() {
273+
visitor.visit_enum(s.to_cow()?.into_deserializer())
274+
} else if let Ok(m) = item.downcast::<PyMapping>() {
275+
// Get the enum variant from the mapping key
276+
if m.len()? != 1 {
275277
return Err(PythonizeError::invalid_length_enum());
276278
}
277-
let variant = d
278-
.keys()
279+
let variant: Bound<PyString> = m
280+
.keys()?
279281
.get_item(0)?
280282
.downcast_into::<PyString>()
281283
.map_err(|_| PythonizeError::dict_key_not_string())?;
282-
let value = d.get_item(&variant)?.unwrap();
284+
let value = m.get_item(&variant)?;
283285
let mut de = Depythonizer::from_object_bound(value);
284286
visitor.visit_enum(PyEnumAccess::new(&mut de, variant))
285-
} else if let Ok(s) = item.downcast::<PyString>() {
286-
visitor.visit_enum(s.to_cow()?.into_deserializer())
287287
} else {
288288
Err(PythonizeError::invalid_enum_type())
289289
}

0 commit comments

Comments
 (0)