serde_valid/utils/duration.rs
1use std::time::Duration;
2
3/// Validate that the duration is less than or equal to the maximum.
4///
5/// # Example
6///
7/// ```rust
8/// use std::time::Duration;
9///
10/// use serde_valid::utils::duration_maximum;
11/// use serde_valid::Validate;
12///
13/// #[derive(Validate)]
14/// struct TestStruct {
15/// #[validate(custom = duration_maximum(Duration::from_micros(5)))]
16/// val: Duration,
17/// }
18///
19/// let s = TestStruct {
20/// val: Duration::from_micros(5),
21/// };
22///
23/// assert!(s.validate().is_ok());
24/// ```
25#[allow(dead_code)]
26pub fn duration_maximum(
27 maximum: Duration,
28) -> impl FnOnce(&Duration) -> Result<(), crate::validation::Error> {
29 move |val: &Duration| {
30 if *val <= maximum {
31 Ok(())
32 } else {
33 Err(crate::validation::Error::Custom(format!(
34 "Duration {val:?} is greater than maximum {maximum:?}.",
35 )))
36 }
37 }
38}
39
40/// Validate that the duration is greater than or equal to the minimum.
41///
42/// # Example
43///
44/// ```rust
45/// use std::time::Duration;
46///
47/// use serde_valid::utils::duration_minimum;
48/// use serde_valid::Validate;
49///
50/// #[derive(Validate)]
51/// struct TestStruct {
52/// #[validate(custom = duration_minimum(Duration::from_micros(5)))]
53/// val: Duration,
54/// }
55///
56/// let s = TestStruct {
57/// val: Duration::from_secs(5),
58/// };
59///
60/// assert!(s.validate().is_ok());
61/// ```
62#[allow(dead_code)]
63pub fn duration_minimum(
64 minimum: Duration,
65) -> impl FnOnce(&Duration) -> Result<(), crate::validation::Error> {
66 move |val: &Duration| {
67 if *val >= minimum {
68 Ok(())
69 } else {
70 Err(crate::validation::Error::Custom(format!(
71 "Duration {val:?} is less than minimum {minimum:?}.",
72 )))
73 }
74 }
75}
76
77/// Validate that the duration is less than the exclusive maximum.
78///
79/// # Example
80///
81/// ```rust
82/// use std::time::Duration;
83///
84/// use serde_valid::utils::duration_exclusive_maximum;
85/// use serde_valid::Validate;
86///
87/// #[derive(Validate)]
88/// struct TestStruct {
89/// #[validate(custom = duration_exclusive_maximum(Duration::from_micros(5)))]
90/// val: Duration,
91/// }
92///
93/// let s = TestStruct {
94/// val: Duration::from_micros(4),
95/// };
96///
97/// assert!(s.validate().is_ok());
98/// ```
99#[allow(dead_code)]
100pub fn duration_exclusive_maximum(
101 maximum: Duration,
102) -> impl FnOnce(&Duration) -> Result<(), crate::validation::Error> {
103 move |val: &Duration| {
104 if *val < maximum {
105 Ok(())
106 } else {
107 Err(crate::validation::Error::Custom(format!(
108 "Duration {val:?} is greater than or equal to exclusive maximum {maximum:?}.",
109 )))
110 }
111 }
112}
113
114/// Validate that the duration is greater than the exclusive minimum.
115///
116/// # Example
117///
118/// ```rust
119/// use std::time::Duration;
120///
121/// use serde_valid::utils::duration_exclusive_minimum;
122/// use serde_valid::Validate;
123///
124/// #[derive(Validate)]
125/// struct TestStruct {
126/// #[validate(custom = duration_exclusive_minimum(Duration::from_micros(5)))]
127/// val: Duration,
128/// }
129///
130/// let s = TestStruct {
131/// val: Duration::from_micros(6),
132/// };
133///
134/// assert!(s.validate().is_ok());
135/// ```
136#[allow(dead_code)]
137pub fn duration_exclusive_minimum(
138 minimum: Duration,
139) -> impl FnOnce(&Duration) -> Result<(), crate::validation::Error> {
140 move |val: &Duration| {
141 if *val > minimum {
142 Ok(())
143 } else {
144 Err(crate::validation::Error::Custom(format!(
145 "Duration {val:?} is less than or equal to exclusive minimum {minimum:?}.",
146 )))
147 }
148 }
149}