feat: allow arbitrary deserialisation from unions
This commit is contained in:
parent
7bc61de1c2
commit
b4ce0c764e
4 changed files with 52 additions and 45 deletions
|
|
@ -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)*
|
||||
}
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue