|
| 1 | +use quote::quote; |
| 2 | +use syn::punctuated::Punctuated; |
| 3 | +use syn::token::Plus; |
| 4 | +use syn::{ |
| 5 | + Generics, Ident, Item, ItemFn, ItemImpl, ItemTrait, TraitItemFn, Type, TypeParamBound, |
| 6 | + Visibility, parse_quote, parse2, |
| 7 | +}; |
| 8 | + |
| 9 | +use crate::traits::AddTypeParamBounds; |
| 10 | +use crate::types::attributes::FunctionAttributes; |
| 11 | +use crate::types::implicits::ImplicitArgFields; |
| 12 | + |
| 13 | +pub struct PreprocessedItemCgpFn { |
| 14 | + pub ident: Ident, |
| 15 | + pub item_fn: ItemFn, |
| 16 | + pub implicit_args: ImplicitArgFields, |
| 17 | + pub attributes: FunctionAttributes, |
| 18 | + pub visibility: Visibility, |
| 19 | + pub generics: Generics, |
| 20 | +} |
| 21 | + |
| 22 | +impl PreprocessedItemCgpFn { |
| 23 | + pub fn to_items(&self) -> syn::Result<Vec<Item>> { |
| 24 | + let item_trait = self.to_item_trait()?; |
| 25 | + let item_impl = self.to_item_impl()?; |
| 26 | + |
| 27 | + Ok(vec![item_trait.into(), item_impl.into()]) |
| 28 | + } |
| 29 | + |
| 30 | + pub fn to_item_trait(&self) -> syn::Result<ItemTrait> { |
| 31 | + let Self { |
| 32 | + ident, |
| 33 | + item_fn, |
| 34 | + attributes, |
| 35 | + generics, |
| 36 | + visibility, |
| 37 | + .. |
| 38 | + } = self; |
| 39 | + |
| 40 | + let trait_item_fn = TraitItemFn { |
| 41 | + attrs: item_fn.attrs.clone(), |
| 42 | + sig: item_fn.sig.clone(), |
| 43 | + default: None, |
| 44 | + semi_token: None, |
| 45 | + }; |
| 46 | + |
| 47 | + let mut item_trait: ItemTrait = parse2(quote! { |
| 48 | + pub trait #ident { |
| 49 | + #trait_item_fn |
| 50 | + } |
| 51 | + })?; |
| 52 | + |
| 53 | + item_trait.generics = generics.clone(); |
| 54 | + item_trait.generics.where_clause = None; |
| 55 | + |
| 56 | + item_trait.supertraits.extend(attributes.extend.clone()); |
| 57 | + |
| 58 | + if !attributes.extend_where.is_empty() { |
| 59 | + item_trait |
| 60 | + .generics |
| 61 | + .make_where_clause() |
| 62 | + .predicates |
| 63 | + .extend(attributes.extend_where.clone()); |
| 64 | + } |
| 65 | + |
| 66 | + attributes.use_type.transform_item_trait(&mut item_trait)?; |
| 67 | + |
| 68 | + item_trait.attrs.extend(attributes.raw_attributes.clone()); |
| 69 | + item_trait.vis = visibility.clone(); |
| 70 | + |
| 71 | + Ok(item_trait) |
| 72 | + } |
| 73 | + |
| 74 | + pub fn to_item_impl(&self) -> syn::Result<ItemImpl> { |
| 75 | + let Self { |
| 76 | + ident, |
| 77 | + item_fn, |
| 78 | + implicit_args, |
| 79 | + attributes, |
| 80 | + generics, |
| 81 | + .. |
| 82 | + } = self; |
| 83 | + |
| 84 | + let type_generics = generics.split_for_impl().1; |
| 85 | + |
| 86 | + let self_type: Type = parse_quote!(Self); |
| 87 | + |
| 88 | + let mut item_impl: ItemImpl = parse2(quote! { |
| 89 | + impl #ident #type_generics for __Context__ { |
| 90 | + #item_fn |
| 91 | + } |
| 92 | + })?; |
| 93 | + |
| 94 | + item_impl.generics = generics.clone(); |
| 95 | + item_impl |
| 96 | + .generics |
| 97 | + .params |
| 98 | + .insert(0, parse_quote!(__Context__)); |
| 99 | + |
| 100 | + item_impl |
| 101 | + .generics |
| 102 | + .params |
| 103 | + .extend(attributes.impl_generics.clone()); |
| 104 | + |
| 105 | + { |
| 106 | + let mut bounds: Punctuated<TypeParamBound, Plus> = Punctuated::default(); |
| 107 | + bounds.extend(attributes.extend.clone()); |
| 108 | + |
| 109 | + for import in attributes.uses.iter() { |
| 110 | + bounds.push(parse2(quote! { #import })?); |
| 111 | + } |
| 112 | + |
| 113 | + if !bounds.is_empty() { |
| 114 | + item_impl |
| 115 | + .generics |
| 116 | + .make_where_clause() |
| 117 | + .predicates |
| 118 | + .push(parse2(quote! { |
| 119 | + Self: #bounds |
| 120 | + })?); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + if !attributes.extend_where.is_empty() { |
| 125 | + item_impl |
| 126 | + .generics |
| 127 | + .make_where_clause() |
| 128 | + .predicates |
| 129 | + .extend(attributes.extend_where.clone()); |
| 130 | + } |
| 131 | + |
| 132 | + implicit_args.add_type_param_bounds(&self_type, &mut item_impl.generics)?; |
| 133 | + |
| 134 | + attributes.use_type.transform_item_impl(&mut item_impl)?; |
| 135 | + attributes |
| 136 | + .use_provider |
| 137 | + .add_type_param_bounds(&self_type, &mut item_impl.generics)?; |
| 138 | + |
| 139 | + item_impl.attrs.extend(attributes.raw_attributes.clone()); |
| 140 | + |
| 141 | + Ok(item_impl) |
| 142 | + } |
| 143 | +} |
0 commit comments