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
pub mod expression;
mod locales;
use crate::{
backends::expressions_from_files::arguments::{expression::Expression, locales::Locales},
utils::{Argument, Input},
};
use proc_macro2::Span;
use syn::{parse_str, spanned::Spanned, Error, Path};
pub struct Arguments {
pub locales: Locales,
pub expressions: Vec<Expression>,
pub path: Option<Path>,
}
impl Arguments {
pub fn new(input: Input) -> Result<Self, Error> {
let mut locales = Locales::new();
let mut expressions = Vec::new();
let mut path = None;
for argument in input.into_iter() {
match argument {
Argument::Mapped { .. } => {
return Err(Error::new(
Span::call_site(),
"Expected only named arguments",
));
}
Argument::Named { name, value } => match name.to_string().as_str() {
"locales" => {
for locale in Input::parse_array("locales", &value, Input::parse_tuple)? {
let name = Input::parse_ident(
"locale name",
locale
.get(0)
.ok_or(Error::new(value.span(), "Expected locale name"))?,
)?;
let path = Input::parse_path(
"locale path",
locale
.get(1)
.ok_or(Error::new(value.span(), "Expected locale path"))?,
)?;
locales.add(name, path);
}
}
"expressions" => {
for expression in
Input::parse_array("expressions", &value, Input::parse_tuple)?
{
let name = Input::parse_ident(
"expression name",
expression
.get(0)
.ok_or(Error::new(value.span(), "Expected expression name"))?,
)?;
let r#type = Input::parse_optional(
"expression type",
expression.get(1),
parse_str("&'static str")?,
Input::parse_type,
)?;
expressions.push(Expression { name, r#type });
}
}
"path" => path = Some(Input::parse_path("path", &value)?),
_ => {
return Err(Error::new(
value.span(),
format!("Unknown parameter `{name}`"),
));
}
},
}
}
Ok(Self {
locales,
expressions,
path,
})
}
}