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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
use super::{ModuleEvent, ModuleEvents};
use crate::{attrs::partition_attributes, utils::FieldsValidator};
use anyhow::{Context, Result};
use syn::{parse_quote, punctuated::Punctuated, Field, Fields, FieldsNamed, Token};

use super::ModuleConfiguration;

/// Odra module struct.
///
/// Wraps up [syn::ItemStruct].
pub struct ModuleStruct {
    pub is_instantiable: bool,
    pub item: syn::ItemStruct,
    pub events: ModuleEvents,
    pub delegated_fields: Vec<DelegatedField>
}

pub struct DelegatedField {
    pub field: syn::Field,
    pub delegated_fields: Vec<String>
}

impl DelegatedField {
    pub(crate) fn validate(&self, fields: &syn::Fields) -> Result<(), syn::Error> {
        let fields = fields
            .iter()
            .filter_map(|f| f.ident.clone())
            .map(|i| i.to_string())
            .collect::<Vec<_>>();
        let is_valid = self.delegated_fields.iter().find(|f| !fields.contains(f));
        if let Some(invalid_ref) = is_valid {
            let error_msg = format!("Using non-existing field {}", invalid_ref);
            return Err(syn::Error::new_spanned(&self.field, error_msg));
        }

        Ok(())
    }
}

impl TryFrom<syn::Field> for DelegatedField {
    type Error = syn::Error;

    fn try_from(value: syn::Field) -> std::result::Result<Self, Self::Error> {
        let (odra_attrs, other_attrs) = partition_attributes(value.attrs.clone()).unwrap();

        let delegated_fields = odra_attrs
            .iter()
            .flat_map(|attr| attr.using())
            .collect::<Vec<_>>();

        let field_ident = value.ident.clone().unwrap().to_string();

        if delegated_fields.contains(&field_ident) {
            return Err(syn::Error::new_spanned(&value, "Self-using is not allowed"));
        }

        Ok(Self {
            field: Field {
                attrs: other_attrs,
                ..value
            },
            delegated_fields
        })
    }
}

impl ModuleStruct {
    pub fn with_config(mut self, mut config: ModuleConfiguration) -> Result<Self, syn::Error> {
        let submodules = self
            .item
            .fields
            .iter()
            .filter(|field| field.ident.is_some())
            .filter_map(filter_primitives)
            .map(|ident| ModuleEvent { ty: ident })
            .collect::<Vec<_>>();

        let mut mappings = self
            .item
            .fields
            .iter()
            .filter(|field| field.ident.is_some())
            .filter_map(|f| match &f.ty {
                syn::Type::Path(path) => extract_mapping_value_ident_from_path(path).ok(),
                _ => None
            })
            .map(|ty| ModuleEvent { ty })
            .collect::<Vec<_>>();
        mappings.dedup();

        config.events.submodules_events.extend(submodules);
        config.events.mappings_events.extend(mappings);

        self.events = config.events;

        Ok(self)
    }
}

impl TryFrom<syn::ItemStruct> for ModuleStruct {
    type Error = syn::Error;

    fn try_from(value: syn::ItemStruct) -> std::result::Result<Self, Self::Error> {
        FieldsValidator::from(&value).result()?;

        let (_, other_attrs) = partition_attributes(value.attrs).unwrap();

        let named = value
            .fields
            .clone()
            .into_iter()
            .map(|field| {
                let (_, other_attrs) = partition_attributes(field.attrs).unwrap();
                Field {
                    attrs: other_attrs,
                    ..field
                }
            })
            .collect::<Punctuated<Field, Token![,]>>();

        let fields: Fields = Fields::Named(FieldsNamed {
            brace_token: Default::default(),
            named
        });

        let delegated_fields = value
            .fields
            .into_iter()
            .map(TryInto::try_into)
            .collect::<Result<Vec<DelegatedField>, syn::Error>>()?;

        delegated_fields
            .iter()
            .try_for_each(|f| f.validate(&fields))?;

        Ok(Self {
            is_instantiable: true,
            item: syn::ItemStruct {
                attrs: other_attrs,
                fields,
                ..value
            },
            events: Default::default(),
            delegated_fields
        })
    }
}

fn extract_mapping_value_ident_from_path(path: &syn::TypePath) -> Result<syn::Type> {
    // Eg. odra::type::Mapping<String, Mapping<String, Mapping<u8, String>>>
    let mut segment = path
        .path
        .segments
        .last()
        .cloned()
        .context("At least one segment expected")?;
    if segment.ident != "Mapping" {
        return Err(anyhow::anyhow!(
            "Mapping expected but found {}",
            segment.ident
        ));
    }
    let mut result: Option<syn::Type> = None;
    loop {
        let args = &segment.arguments;
        if args.is_empty() {
            break;
        }
        if let syn::PathArguments::AngleBracketed(args) = args {
            match args
                .args
                .last()
                .context("syn::GenericArgument expected but not found")?
            {
                syn::GenericArgument::Type(syn::Type::Path(path)) => {
                    result = Some(parse_quote!(#path));
                    let path = &path.path;
                    segment = path
                        .segments
                        .last()
                        .cloned()
                        .context("At least one segment expected")?;
                }
                other => {
                    return Err(anyhow::anyhow!(
                        "syn::TypePath expected but found {:?}",
                        other
                    ))
                }
            }
        } else {
            return Err(anyhow::anyhow!(
                "syn::AngleBracketedGenericArguments expected but found {:?}",
                args
            ));
        }
    }
    Ok(result.unwrap())
}

fn filter_primitives(field: &syn::Field) -> Option<syn::Type> {
    filter_ident(field, &["Variable", "Mapping", "List", "Sequence"])
}

fn filter_ident(field: &syn::Field, exclusions: &'static [&str]) -> Option<syn::Type> {
    match &field.ty {
        syn::Type::Path(path) => {
            let path = &path.path;
            match &path.segments.last() {
                Some(segment) => {
                    if exclusions.contains(&segment.ident.to_string().as_str()) {
                        return None;
                    }
                    Some(field.ty.clone())
                }
                _ => None
            }
        }
        _ => None
    }
}

#[cfg(test)]
mod test {
    use quote::ToTokens;

    use super::*;

    #[test]
    fn test() {
        let path = syn::parse_str::<syn::TypePath>(
            "Mapping<String, Mapping<String, Mapping<u8, a::b::String>>>"
        )
        .unwrap();
        let ident = extract_mapping_value_ident_from_path(&path);
        assert_eq!(
            ident.unwrap().into_token_stream().to_string(),
            "a :: b :: String"
        );

        // Mapping expected but found String
        let path = syn::parse_str::<syn::TypePath>("String<i32, u8, u16>").unwrap();
        let ident = extract_mapping_value_ident_from_path(&path);
        assert!(ident.is_err());

        // Invalid Mapping - parenthesized arguments instead of angle bracketed
        let path = syn::parse_str::<syn::TypePath>(
            "Mapping<String, Mapping<String, Mapping(u8, String)>>"
        )
        .unwrap();
        let ident = extract_mapping_value_ident_from_path(&path);
        assert!(ident.is_err());

        // Invalid Mapping - function type instead of type
        let path = syn::parse_str::<syn::TypePath>(
            "Mapping<String, Mapping<String, Mapping<fn(usize) -> bool>>>"
        )
        .unwrap();
        let ident = extract_mapping_value_ident_from_path(&path);
        assert!(ident.is_err());
    }
}