async_graphql/validators/
maximum.rs

1use std::fmt::Display;
2
3use num_traits::AsPrimitive;
4
5use crate::{InputType, InputValueError};
6
7pub fn maximum<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 less 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_maximum() {
30        assert!(maximum(&99, 100).is_ok());
31        assert!(maximum(&100, 100).is_ok());
32        assert!(maximum(&101, 100).is_err());
33    }
34}