diff options
author | Nadrieril Feneanar | 2019-04-06 00:43:09 +0200 |
---|---|---|
committer | GitHub | 2019-04-06 00:43:09 +0200 |
commit | 5eccde86fc3ccdeb34c9f8bb44de33d25e77f30c (patch) | |
tree | 76d0f0fc848887d2945d586b58847575ca31d0da /dhall_generator/src/dhall_type.rs | |
parent | f78af6d1e7f6c1dc39bde6cf97138327004ddb06 (diff) | |
parent | 6a675a13fcfafa057c44db84c3b0ca3b344cfdab (diff) |
Merge pull request #48 from Nadrieril/exprf
Move recursion out of Expr for enhanced genericity
Diffstat (limited to 'dhall_generator/src/dhall_type.rs')
-rw-r--r-- | dhall_generator/src/dhall_type.rs | 50 |
1 files changed, 18 insertions, 32 deletions
diff --git a/dhall_generator/src/dhall_type.rs b/dhall_generator/src/dhall_type.rs index ac8eb4e..ec023bc 100644 --- a/dhall_generator/src/dhall_type.rs +++ b/dhall_generator/src/dhall_type.rs @@ -39,21 +39,17 @@ pub fn derive_for_struct( .collect(), syn::Fields::Unit => vec![], }; - let fields = fields.into_iter().map(|(name, ty)| { - constraints.push(ty.clone()); - quote! { - m.insert( - dhall_core::Label::from(#name), - <#ty as dhall::Type>::get_type() - ); - } - }); - Ok(quote! { dhall_core::rc(dhall_core::Expr::RecordType({ - use std::collections::BTreeMap; - let mut m = BTreeMap::new(); - #(#fields)* - m - })) }) + let fields = fields + .into_iter() + .map(|(name, ty)| { + let name = dhall_core::Label::from(name); + constraints.push(ty.clone()); + (name, quote!(<#ty as dhall::Type>::get_type())) + }) + .collect(); + let record = + crate::dhall_expr::quote_expr(dhall_core::ExprF::RecordType(fields)); + Ok(quote! { dhall_core::rc(#record) }) } pub fn derive_for_enum( @@ -64,7 +60,7 @@ pub fn derive_for_enum( .variants .iter() .map(|v| { - let name = v.ident.to_string(); + let name = dhall_core::Label::from(v.ident.to_string()); let ty = match &v.fields { syn::Fields::Unnamed(fields) if fields.unnamed.is_empty() => { Err(Error::new( @@ -92,21 +88,13 @@ pub fn derive_for_enum( }; let ty = ty?; constraints.push(ty.clone()); - Ok(quote! { - m.insert( - dhall_core::Label::from(#name), - <#ty as dhall::Type>::get_type() - ); - }) + Ok((name, quote!(<#ty as dhall::Type>::get_type()))) }) - .collect::<Result<Vec<_>, Error>>()?; + .collect::<Result<_, Error>>()?; - Ok(quote! { dhall_core::rc(dhall_core::Expr::UnionType({ - use std::collections::BTreeMap; - let mut m = BTreeMap::new(); - #(#variants)* - m - })) }) + let union = + crate::dhall_expr::quote_expr(dhall_core::ExprF::UnionType(variants)); + Ok(quote! { dhall_core::rc(#union) }) } pub fn derive_type_inner( @@ -168,9 +156,7 @@ pub fn derive_type_inner( // Ensure that all the fields have a Type impl let mut where_clause = orig_where_clause.clone(); for ty in constraints.iter() { - where_clause - .predicates - .push(parse_quote!(#ty: dhall::Type)); + where_clause.predicates.push(parse_quote!(#ty: dhall::Type)); } let ident = &input.ident; |