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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
use crate::{
errors::Error,
param_types::{EnumVariants, ParamType},
utils::{has_array_format, has_tuple_format},
Property, TypeDeclaration,
};
use std::str::FromStr;
use std::{collections::HashMap, convert::TryFrom};
impl TryFrom<&Property> for ParamType {
type Error = Error;
fn try_from(prop: &Property) -> Result<Self, Self::Error> {
match ParamType::from_str(&prop.type_field) {
Ok(param_type) => Ok(param_type),
Err(_) => {
if prop.type_field == "()" {
return Ok(ParamType::Unit);
}
if prop.type_field.starts_with('[') && prop.type_field.ends_with(']') {
return ParamType::parse_array_param(prop);
}
if prop.type_field.contains("str[") {
return ParamType::parse_string_param(prop);
}
if prop.type_field.starts_with('(') && prop.type_field.ends_with(')') {
return ParamType::parse_tuple_param(prop);
}
ParamType::parse_custom_type_param(prop)
}
}
}
}
impl ParamType {
pub fn from_type_declaration(
prop: &TypeDeclaration,
types: &HashMap<usize, TypeDeclaration>,
) -> Result<Self, Error> {
match ParamType::from_str(&prop.type_field) {
Ok(param_type) => Ok(param_type),
Err(_) => {
if prop.type_field == "()" {
return Ok(ParamType::Unit);
}
if has_array_format(&prop.type_field) {
return ParamType::_new_parse_array_param(prop, types);
}
if prop.type_field.contains("str[") {
return ParamType::_new_parse_string_param(prop);
}
if has_tuple_format(&prop.type_field) {
return ParamType::_new_parse_tuple_param(prop, types);
}
ParamType::_new_parse_custom_type_param(prop, types)
}
}
}
pub fn parse_tuple_param(prop: &Property) -> Result<Self, Error> {
let mut params: Vec<Self> = Vec::new();
for tuple_component in prop
.components
.as_ref()
.expect("tuples should have components")
{
params.push(Self::try_from(tuple_component)?);
}
Ok(ParamType::Tuple(params))
}
pub fn _new_parse_tuple_param(
prop: &TypeDeclaration,
types: &HashMap<usize, TypeDeclaration>,
) -> Result<Self, Error> {
let mut params: Vec<Self> = Vec::new();
for tuple_component in prop
.components
.as_ref()
.expect("tuples should have components")
{
let tuple_component_type_declaration = types.get(&tuple_component.type_field).unwrap();
params.push(Self::from_type_declaration(
tuple_component_type_declaration,
types,
)?);
}
Ok(ParamType::Tuple(params))
}
pub fn parse_string_param(prop: &Property) -> Result<Self, Error> {
let split: Vec<&str> = prop.type_field.split('[').collect();
if split.len() != 2 || !split[0].eq("str") {
return Err(Error::InvalidType(format!(
"Expected parameter type `str[n]`, found `{}`",
prop.type_field
)));
}
let size: usize = split[1][..split[1].len() - 1].parse()?;
Ok(ParamType::String(size))
}
pub fn _new_parse_string_param(prop: &TypeDeclaration) -> Result<Self, Error> {
let split: Vec<&str> = prop.type_field.split('[').collect();
if split.len() != 2 || !split[0].eq("str") {
return Err(Error::InvalidType(format!(
"Expected parameter type `str[n]`, found `{}`",
prop.type_field
)));
}
let size: usize = split[1][..split[1].len() - 1].parse()?;
Ok(ParamType::String(size))
}
pub fn parse_array_param(prop: &Property) -> Result<ParamType, Error> {
let split: Vec<&str> = prop.type_field.split("; ").collect();
if split.len() != 2 {
return Err(Error::InvalidType(format!(
"Expected parameter type `[T; n]`, found `{}`",
prop.type_field
)));
}
let (type_field, size) = (split[0], split[1]);
let type_field = type_field[1..].to_string();
let param_type = match Self::from_str(&type_field) {
Ok(param_type) => param_type,
Err(_) => {
let component = prop
.components
.as_ref()
.expect("array should have components")
.first()
.expect("components in array should have at least one component");
if type_field.contains("str[") {
ParamType::parse_string_param(component)?
} else {
ParamType::parse_custom_type_param(component)?
}
}
};
let size: usize = size[..size.len() - 1].parse()?;
Ok(ParamType::Array(Box::new(param_type), size))
}
pub fn _new_parse_array_param(
prop: &TypeDeclaration,
types: &HashMap<usize, TypeDeclaration>,
) -> Result<ParamType, Error> {
let split: Vec<&str> = prop.type_field.split("; ").collect();
if split.len() != 2 {
return Err(Error::InvalidType(format!(
"Expected parameter type `[T; n]`, found `{}`",
prop.type_field
)));
}
let (_type_field, size) = (split[0], split[1]);
let t = if let Some([component]) = prop.components.as_deref() {
types
.get(&component.type_field)
.expect("couldn't find type declaration for array component")
} else {
panic!("array should have components");
};
let type_field = t.type_field.clone();
let param_type = match Self::from_str(&type_field) {
Ok(param_type) => param_type,
Err(_) => {
if type_field.contains("str[") {
ParamType::_new_parse_string_param(t)?
} else {
ParamType::_new_parse_custom_type_param(t, types)?
}
}
};
let size: usize = size[..size.len() - 1].parse()?;
Ok(ParamType::Array(Box::new(param_type), size))
}
pub fn parse_custom_type_param(prop: &Property) -> Result<ParamType, Error> {
let mut params: Vec<ParamType> = vec![];
match &prop.components {
Some(c) => {
for component in c {
params.push(Self::try_from(component)?)
}
if prop.is_struct_type() {
return Ok(ParamType::Struct(params));
}
if prop.is_enum_type() {
return Ok(ParamType::Enum(EnumVariants::new(params)?));
}
Err(Error::InvalidType(prop.type_field.clone()))
}
None => Err(Error::InvalidType(
"cannot parse custom type with no components".into(),
)),
}
}
pub fn _new_parse_custom_type_param(
prop: &TypeDeclaration,
types: &HashMap<usize, TypeDeclaration>,
) -> Result<ParamType, Error> {
match &prop.components {
Some(c) => {
let params = c
.iter()
.map(|component| {
let component_type_declaration = types.get(&component.type_field).unwrap();
Self::from_type_declaration(component_type_declaration, types).unwrap()
})
.collect();
if prop.is_struct_type() {
return Ok(ParamType::Struct(params));
}
if prop.is_enum_type() {
return Ok(ParamType::Enum(EnumVariants::new(params)?));
}
Err(Error::InvalidType(prop.type_field.clone()))
}
None => Err(Error::InvalidType(
"cannot parse custom type with no components".into(),
)),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_string_and_array_param() -> Result<(), Error> {
let array_prop = Property {
name: "some_array".to_string(),
type_field: "[bool; 4]".to_string(),
components: None,
};
let expected = "Array(Box::new(ParamType::Bool),4)";
let result = ParamType::parse_array_param(&array_prop)?.to_string();
assert_eq!(result, expected);
let string_prop = Property {
name: "some_array".to_string(),
type_field: "str[5]".to_string(),
components: None,
};
let expected = "String(5)";
let result = ParamType::parse_string_param(&string_prop)?.to_string();
assert_eq!(result, expected);
let expected = "Invalid type: Expected parameter type `str[n]`, found `[bool; 4]`";
let result = ParamType::parse_string_param(&array_prop)
.unwrap_err()
.to_string();
assert_eq!(result, expected);
let expected = "Invalid type: Expected parameter type `[T; n]`, found `str[5]`";
let result = ParamType::parse_array_param(&string_prop)
.unwrap_err()
.to_string();
assert_eq!(result, expected);
Ok(())
}
#[test]
fn test_parse_custom_type_params() -> Result<(), Error> {
let components = vec![
Property {
name: "vodka".to_string(),
type_field: "u64".to_string(),
components: None,
},
Property {
name: "redbull".to_string(),
type_field: "bool".to_string(),
components: None,
},
];
let some_struct = Property {
name: String::from("something_you_drink"),
type_field: String::from("struct Cocktail"),
components: Some(components.clone()),
};
let struct_result = ParamType::parse_custom_type_param(&some_struct)?;
let expected = ParamType::Struct(vec![ParamType::U64, ParamType::Bool]);
assert_eq!(struct_result, expected);
let expected_string = "Struct(vec![ParamType::U64,ParamType::Bool])";
assert_eq!(struct_result.to_string(), expected_string);
let some_enum = Property {
name: String::from("something_you_drink"),
type_field: String::from("enum Cocktail"),
components: Some(components),
};
let enum_result = ParamType::parse_custom_type_param(&some_enum)?;
let expected = ParamType::Enum(EnumVariants::new(vec![ParamType::U64, ParamType::Bool])?);
assert_eq!(enum_result, expected);
let expected_string =
"Enum(EnumVariants::new(vec![ParamType::U64,ParamType::Bool]).unwrap())";
assert_eq!(enum_result.to_string(), expected_string);
Ok(())
}
}