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
use proc_macro2::Span;
use syn::{Expr, Type};
#[derive(Debug, Clone)]
pub enum Validator {
Email,
Url,
Custom {
function: String,
argument: Box<Option<CustomArgument>>,
},
MustMatch(String),
Contains(String),
Regex(String),
Range {
min: Option<ValueOrPath<f64>>,
max: Option<ValueOrPath<f64>>,
},
Length {
min: Option<ValueOrPath<u64>>,
max: Option<ValueOrPath<u64>>,
equal: Option<ValueOrPath<u64>>,
},
#[cfg(feature = "card")]
CreditCard,
#[cfg(feature = "phone")]
Phone,
Nested,
#[cfg(feature = "unic")]
NonControlCharacter,
Required,
RequiredNested,
DoesNotContain(String),
}
#[derive(Debug, Clone, PartialEq)]
pub enum ValueOrPath<T: std::fmt::Debug + Clone + PartialEq> {
Value(T),
Path(String),
}
#[derive(Debug, Clone)]
pub struct CustomArgument {
pub def_span: Span,
pub arg_type: Type,
pub arg_access: Option<Expr>,
}
impl CustomArgument {
pub fn new(def_span: Span, arg_type: Type) -> Self {
CustomArgument { def_span, arg_type, arg_access: None }
}
}
impl Validator {
pub fn code(&self) -> &'static str {
match *self {
Validator::MustMatch(_) => "must_match",
Validator::Email => "email",
Validator::Url => "url",
Validator::Custom { .. } => "custom",
Validator::Contains(_) => "contains",
Validator::Regex(_) => "regex",
Validator::Range { .. } => "range",
Validator::Length { .. } => "length",
#[cfg(feature = "card")]
Validator::CreditCard => "credit_card",
#[cfg(feature = "phone")]
Validator::Phone => "phone",
Validator::Nested => "nested",
#[cfg(feature = "unic")]
Validator::NonControlCharacter => "non_control_character",
Validator::Required => "required",
Validator::RequiredNested => "required_nested",
Validator::DoesNotContain(_) => "does_not_contain",
}
}
pub fn get_custom_argument(&self) -> Option<&CustomArgument> {
match self {
Validator::Custom { argument, .. } => (**argument).as_ref(),
_ => None,
}
}
pub fn get_custom_argument_mut(&mut self) -> Option<&mut CustomArgument> {
match self {
Validator::Custom { argument, .. } => (**argument).as_mut(),
_ => None,
}
}
pub fn has_custom_argument(&self) -> bool {
self.get_custom_argument().is_some()
}
}