1use syn::{Ident, Path};
23use crate::{Error, FromField, FromMeta};
45use super::ParseAttribute;
67/// A forwarded field and attributes that influence its behavior.
8#[derive(Debug, Clone)]
9pub struct ForwardedField {
10/// The ident of the field that will receive the forwarded value.
11pub 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`.
14pub with: Option<Path>,
15}
1617impl FromField for ForwardedField {
18fn from_field(field: &syn::Field) -> crate::Result<Self> {
19let result = Self {
20 ident: field.ident.clone().ok_or_else(|| {
21 Error::custom("forwarded field must be named field").with_span(field)
22 })?,
23 with: None,
24 };
2526 result.parse_attributes(&field.attrs)
27 }
28}
2930impl ParseAttribute for ForwardedField {
31fn parse_nested(&mut self, mi: &syn::Meta) -> crate::Result<()> {
32if mi.path().is_ident("with") {
33if self.with.is_some() {
34return Err(Error::duplicate_field_path(mi.path()).with_span(mi));
35 }
3637self.with = FromMeta::from_meta(mi)?;
38Ok(())
39 } else {
40Err(Error::unknown_field_path_with_alts(mi.path(), &["with"]).with_span(mi))
41 }
42 }
43}