1 | use proc_macro2::TokenStream; |
2 | use quote::ToTokens; |
3 | use syn::Ident; |
4 | |
5 | use crate::codegen::FromDeriveInputImpl; |
6 | use crate::options::{DeriveInputShapeSet, OuterFrom, ParseAttribute, ParseData}; |
7 | use crate::{FromField, FromMeta, Result}; |
8 | |
9 | use super::forwarded_field::ForwardedField; |
10 | |
11 | #[derive (Debug)] |
12 | pub struct FdiOptions { |
13 | pub base: OuterFrom, |
14 | |
15 | /// The field on the target struct which should receive the type visibility, if any. |
16 | pub vis: Option<Ident>, |
17 | |
18 | /// The field on the target struct which should receive the type generics, if any. |
19 | pub generics: Option<Ident>, |
20 | |
21 | /// The field on the target struct which should receive the derive input body, if any. |
22 | pub data: Option<ForwardedField>, |
23 | |
24 | pub supports: Option<DeriveInputShapeSet>, |
25 | } |
26 | |
27 | impl FdiOptions { |
28 | pub fn new(di: &syn::DeriveInput) -> Result<Self> { |
29 | (FdiOptions { |
30 | base: OuterFrom::start(di)?, |
31 | vis: Default::default(), |
32 | generics: Default::default(), |
33 | data: Default::default(), |
34 | supports: Default::default(), |
35 | }) |
36 | .parse_attributes(&di.attrs)? |
37 | .parse_body(&di.data) |
38 | } |
39 | } |
40 | |
41 | impl ParseAttribute for FdiOptions { |
42 | fn parse_nested(&mut self, mi: &syn::Meta) -> Result<()> { |
43 | if mi.path().is_ident("supports" ) { |
44 | self.supports = FromMeta::from_meta(item:mi)?; |
45 | Ok(()) |
46 | } else { |
47 | self.base.parse_nested(mi) |
48 | } |
49 | } |
50 | } |
51 | |
52 | impl ParseData for FdiOptions { |
53 | fn parse_variant(&mut self, variant: &syn::Variant) -> Result<()> { |
54 | self.base.parse_variant(variant) |
55 | } |
56 | |
57 | fn parse_field(&mut self, field: &syn::Field) -> Result<()> { |
58 | match field.ident.as_ref().map(|v| v.to_string()).as_deref() { |
59 | Some("vis" ) => { |
60 | self.vis.clone_from(&field.ident); |
61 | Ok(()) |
62 | } |
63 | Some("data" ) => { |
64 | self.data = ForwardedField::from_field(field).map(Some)?; |
65 | Ok(()) |
66 | } |
67 | Some("generics" ) => { |
68 | self.generics.clone_from(&field.ident); |
69 | Ok(()) |
70 | } |
71 | _ => self.base.parse_field(field), |
72 | } |
73 | } |
74 | |
75 | fn validate_body(&self, errors: &mut crate::error::Accumulator) { |
76 | self.base.validate_body(errors); |
77 | } |
78 | } |
79 | |
80 | impl<'a> From<&'a FdiOptions> for FromDeriveInputImpl<'a> { |
81 | fn from(v: &'a FdiOptions) -> Self { |
82 | FromDeriveInputImpl { |
83 | base: (&v.base.container).into(), |
84 | attr_names: &v.base.attr_names, |
85 | from_ident: v.base.from_ident, |
86 | ident: v.base.ident.as_ref(), |
87 | vis: v.vis.as_ref(), |
88 | data: v.data.as_ref(), |
89 | generics: v.generics.as_ref(), |
90 | forward_attrs: v.base.as_forward_attrs(), |
91 | supports: v.supports.as_ref(), |
92 | } |
93 | } |
94 | } |
95 | |
96 | impl ToTokens for FdiOptions { |
97 | fn to_tokens(&self, tokens: &mut TokenStream) { |
98 | FromDeriveInputImpl::from(self).to_tokens(tokens) |
99 | } |
100 | } |
101 | |