refined_type/rule/number/
min_max.rs

1macro_rules! define_min_max_rule {
2    ($t: ty) => {
3        $crate::paste::item! {
4            /// A type that holds a value satisfying the `MinMaxRule`
5            pub type [<MinMax $t:camel>]<const MIN: $t, const MAX: $t> = $crate::Refined<[<MinMaxRule $t:camel>]<MIN, MAX>>;
6
7            /// Rule where the target value must be greater than or equal to `MIN` and less than or equal to `MAX`
8            pub type [<MinMaxRule $t:camel>]<const MIN: $t, const MAX: $t> = $crate::And![
9                $crate::rule::[<GreaterEqualRule $t:camel>]<MIN>,
10                $crate::rule::[<LessEqualRule $t:camel>]<MAX>
11            ];
12        }
13    };
14    ($t: ty, $($ts: ty),+) => {
15        define_min_max_rule!($t);
16        define_min_max_rule!($($ts), +);
17    };
18}
19
20define_min_max_rule!(i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize);
21
22#[cfg(test)]
23mod test {
24    use crate::rule::MinMaxI8;
25
26    #[test]
27    fn test_min_max_i8_ok() {
28        let min_max_result = MinMaxI8::<1, 10>::new(5);
29        assert!(min_max_result.is_ok());
30    }
31
32    #[test]
33    fn test_min_max_i8_err() {
34        let min_max_result = MinMaxI8::<1, 10>::new(100);
35        assert!(min_max_result.is_err());
36    }
37}