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
263
264
265
266
267
268
269
270
271
272
273
use serde::{Deserialize, Serialize};
use std::{
    collections::{HashMap, HashSet},
    hash::{Hash, Hasher},
};

fn is_false(value: &bool) -> bool {
    !value
}

#[derive(Debug, Serialize, Deserialize)]
pub struct IgniteTypeDefinition {
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub namespace: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub generic_args: Vec<String>,
    pub variant: IgniteTypeVariant,
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub meta: HashMap<String, IgniteAttribMeta>,
    #[serde(default, skip_serializing_if = "is_false")]
    pub is_proxy: bool,
}

impl IgniteTypeDefinition {
    pub fn name(&self) -> String {
        self.variant.name()
    }

    pub fn referenced(&self) -> HashSet<String> {
        self.variant.referenced()
    }
}

impl Hash for IgniteTypeDefinition {
    fn hash<H>(&self, state: &mut H)
    where
        H: Hasher,
    {
        self.namespace.hash(state);
        self.generic_args.hash(state);
        self.variant.hash(state);
    }
}

#[derive(Debug, Serialize, Deserialize, Hash)]
pub enum IgniteTypeVariant {
    StructUnit(String),
    StructNamed(IgniteNamed),
    StructUnnamed(IgniteUnnamed),
    Enum(IgniteEnum),
}

impl IgniteTypeVariant {
    pub fn name(&self) -> String {
        match self {
            Self::StructUnit(name) => name.clone(),
            Self::StructNamed(value) => value.name.clone(),
            Self::StructUnnamed(value) => value.name.clone(),
            Self::Enum(value) => value.name.clone(),
        }
    }

    pub fn referenced(&self) -> HashSet<String> {
        match self {
            Self::StructUnit(_) => HashSet::new(),
            Self::StructNamed(value) => value.referenced(),
            Self::StructUnnamed(value) => value.referenced(),
            Self::Enum(value) => value.referenced(),
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Hash)]
pub struct IgniteNamed {
    pub name: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub fields: Vec<IgniteNamedField>,
}

impl IgniteNamed {
    pub fn referenced(&self) -> HashSet<String> {
        self.fields
            .iter()
            .flat_map(|field| field.referenced())
            .collect()
    }
}

#[derive(Debug, Serialize, Deserialize, Hash)]
pub struct IgniteUnnamed {
    pub name: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub fields: Vec<IgniteUnnamedField>,
}

impl IgniteUnnamed {
    pub fn referenced(&self) -> HashSet<String> {
        self.fields
            .iter()
            .flat_map(|field| field.referenced())
            .collect()
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct IgniteNamedField {
    pub name: String,
    pub typename: IgniteType,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mapping: Option<String>,
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub meta: HashMap<String, IgniteAttribMeta>,
}

impl IgniteNamedField {
    pub fn referenced(&self) -> HashSet<String> {
        if let Some(mapping) = &self.mapping {
            let mut result = HashSet::new();
            if let Some(mapping) = mapping.split('.').last() {
                result.insert(mapping.to_owned());
            }
            result
        } else {
            self.typename.referenced()
        }
    }
}

impl Hash for IgniteNamedField {
    fn hash<H>(&self, state: &mut H)
    where
        H: Hasher,
    {
        self.name.hash(state);
        self.typename.hash(state);
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct IgniteUnnamedField {
    pub typename: IgniteType,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mapping: Option<String>,
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub meta: HashMap<String, IgniteAttribMeta>,
}

impl IgniteUnnamedField {
    pub fn referenced(&self) -> HashSet<String> {
        if let Some(mapping) = &self.mapping {
            let mut result = HashSet::new();
            if let Some(mapping) = mapping.split('.').last() {
                result.insert(mapping.to_owned());
            }
            result
        } else {
            self.typename.referenced()
        }
    }
}

impl Hash for IgniteUnnamedField {
    fn hash<H>(&self, state: &mut H)
    where
        H: Hasher,
    {
        self.typename.hash(state);
    }
}

#[derive(Debug, Serialize, Deserialize, Hash)]
pub struct IgniteEnum {
    pub name: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub variants: Vec<IgniteVariant>,
}

impl IgniteEnum {
    pub fn referenced(&self) -> HashSet<String> {
        self.variants
            .iter()
            .flat_map(|variant| variant.referenced())
            .collect()
    }
}

#[derive(Debug, Serialize, Deserialize, Hash)]
pub enum IgniteVariant {
    Unit(String),
    Named(IgniteNamed),
    Unnamed(IgniteUnnamed),
}

impl IgniteVariant {
    pub fn referenced(&self) -> HashSet<String> {
        match self {
            Self::Unit(_) => HashSet::new(),
            Self::Named(value) => value.referenced(),
            Self::Unnamed(value) => value.referenced(),
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Hash)]
pub enum IgniteType {
    Unit,
    Atom(String),
    Tuple(Vec<IgniteType>),
    Array(IgniteTypeArray),
    Generic(IgniteTypeGeneric),
}

impl IgniteType {
    pub fn referenced(&self) -> HashSet<String> {
        match self {
            Self::Unit => HashSet::new(),
            Self::Atom(name) => {
                let mut result = HashSet::new();
                result.insert(name.clone());
                result
            }
            Self::Tuple(value) => value.iter().flat_map(|item| item.referenced()).collect(),
            Self::Array(value) => value.referenced(),
            Self::Generic(value) => value.referenced(),
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Hash)]
pub struct IgniteTypeArray {
    pub typename: Box<IgniteType>,
    pub size: usize,
}

impl IgniteTypeArray {
    pub fn referenced(&self) -> HashSet<String> {
        self.typename.referenced()
    }
}

#[derive(Debug, Serialize, Deserialize, Hash)]
pub struct IgniteTypeGeneric {
    pub name: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub arguments: Vec<IgniteType>,
}

impl IgniteTypeGeneric {
    pub fn referenced(&self) -> HashSet<String> {
        std::iter::once(self.name.clone())
            .chain(self.arguments.iter().flat_map(|arg| arg.referenced()))
            .collect()
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub enum IgniteAttribMeta {
    None,
    Bool(bool),
    String(String),
    Integer(i64),
    Float(f64),
}

impl Default for IgniteAttribMeta {
    fn default() -> Self {
        Self::None
    }
}

pub trait Ignite {
    fn generate_type_definition() -> IgniteTypeDefinition;
}