1 | use syn::spanned::Spanned; |
2 | use syn::{Field, Ident, Meta}; |
3 | |
4 | use crate::ast::Data; |
5 | use crate::codegen::ForwardAttrs; |
6 | use crate::options::{ |
7 | Core, DefaultExpression, ForwardAttrsFilter, ForwardedField, ParseAttribute, ParseData, |
8 | }; |
9 | use crate::util::PathList; |
10 | use crate::{Error, FromField, FromMeta, Result}; |
11 | |
12 | /// Reusable base for `FromDeriveInput`, `FromVariant`, `FromField`, and other top-level |
13 | /// `From*` traits. |
14 | #[derive (Debug, Clone)] |
15 | pub struct OuterFrom { |
16 | /// The field on the target struct which should receive the type identifier, if any. |
17 | pub ident: Option<Ident>, |
18 | |
19 | /// The field on the target struct which should receive the type attributes, if any. |
20 | pub attrs: Option<ForwardedField>, |
21 | |
22 | pub container: Core, |
23 | |
24 | /// The attribute names that should be searched. |
25 | pub attr_names: PathList, |
26 | |
27 | /// The attribute names that should be forwarded. The presence of the word with no additional |
28 | /// filtering will cause _all_ attributes to be cloned and exposed to the struct after parsing. |
29 | pub forward_attrs: Option<ForwardAttrsFilter>, |
30 | |
31 | /// Whether or not the container can be made through conversion from the type `Ident`. |
32 | pub from_ident: bool, |
33 | } |
34 | |
35 | impl OuterFrom { |
36 | pub fn start(di: &syn::DeriveInput) -> Result<Self> { |
37 | Ok(OuterFrom { |
38 | container: Core::start(di)?, |
39 | attrs: Default::default(), |
40 | ident: Default::default(), |
41 | attr_names: Default::default(), |
42 | forward_attrs: Default::default(), |
43 | from_ident: Default::default(), |
44 | }) |
45 | } |
46 | |
47 | pub fn as_forward_attrs(&self) -> ForwardAttrs<'_> { |
48 | ForwardAttrs { |
49 | field: self.attrs.as_ref(), |
50 | filter: self.forward_attrs.as_ref(), |
51 | } |
52 | } |
53 | } |
54 | |
55 | impl ParseAttribute for OuterFrom { |
56 | fn parse_nested(&mut self, mi: &Meta) -> Result<()> { |
57 | let path: &Path = mi.path(); |
58 | if path.is_ident("attributes" ) { |
59 | self.attr_names = FromMeta::from_meta(item:mi)?; |
60 | } else if path.is_ident("forward_attrs" ) { |
61 | self.forward_attrs = FromMeta::from_meta(item:mi)?; |
62 | } else if path.is_ident("from_ident" ) { |
63 | // HACK: Declaring that a default is present will cause fields to |
64 | // generate correct code, but control flow isn't that obvious. |
65 | self.container.default = Some(DefaultExpression::Trait { |
66 | // Use the span of the `from_ident` keyword so that errors in generated code |
67 | // caused by this will point back to the correct location. |
68 | span: path.span(), |
69 | }); |
70 | self.from_ident = true; |
71 | } else { |
72 | return self.container.parse_nested(mi); |
73 | } |
74 | Ok(()) |
75 | } |
76 | } |
77 | |
78 | impl ParseData for OuterFrom { |
79 | fn parse_field(&mut self, field: &Field) -> Result<()> { |
80 | match field.ident.as_ref().map(|v| v.to_string()).as_deref() { |
81 | Some("ident" ) => { |
82 | self.ident.clone_from(&field.ident); |
83 | Ok(()) |
84 | } |
85 | Some("attrs" ) => { |
86 | self.attrs = ForwardedField::from_field(field).map(Some)?; |
87 | Ok(()) |
88 | } |
89 | _ => self.container.parse_field(field), |
90 | } |
91 | } |
92 | |
93 | fn validate_body(&self, errors: &mut crate::error::Accumulator) { |
94 | self.container.validate_body(errors); |
95 | if let Some(attrs) = &self.attrs { |
96 | if self.forward_attrs.is_none() { |
97 | let container_name = match &self.container.data { |
98 | Data::Enum(_) => "enum" , |
99 | Data::Struct(_) => "struct" , |
100 | }; |
101 | errors.push( |
102 | Error::custom(format!( |
103 | "field will not be populated because `forward_attrs` is not set on the {}" , |
104 | container_name |
105 | )) |
106 | .with_span(&attrs.ident), |
107 | ); |
108 | } |
109 | } |
110 | } |
111 | } |
112 | |