Skip to content

Commit d7f54f1

Browse files
authored
Merge pull request #62 from juntyr/named-mapping-items
Support serialising to named mappings
2 parents 6c2c3f2 + 7c18b9f commit d7f54f1

6 files changed

+345
-82
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
- `Depythonizer` now contains a `&Bound` and so has an extra lifetime `'bound`
1111
- `Depythonizer::from_object()` now takes a `&Bound` and is no longer deprecated
1212
- Fix overflow error attempting to depythonize `u64` values greater than `i64::MAX` to types like `serde_json::Value`
13+
- Implement `PythonizeListType` for `PyTuple`
14+
- Support deserializing enums from any `PyMapping` instead of just `PyDict`
15+
- Support serializing struct-like types to named mappings using `PythonizeTypes::NamedMap`
1316

1417
## 0.21.1 - 2024-04-02
1518

src/de.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -294,21 +294,21 @@ impl<'a, 'py, 'de, 'bound> de::Deserializer<'de> for &'a mut Depythonizer<'py, '
294294
V: de::Visitor<'de>,
295295
{
296296
let item = &self.input;
297-
if let Ok(d) = item.downcast::<PyDict>() {
298-
// Get the enum variant from the dict key
299-
if d.len() != 1 {
297+
if let Ok(s) = item.downcast::<PyString>() {
298+
visitor.visit_enum(s.to_cow()?.into_deserializer())
299+
} else if let Ok(m) = item.downcast::<PyMapping>() {
300+
// Get the enum variant from the mapping key
301+
if m.len()? != 1 {
300302
return Err(PythonizeError::invalid_length_enum());
301303
}
302-
let variant = d
303-
.keys()
304+
let variant: Bound<PyString> = m
305+
.keys()?
304306
.get_item(0)?
305307
.downcast_into::<PyString>()
306308
.map_err(|_| PythonizeError::dict_key_not_string())?;
307-
let value = d.get_item(&variant)?.unwrap();
309+
let value = m.get_item(&variant)?;
308310
let mut de = Depythonizer::from_object(&value);
309311
visitor.visit_enum(PyEnumAccess::new(&mut de, variant))
310-
} else if let Ok(s) = item.downcast::<PyString>() {
311-
visitor.visit_enum(s.to_cow()?.into_deserializer())
312312
} else {
313313
Err(PythonizeError::invalid_enum_type())
314314
}

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ pub use crate::de::depythonize_bound;
4545
pub use crate::de::{depythonize, Depythonizer};
4646
pub use crate::error::{PythonizeError, Result};
4747
pub use crate::ser::{
48-
pythonize, pythonize_custom, PythonizeDefault, PythonizeDictType, PythonizeListType,
49-
PythonizeTypes, Pythonizer,
48+
pythonize, pythonize_custom, PythonizeDefault, PythonizeListType, PythonizeMappingType,
49+
PythonizeNamedMappingType, PythonizeTypes, PythonizeUnnamedMappingAdapter, Pythonizer,
5050
};

0 commit comments

Comments
 (0)