|
| 1 | +use convert_case::{Case, Casing}; |
| 2 | +use proc_macro2::{Ident, TokenStream}; |
| 3 | +use quote::quote_spanned; |
| 4 | +use synstructure::VariantInfo; |
| 5 | + |
| 6 | +const RUST_KEYWORDS: &[&str] = &[ |
| 7 | + "mut", "true", "false", "const", "static", "ref", "struct", "enum", "trait", "union", "fn", |
| 8 | + "use", "return", "move", "let", "break", "loop", "continue", "await", "if", "for", "unsafe", |
| 9 | +]; |
| 10 | + |
| 11 | +/// Create methods to build this type. |
| 12 | +/// |
| 13 | +/// For a struct, we create a single `new` method that takes each field. |
| 14 | +/// |
| 15 | +/// For an enum, we create methods named after each variant. |
| 16 | +pub(crate) fn constructor_methods(s: synstructure::Structure) -> TokenStream { |
| 17 | + match s.ast().data { |
| 18 | + syn::Data::Struct(_) => derive_new_for_struct(s), |
| 19 | + syn::Data::Enum(_) => derive_new_for_variants(s), |
| 20 | + syn::Data::Union(_) => return Default::default(), |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +fn derive_new_for_struct(s: synstructure::Structure<'_>) -> TokenStream { |
| 25 | + derive_new_for_variant( |
| 26 | + &s, |
| 27 | + &s.variants()[0], |
| 28 | + &Ident::new("new", s.ast().ident.span()), |
| 29 | + ) |
| 30 | +} |
| 31 | + |
| 32 | +fn derive_new_for_variants(s: synstructure::Structure<'_>) -> TokenStream { |
| 33 | + s.variants() |
| 34 | + .iter() |
| 35 | + .map(|v| { |
| 36 | + let mut fn_name = v.ast().ident.to_string().to_case(Case::Snake); |
| 37 | + if RUST_KEYWORDS.iter().any(|&kw| kw == fn_name) { |
| 38 | + fn_name.push('_'); |
| 39 | + } |
| 40 | + let fn_name = Ident::new(&fn_name, v.ast().ident.span()); |
| 41 | + derive_new_for_variant(&s, v, &fn_name) |
| 42 | + }) |
| 43 | + .collect() |
| 44 | +} |
| 45 | + |
| 46 | +fn derive_new_for_variant( |
| 47 | + s: &synstructure::Structure<'_>, |
| 48 | + v: &VariantInfo<'_>, |
| 49 | + fn_name: &Ident, |
| 50 | +) -> TokenStream { |
| 51 | + let type_name = &s.ast().ident; |
| 52 | + let (impl_generics, type_generics, where_clauses) = s.ast().generics.split_for_impl(); |
| 53 | + |
| 54 | + // If there are no bindings, not worth it. |
| 55 | + if v.bindings().is_empty() { |
| 56 | + return TokenStream::default(); |
| 57 | + } |
| 58 | + |
| 59 | + let binding_names = v.bindings().iter().map(|b| &b.binding).collect::<Vec<_>>(); |
| 60 | + let binding_types = v.bindings().iter().map(|b| &b.ast().ty).collect::<Vec<_>>(); |
| 61 | + let construct = v.construct(|_b, i| { |
| 62 | + let name = binding_names[i]; |
| 63 | + quote_spanned!( |
| 64 | + binding_names[i].span() => |
| 65 | + formality_core::Upcast::upcast(#name) |
| 66 | + ) |
| 67 | + }); |
| 68 | + |
| 69 | + quote_spanned! { v.ast().ident.span() => |
| 70 | + #[allow(dead_code)] |
| 71 | + impl #impl_generics #type_name #type_generics |
| 72 | + where #where_clauses |
| 73 | + { |
| 74 | + pub fn #fn_name( |
| 75 | + #( |
| 76 | + #binding_names: impl formality_core::Upcast<#binding_types>, |
| 77 | + )* |
| 78 | + ) -> Self { |
| 79 | + #construct |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments