Skip to content

Commit

Permalink
Merge pull request #910 from Chia-Network/altendky-patch-8
Browse files Browse the repository at this point in the history
add unnamed single-field struct support to `PyJsonDict`
  • Loading branch information
altendky authored Feb 5, 2025
2 parents b836f85 + 73fdb9f commit 7fa053a
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion crates/chia_py_streamable_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,32 @@ pub fn py_json_dict_macro(input: proc_macro::TokenStream) -> proc_macro::TokenSt
}
});
}
syn::Fields::Unnamed(FieldsUnnamed { unnamed, .. }) if unnamed.len() == 1 => {
let ftype: syn::Type = unnamed
.first()
.expect("match arm if requires 1 item")
.ty
.clone();

py_protocol.extend( quote! {

impl #crate_name::to_json_dict::ToJsonDict for #ident {
fn to_json_dict(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::PyObject> {
Ok(self.0.to_json_dict(py)?.into())
}
}

impl #crate_name::from_json_dict::FromJsonDict for #ident {
fn from_json_dict(o: &pyo3::Bound<pyo3::PyAny>) -> pyo3::PyResult<Self> {
Ok(Self(
<#ftype as #crate_name::from_json_dict::FromJsonDict>::from_json_dict(&o)?
))
}
}
});
}
_ => {
panic!("PyJsonDict only supports structs");
panic!("PyJsonDict only supports named structs and single field unnamed structs");
}
}

Expand Down

0 comments on commit 7fa053a

Please sign in to comment.