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
use crate::fields::FieldInfo;
use quote::quote;
#[derive(Debug, PartialEq, Eq)]
pub enum Modifier {
Trim,
Uppercase,
Lowercase,
Capitalize,
Custom { function: syn::Path },
Nested,
}
impl Modifier {
/// Returns direct modification tokens as the first element and any nested validify tokens as the second element.
/// Necessary because we need both in case a nested validify occurs. In that case, the first element will have the
/// necessary modification tokens for nested elements in the `Modify` impl while the second will have the tokens
/// for the `Validify` impl.
pub fn to_validify_tokens(&self, field_info: &FieldInfo) -> proc_macro2::TokenStream {
let param = field_info.modifier_param_tokens();
match self {
Modifier::Trim => {
let tokens = if field_info.ident_override.is_some() || field_info.is_option() {
quote!(
*#param = #param.trim().to_string();
)
} else {
quote!(
#param = #param.trim().to_string();
)
};
field_info.wrap_modifier_if_option(
field_info.wrap_modifier_if_collection(param, tokens, self),
)
}
Modifier::Uppercase => {
let tokens = if field_info.ident_override.is_some() || field_info.is_option() {
quote!(
*#param = #param.to_uppercase();
)
} else {
quote!(
#param = #param.to_uppercase();
)
};
field_info.wrap_modifier_if_option(
field_info.wrap_modifier_if_collection(param, tokens, self),
)
}
Modifier::Lowercase => {
let tokens = if field_info.ident_override.is_some() || field_info.is_option() {
quote!(
*#param = #param.to_lowercase();
)
} else {
quote!(
#param = #param.to_lowercase();
)
};
field_info.wrap_modifier_if_option(
field_info.wrap_modifier_if_collection(param, tokens, self),
)
}
Modifier::Capitalize => {
let tokens = if field_info.ident_override.is_some() || field_info.is_option() {
quote!(
*#param = ::std::format!("{}{}", &#param[0..1].to_uppercase(), &#param[1..]);
)
} else {
quote!(
#param = ::std::format!("{}{}", &#param[0..1].to_uppercase(), &#param[1..]);
)
};
field_info.wrap_modifier_if_option(
field_info.wrap_modifier_if_collection(param, tokens, self),
)
}
Modifier::Custom { function } => {
let tokens = if field_info.ident_override.is_some() || field_info.is_option() {
quote!(
#function(#param);
)
} else {
quote!(
#function(&mut #param);
)
};
field_info.wrap_modifier_if_option(tokens)
}
Modifier::Nested => {
let modifications = if field_info.is_list() {
quote!(
for el in #param.iter_mut() {
el.modify();
}
)
} else {
quote!(#param.modify();)
};
field_info.wrap_modifier_if_option(modifications)
}
}
}
}