1 | use syn::{Ident, Path}; |
2 | |
3 | use crate::{Error, FromField, FromMeta}; |
4 | |
5 | use super::ParseAttribute; |
6 | |
7 | /// A forwarded field and attributes that influence its behavior. |
8 | #[derive (Debug, Clone)] |
9 | pub struct ForwardedField { |
10 | /// The ident of the field that will receive the forwarded value. |
11 | pub ident: Ident, |
12 | /// Path of the function that will be called to convert the forwarded value |
13 | /// into the type expected by the field in `ident`. |
14 | pub with: Option<Path>, |
15 | } |
16 | |
17 | impl FromField for ForwardedField { |
18 | fn from_field(field: &syn::Field) -> crate::Result<Self> { |
19 | let result: ForwardedField = Self { |
20 | ident: field.ident.clone().ok_or_else(|| { |
21 | Error::custom("forwarded field must be named field" ).with_span(node:field) |
22 | })?, |
23 | with: None, |
24 | }; |
25 | |
26 | result.parse_attributes(&field.attrs) |
27 | } |
28 | } |
29 | |
30 | impl ParseAttribute for ForwardedField { |
31 | fn parse_nested(&mut self, mi: &syn::Meta) -> crate::Result<()> { |
32 | if mi.path().is_ident("with" ) { |
33 | if self.with.is_some() { |
34 | return Err(Error::duplicate_field_path(mi.path()).with_span(node:mi)); |
35 | } |
36 | |
37 | self.with = FromMeta::from_meta(item:mi)?; |
38 | Ok(()) |
39 | } else { |
40 | Err(Error::unknown_field_path_with_alts(mi.path(), &["with" ]).with_span(node:mi)) |
41 | } |
42 | } |
43 | } |
44 | |