async_graphql/validators/
minimum.rs

1use std::fmt::Display;
2
3use num_traits::AsPrimitive;
4
5use crate::{InputType, InputValueError};
6
7pub fn minimum<T, N>(value: &T, n: N) -> Result<(), InputValueError<T>>
8where
9    T: AsPrimitive<N> + InputType,
10    N: PartialOrd + Display + Copy + 'static,
11{
12    if value.as_() >= n {
13        Ok(())
14    } else {
15        Err(format!(
16            "the value is {}, must be greater than or equal to {}",
17            value.as_(),
18            n
19        )
20        .into())
21    }
22}
23
24#[cfg(test)]
25mod tests {
26    use super::*;
27
28    #[test]
29    fn test_minimum() {
30        assert!(minimum(&99, 100).is_err());
31        assert!(minimum(&100, 100).is_ok());
32        assert!(minimum(&101, 100).is_ok());
33    }
34}