|
| 1 | +macro_rules! define_range_rule { |
| 2 | + ($t: ty) => { |
| 3 | + $crate::paste::item! { |
| 4 | + /// A type that holds a value satisfying the `MinMaxRule` |
| 5 | + pub type [<Range $t:camel>]<const FROM: $t, const UNTIL: $t> = $crate::Refined<[<RangeRule $t:camel>]<FROM, UNTIL>>; |
| 6 | + |
| 7 | + /// Rule where the target value must be greater than or equal to `MIN` and less than `MAX` |
| 8 | + pub type [<RangeRule $t:camel>]<const FROM: $t, const UNTIL: $t> = $crate::And![ |
| 9 | + $crate::rule::[<GreaterEqualRule $t:camel>]<FROM>, |
| 10 | + $crate::rule::[<LessRule $t:camel>]<UNTIL> |
| 11 | + ]; |
| 12 | + } |
| 13 | + }; |
| 14 | + ($t: ty, $($ts: ty),+) => { |
| 15 | + define_range_rule!($t); |
| 16 | + define_range_rule!($($ts), +); |
| 17 | + }; |
| 18 | +} |
| 19 | + |
| 20 | +define_range_rule!(i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize); |
| 21 | + |
| 22 | +#[cfg(test)] |
| 23 | +mod test { |
| 24 | + use crate::rule::RangeI8; |
| 25 | + |
| 26 | + #[test] |
| 27 | + fn test_range_i8_ok() { |
| 28 | + let range_result = RangeI8::<1, 10>::new(0); |
| 29 | + assert!(range_result.is_err()); |
| 30 | + |
| 31 | + let range_result = RangeI8::<1, 10>::new(1); |
| 32 | + assert!(range_result.is_ok()); |
| 33 | + |
| 34 | + let range_result = RangeI8::<1, 10>::new(10); |
| 35 | + assert!(range_result.is_err()); |
| 36 | + } |
| 37 | + |
| 38 | + #[test] |
| 39 | + fn test_range_i8_err() { |
| 40 | + let range_result = RangeI8::<1, 10>::new(-1); |
| 41 | + assert!(range_result.is_err()); |
| 42 | + |
| 43 | + let range_result = RangeI8::<1, 10>::new(11); |
| 44 | + assert!(range_result.is_err()); |
| 45 | + } |
| 46 | +} |
0 commit comments