1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use std::collections::HashMap;

use syn::{punctuated::Punctuated, Attribute, Meta, Path, Token};

use crate::{
    common::{path::meta_2_path, r#type::TypeWithPunctuatedMeta, tools::HashType},
    panic, Trait,
};

pub(crate) struct FieldAttribute {
    pub(crate) types: HashMap<HashType, Option<Path>>,
}

#[derive(Debug)]
pub(crate) struct FieldAttributeBuilder {
    pub(crate) enable_types: bool,
}

impl FieldAttributeBuilder {
    pub(crate) fn build_from_into_meta(&self, meta: &[Meta]) -> syn::Result<FieldAttribute> {
        debug_assert!(!meta.is_empty());

        let mut types = HashMap::new();

        for meta in meta {
            debug_assert!(meta.path().is_ident("Into"));

            let correct_usage_for_into_attribute = {
                let mut usage = vec![];

                if self.enable_types {
                    usage.push(stringify!(#[educe(Into(type))]));
                    usage.push(stringify!(#[educe(Into(type, method(path_to_method)))]));
                }

                usage
            };

            match meta {
                Meta::Path(_) | Meta::NameValue(_) => {
                    return Err(panic::attribute_incorrect_format(
                        meta.path().get_ident().unwrap(),
                        &correct_usage_for_into_attribute,
                    ));
                },
                Meta::List(list) => {
                    if !self.enable_types {
                        return Err(panic::attribute_incorrect_format(
                            meta.path().get_ident().unwrap(),
                            &correct_usage_for_into_attribute,
                        ));
                    }

                    let TypeWithPunctuatedMeta {
                        ty,
                        list: result,
                    } = list.parse_args()?;

                    let ty = super::super::common::to_hash_type(&ty);

                    let mut method = None;
                    let mut method_is_set = false;

                    let mut handler = |meta: Meta| -> syn::Result<bool> {
                        if let Some(ident) = meta.path().get_ident() {
                            if ident == "method" {
                                let v = meta_2_path(&meta)?;

                                if method_is_set {
                                    return Err(panic::parameter_reset(ident));
                                }

                                method_is_set = true;

                                method = Some(v);

                                return Ok(true);
                            }
                        }

                        Ok(false)
                    };

                    for p in result {
                        if !handler(p)? {
                            return Err(panic::attribute_incorrect_format(
                                meta.path().get_ident().unwrap(),
                                &correct_usage_for_into_attribute,
                            ));
                        }
                    }

                    if types.contains_key(&ty) {
                        return Err(super::super::panic::reset_a_type(&ty));
                    }

                    types.insert(ty, method);
                },
            }
        }

        Ok(FieldAttribute {
            types,
        })
    }

    pub(crate) fn build_from_attributes(
        &self,
        attributes: &[Attribute],
        traits: &[Trait],
    ) -> syn::Result<FieldAttribute> {
        let mut output: Option<FieldAttribute> = None;

        let mut v_meta = Vec::new();

        for attribute in attributes.iter() {
            let path = attribute.path();

            if path.is_ident("educe") {
                if let Meta::List(list) = &attribute.meta {
                    let result =
                        list.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated)?;

                    for meta in result {
                        let path = meta.path();

                        let t = match Trait::from_path(path) {
                            Some(t) => t,
                            None => return Err(panic::unsupported_trait(meta.path())),
                        };

                        if !traits.contains(&t) {
                            return Err(panic::trait_not_used(path.get_ident().unwrap()));
                        }

                        if t == Trait::Into {
                            v_meta.push(meta);
                        }
                    }
                }
            }
        }

        if !v_meta.is_empty() {
            output = Some(self.build_from_into_meta(&v_meta)?);
        }

        Ok(output.unwrap_or_else(|| FieldAttribute {
            types: HashMap::new()
        }))
    }
}