async-graphql/src/validators/minimum.rs

35 lines
698 B
Rust
Raw Normal View History

2021-11-15 03:08:56 +00:00
use std::fmt::Display;
2021-11-14 13:09:14 +00:00
use num_traits::AsPrimitive;
use crate::{InputType, InputValueError};
2021-11-15 03:08:56 +00:00
pub fn minimum<T, N>(value: &T, n: N) -> Result<(), InputValueError<T>>
where
T: AsPrimitive<N> + InputType,
N: PartialOrd + Display + Copy + 'static,
{
2021-11-14 13:09:14 +00:00
if value.as_() >= n {
Ok(())
} else {
Err(format!(
"the value is {}, must be greater than or equal to {}",
value.as_(),
n
)
.into())
}
}
2021-11-15 03:08:56 +00:00
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_minimum() {
assert!(minimum(&99, 100).is_err());
assert!(minimum(&100, 100).is_ok());
assert!(minimum(&101, 100).is_ok());
}
}