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
use crate::error::ToDefaultMessage;
macro_rules! struct_numeric_range_params {
($Params:tt, $limit:tt, $message:tt) => {
#[derive(Debug)]
pub struct $Params {
$limit: String,
}
impl $Params {
pub fn new<T>($limit: T) -> Self
where
T: PartialOrd + PartialEq + ToString,
{
Self {
$limit: $limit.to_string(),
}
}
#[allow(dead_code)]
pub fn $limit(&self) -> &str {
&self.$limit
}
}
impl ToDefaultMessage for $Params {
fn to_default_message(&self) -> String {
format!($message, self.$limit)
}
}
};
}
struct_numeric_range_params!(MinimumErrorParams, minimum, "the number must be `>= {}`.");
struct_numeric_range_params!(MaximumErrorParams, maximum, "the number must be `<= {}`.");
struct_numeric_range_params!(
ExclusiveMinimumErrorParams,
exclusive_minimum,
"the number must be `> {}`."
);
struct_numeric_range_params!(
ExclusiveMaximumErrorParams,
exclusive_maximum,
"the number must be `< {}`."
);