darling_core\options/
forward_attrs.rs1use proc_macro2::Ident;
2use syn::Path;
3
4use crate::ast::NestedMeta;
5use crate::util::PathList;
6use crate::{Error, FromField, FromMeta, Result};
7
8use super::ParseAttribute;
9
10#[derive(Debug, Clone)]
12pub struct AttrsField {
13 pub ident: Ident,
15 pub with: Option<Path>,
18}
19
20impl FromField for AttrsField {
21 fn from_field(field: &syn::Field) -> crate::Result<Self> {
22 let result = Self {
23 ident: field.ident.clone().ok_or_else(|| {
24 Error::custom("attributes receiver must be named field").with_span(field)
25 })?,
26 with: None,
27 };
28
29 result.parse_attributes(&field.attrs)
30 }
31}
32
33impl ParseAttribute for AttrsField {
34 fn parse_nested(&mut self, mi: &syn::Meta) -> crate::Result<()> {
35 if mi.path().is_ident("with") {
36 if self.with.is_some() {
37 return Err(Error::duplicate_field_path(mi.path()).with_span(mi));
38 }
39
40 self.with = FromMeta::from_meta(mi)?;
41 Ok(())
42 } else {
43 Err(Error::unknown_field_path_with_alts(mi.path(), &["with"]).with_span(mi))
44 }
45 }
46}
47
48#[derive(Debug, Clone, PartialEq, Eq)]
50pub enum ForwardAttrsFilter {
51 All,
52 Only(PathList),
53}
54
55impl ForwardAttrsFilter {
56 pub fn is_empty(&self) -> bool {
58 match *self {
59 ForwardAttrsFilter::All => false,
60 ForwardAttrsFilter::Only(ref list) => list.is_empty(),
61 }
62 }
63}
64
65impl FromMeta for ForwardAttrsFilter {
66 fn from_word() -> Result<Self> {
67 Ok(ForwardAttrsFilter::All)
68 }
69
70 fn from_list(nested: &[NestedMeta]) -> Result<Self> {
71 Ok(ForwardAttrsFilter::Only(PathList::from_list(nested)?))
72 }
73}