educe/trait_handlers/eq/models/
field_attribute.rs

1use syn::{punctuated::Punctuated, Attribute, Meta, Token};
2
3use crate::{panic, supported_traits::Trait};
4
5pub(crate) struct FieldAttribute;
6
7pub(crate) struct FieldAttributeBuilder;
8
9impl FieldAttributeBuilder {
10    pub(crate) fn build_from_eq_meta(&self, meta: &Meta) -> syn::Result<FieldAttribute> {
11        debug_assert!(meta.path().is_ident("Eq"));
12
13        return Err(panic::attribute_incorrect_place(meta.path().get_ident().unwrap()));
14    }
15
16    pub(crate) fn build_from_attributes(
17        &self,
18        attributes: &[Attribute],
19        traits: &[Trait],
20    ) -> syn::Result<FieldAttribute> {
21        let mut output = None;
22
23        for attribute in attributes.iter() {
24            let path = attribute.path();
25
26            if path.is_ident("educe") {
27                if let Meta::List(list) = &attribute.meta {
28                    let result =
29                        list.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated)?;
30
31                    for meta in result {
32                        let path = meta.path();
33
34                        let t = match Trait::from_path(path) {
35                            Some(t) => t,
36                            None => return Err(panic::unsupported_trait(meta.path())),
37                        };
38
39                        if !traits.contains(&t) {
40                            return Err(panic::trait_not_used(path.get_ident().unwrap()));
41                        }
42
43                        if t == Trait::Eq {
44                            if output.is_some() {
45                                return Err(panic::reuse_a_trait(path.get_ident().unwrap()));
46                            }
47
48                            output = Some(self.build_from_eq_meta(&meta)?);
49                        }
50                    }
51                }
52            }
53        }
54
55        Ok(output.unwrap_or(FieldAttribute))
56    }
57}