validators_options/
lib.rs1#![no_std]
8
9#[macro_use]
10extern crate educe;
11
12#[macro_use]
13extern crate enum_ordinalize;
14
15#[derive(Debug, Copy, Clone, PartialEq, Eq, Educe, Ordinalize)]
17#[educe(Default)]
18pub enum ValidatorOption {
19 Must,
20 #[educe(Default)]
21 Allow,
22 NotAllow,
23}
24
25impl ValidatorOption {
26 #[doc(hidden)]
27 #[inline]
28 pub const fn new() -> ValidatorOption {
29 ValidatorOption::Allow
30 }
31
32 #[inline]
33 pub fn allow(&self) -> bool {
34 match self {
35 ValidatorOption::Must => true,
36 ValidatorOption::Allow => true,
37 ValidatorOption::NotAllow => false,
38 }
39 }
40
41 #[inline]
42 pub fn not_allow(&self) -> bool {
43 match self {
44 ValidatorOption::Must => false,
45 ValidatorOption::Allow => false,
46 ValidatorOption::NotAllow => true,
47 }
48 }
49
50 #[inline]
51 pub fn must(&self) -> bool {
52 match self {
53 ValidatorOption::Must => true,
54 ValidatorOption::Allow => false,
55 ValidatorOption::NotAllow => false,
56 }
57 }
58}
59
60#[derive(Debug, Copy, Clone, PartialEq, Eq, Educe, Ordinalize)]
62#[educe(Default)]
63pub enum ValidatorCaseOption {
64 #[educe(Default)]
65 Any,
66 Upper,
67 Lower,
68}
69
70impl ValidatorCaseOption {
71 #[doc(hidden)]
72 #[inline]
73 pub const fn new() -> ValidatorCaseOption {
74 ValidatorCaseOption::Any
75 }
76
77 #[inline]
78 pub fn any(&self) -> bool {
79 match self {
80 ValidatorCaseOption::Any => true,
81 ValidatorCaseOption::Upper => false,
82 ValidatorCaseOption::Lower => false,
83 }
84 }
85
86 #[inline]
87 pub fn upper(&self) -> bool {
88 match self {
89 ValidatorCaseOption::Any => true,
90 ValidatorCaseOption::Upper => true,
91 ValidatorCaseOption::Lower => false,
92 }
93 }
94
95 #[inline]
96 pub fn lower(&self) -> bool {
97 match self {
98 ValidatorCaseOption::Any => true,
99 ValidatorCaseOption::Upper => false,
100 ValidatorCaseOption::Lower => true,
101 }
102 }
103}
104
105#[derive(Debug, Copy, Clone, PartialEq, Eq)]
107pub enum ValidatorSeparatorOption {
108 Must(u8),
109 Allow(u8),
110 NotAllow,
111}
112
113impl ValidatorSeparatorOption {
114 #[inline]
115 pub fn allow(&self) -> Option<u8> {
116 match self {
117 ValidatorSeparatorOption::Must(e) => Some(*e),
118 ValidatorSeparatorOption::Allow(e) => Some(*e),
119 ValidatorSeparatorOption::NotAllow => None,
120 }
121 }
122
123 #[inline]
124 pub fn not_allow(&self) -> bool {
125 match self {
126 ValidatorSeparatorOption::Must(_) => false,
127 ValidatorSeparatorOption::Allow(_) => false,
128 ValidatorSeparatorOption::NotAllow => true,
129 }
130 }
131
132 #[inline]
133 pub fn must(&self) -> Option<u8> {
134 match self {
135 ValidatorSeparatorOption::Must(e) => Some(*e),
136 ValidatorSeparatorOption::Allow(_) => None,
137 ValidatorSeparatorOption::NotAllow => None,
138 }
139 }
140}
141
142#[derive(Debug, Clone, PartialEq, Eq, Educe)]
144#[educe(Default)]
145pub enum ValidatorRangeOption<T> {
146 Inside {
147 min: Option<T>,
148 max: Option<T>,
149 },
150 Outside {
151 min: Option<T>,
152 max: Option<T>,
153 },
154 #[educe(Default)]
155 NotLimited,
156}
157
158macro_rules! validator_range_option_impl {
159 ($($ty:ident),* $(,)*) => {
160 $(
161 impl ValidatorRangeOption<$ty> {
162 #[doc(hidden)]
163 #[inline]
164 pub const fn new() -> ValidatorRangeOption<$ty> {
165 ValidatorRangeOption::NotLimited
166 }
167
168 #[inline]
169 pub fn inside(&self) -> Option<(Option<$ty>, Option<$ty>)> {
170 if let ValidatorRangeOption::Inside {
171 min, max
172 } = self {
173 Some((*min, *max))
174 } else {
175 None
176 }
177 }
178
179 #[inline]
180 pub fn outside(&self) -> Option<(Option<$ty>, Option<$ty>)> {
181 if let ValidatorRangeOption::Outside {
182 min, max
183 } = self {
184 Some((*min, *max))
185 } else {
186 None
187 }
188 }
189
190 #[inline]
191 pub fn not_limited(&self) -> bool {
192 if let ValidatorRangeOption::NotLimited = self {
193 true
194 } else {
195 false
196 }
197 }
198 }
199 )*
200 }
201}
202
203validator_range_option_impl!(
204 f32, f64, u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize
205);