Skip to content

Commit

Permalink
Allow deserializing an enum from any mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
juntyr committed May 18, 2024
1 parent 7351dca commit 23565b6
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,21 +269,21 @@ impl<'a, 'py, 'de> de::Deserializer<'de> for &'a mut Depythonizer<'py> {
V: de::Visitor<'de>,
{
let item = &self.input;
if let Ok(d) = item.downcast::<PyDict>() {
// Get the enum variant from the dict key
if d.len() != 1 {
if let Ok(s) = item.downcast::<PyString>() {
visitor.visit_enum(s.to_cow()?.into_deserializer())
} else if let Ok(m) = item.downcast::<PyMapping>() {
// Get the enum variant from the mapping key
if m.len()? != 1 {
return Err(PythonizeError::invalid_length_enum());
}
let variant = d
.keys()
let variant: Bound<PyString> = m
.keys()?
.get_item(0)?
.downcast_into::<PyString>()
.map_err(|_| PythonizeError::dict_key_not_string())?;
let value = d.get_item(&variant)?.unwrap();
let value = m.get_item(&variant)?;
let mut de = Depythonizer::from_object_bound(value);
visitor.visit_enum(PyEnumAccess::new(&mut de, variant))
} else if let Ok(s) = item.downcast::<PyString>() {
visitor.visit_enum(s.to_cow()?.into_deserializer())
} else {
Err(PythonizeError::invalid_enum_type())
}
Expand Down

0 comments on commit 23565b6

Please sign in to comment.