feat: allow arbitrary deserialisation from unions

This commit is contained in:
TotallyNot 2025-05-01 15:51:51 +02:00
parent 7bc61de1c2
commit b4ce0c764e
Signed by: pyrite
GPG key ID: 7F1BA9170CD35D15
4 changed files with 52 additions and 45 deletions

View file

@ -33,7 +33,7 @@ impl Union {
let ty_name = format_ident!("{}", variant_name);
variants.push(quote! {
pub fn #accessor_name(&self) -> Result<crate::models::#ty_name, serde_json::Error> {
<crate::models::#ty_name as serde::Deserialize>::deserialize(&self.0)
self.deserialize()
}
});
}
@ -43,6 +43,13 @@ impl Union {
pub struct #name(serde_json::Value);
impl #name {
pub fn deserialize<'de, T>(&'de self) -> Result<T, serde_json::Error>
where
T: serde::Deserialize<'de>,
{
T::deserialize(&self.0)
}
#(#variants)*
}
})