-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from yassun7010/add_duration_utils
feat: add duration validation.
- Loading branch information
Showing
7 changed files
with
335 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
mod duration; | ||
|
||
pub use duration::duration_exclusive_maximum; | ||
pub use duration::duration_exclusive_minimum; | ||
pub use duration::duration_maximum; | ||
pub use duration::duration_minimum; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
use std::time::Duration; | ||
|
||
/// Validate that the duration is less than or equal to the maximum. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// use std::time::Duration; | ||
/// | ||
/// use serde_valid::utils::duration_maximum; | ||
/// use serde_valid::Validate; | ||
/// | ||
/// #[derive(Validate)] | ||
/// struct TestStruct { | ||
/// #[validate(custom(duration_maximum(Duration::from_micros(5))))] | ||
/// val: Duration, | ||
/// } | ||
/// | ||
/// let s = TestStruct { | ||
/// val: Duration::from_micros(5), | ||
/// }; | ||
/// | ||
/// assert!(s.validate().is_ok()); | ||
/// ``` | ||
#[allow(dead_code)] | ||
pub fn duration_maximum( | ||
maximum: Duration, | ||
) -> impl FnOnce(&Duration) -> Result<(), crate::validation::Error> { | ||
move |val: &Duration| { | ||
if *val <= maximum { | ||
Ok(()) | ||
} else { | ||
Err(crate::validation::Error::Custom(format!( | ||
"Duration {val:?} is greater than maximum {maximum:?}.", | ||
))) | ||
} | ||
} | ||
} | ||
|
||
/// Validate that the duration is greater than or equal to the minimum. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// use std::time::Duration; | ||
/// | ||
/// use serde_valid::utils::duration_minimum; | ||
/// use serde_valid::Validate; | ||
/// | ||
/// #[derive(Validate)] | ||
/// struct TestStruct { | ||
/// #[validate(custom(duration_minimum(Duration::from_micros(5))))] | ||
/// val: Duration, | ||
/// } | ||
/// | ||
/// let s = TestStruct { | ||
/// val: Duration::from_secs(5), | ||
/// }; | ||
/// | ||
/// assert!(s.validate().is_ok()); | ||
/// ``` | ||
#[allow(dead_code)] | ||
pub fn duration_minimum( | ||
minimum: Duration, | ||
) -> impl FnOnce(&Duration) -> Result<(), crate::validation::Error> { | ||
move |val: &Duration| { | ||
if *val >= minimum { | ||
Ok(()) | ||
} else { | ||
Err(crate::validation::Error::Custom(format!( | ||
"Duration {val:?} is less than minimum {minimum:?}.", | ||
))) | ||
} | ||
} | ||
} | ||
|
||
/// Validate that the duration is less than the exclusive maximum. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// use std::time::Duration; | ||
/// | ||
/// use serde_valid::utils::duration_exclusive_maximum; | ||
/// use serde_valid::Validate; | ||
/// | ||
/// #[derive(Validate)] | ||
/// struct TestStruct { | ||
/// #[validate(custom(duration_exclusive_maximum(Duration::from_micros(5))))] | ||
/// val: Duration, | ||
/// } | ||
/// | ||
/// let s = TestStruct { | ||
/// val: Duration::from_micros(4), | ||
/// }; | ||
/// | ||
/// assert!(s.validate().is_ok()); | ||
/// ``` | ||
#[allow(dead_code)] | ||
pub fn duration_exclusive_maximum( | ||
maximum: Duration, | ||
) -> impl FnOnce(&Duration) -> Result<(), crate::validation::Error> { | ||
move |val: &Duration| { | ||
if *val < maximum { | ||
Ok(()) | ||
} else { | ||
Err(crate::validation::Error::Custom(format!( | ||
"Duration {val:?} is greater than or equal to exclusive maximum {maximum:?}.", | ||
))) | ||
} | ||
} | ||
} | ||
|
||
/// Validate that the duration is greater than the exclusive minimum. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// use std::time::Duration; | ||
/// | ||
/// use serde_valid::utils::duration_exclusive_minimum; | ||
/// use serde_valid::Validate; | ||
/// | ||
/// #[derive(Validate)] | ||
/// struct TestStruct { | ||
/// #[validate(custom(duration_exclusive_minimum(Duration::from_micros(5))))] | ||
/// val: Duration, | ||
/// } | ||
/// | ||
/// let s = TestStruct { | ||
/// val: Duration::from_micros(6), | ||
/// }; | ||
/// | ||
/// assert!(s.validate().is_ok()); | ||
/// ``` | ||
#[allow(dead_code)] | ||
pub fn duration_exclusive_minimum( | ||
minimum: Duration, | ||
) -> impl FnOnce(&Duration) -> Result<(), crate::validation::Error> { | ||
move |val: &Duration| { | ||
if *val > minimum { | ||
Ok(()) | ||
} else { | ||
Err(crate::validation::Error::Custom(format!( | ||
"Duration {val:?} is less than or equal to exclusive minimum {minimum:?}.", | ||
))) | ||
} | ||
} | ||
} |
Oops, something went wrong.