summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorstuebinm2021-04-14 18:40:24 +0200
committerstuebinm2021-04-14 18:40:24 +0200
commit449d72e98e427b3f81dc72e800098fce8895353b (patch)
tree93ab816f0285be94ec0f04aded05b6a5af71a073
parentc7f7821b9180854f7f5509f5117a862b285eb7fb (diff)
derive macro: support records in union types
-rw-r--r--dhall_proc_macros/src/derive.rs20
1 files changed, 15 insertions, 5 deletions
diff --git a/dhall_proc_macros/src/derive.rs b/dhall_proc_macros/src/derive.rs
index 0975d98..a6b39ad 100644
--- a/dhall_proc_macros/src/derive.rs
+++ b/dhall_proc_macros/src/derive.rs
@@ -78,15 +78,25 @@ fn derive_for_enum(
constraints.push(ty.clone());
let ty = static_type(ty);
Ok(quote!( (#name.to_owned(), Some(#ty)) ))
- }
+ },
syn::Fields::Unnamed(_) => Err(Error::new(
v.span(),
"Derive StaticType: Variants with more than one field are not supported",
)),
- syn::Fields::Named(_) => Err(Error::new(
- v.span(),
- "Derive StaticType: Named variants are not supported",
- )),
+ syn::Fields::Named(fields) => {
+ let entries = fields
+ .named
+ .iter()
+ .map(|field| {
+ constraints.push(field.ty.clone());
+ let ty = static_type(&field.ty);
+ let name = field.ident.as_ref().unwrap().to_string();
+ quote!( (#name.to_owned(), #ty) ) });
+ let record = quote! {::serde_dhall::SimpleType::Record(
+ vec![ #(#entries),* ].into_iter().collect()
+ )};
+ Ok(quote!( (#name.to_owned(), Some(#record)) ))
+ }
}
})
.collect::<Result<_, Error>>()?;