From 2d37c5ae7c93cf050b7dc9dcf6f0712a38ffca80 Mon Sep 17 00:00:00 2001 From: tomoikey <55743826+tomoikey@users.noreply.github.com> Date: Wed, 30 Oct 2024 00:39:35 +0900 Subject: [PATCH] implement range --- src/rule/number.rs | 2 ++ src/rule/number/range.rs | 46 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/rule/number/range.rs diff --git a/src/rule/number.rs b/src/rule/number.rs index 42e3aaa..e46b972 100644 --- a/src/rule/number.rs +++ b/src/rule/number.rs @@ -6,6 +6,7 @@ mod less; mod less_equal; mod min_max; mod odd; +mod range; pub use equal::*; pub use even::*; @@ -15,3 +16,4 @@ pub use less::*; pub use less_equal::*; pub use min_max::*; pub use odd::*; +pub use range::*; diff --git a/src/rule/number/range.rs b/src/rule/number/range.rs new file mode 100644 index 0000000..679a99a --- /dev/null +++ b/src/rule/number/range.rs @@ -0,0 +1,46 @@ +macro_rules! define_range_rule { + ($t: ty) => { + $crate::paste::item! { + /// A type that holds a value satisfying the `MinMaxRule` + pub type [] = $crate::Refined<[]>; + + /// Rule where the target value must be greater than or equal to `MIN` and less than `MAX` + pub type [] = $crate::And![ + $crate::rule::[], + $crate::rule::[] + ]; + } + }; + ($t: ty, $($ts: ty),+) => { + define_range_rule!($t); + define_range_rule!($($ts), +); + }; +} + +define_range_rule!(i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize); + +#[cfg(test)] +mod test { + use crate::rule::RangeI8; + + #[test] + fn test_range_i8_ok() { + let range_result = RangeI8::<1, 10>::new(0); + assert!(range_result.is_err()); + + let range_result = RangeI8::<1, 10>::new(1); + assert!(range_result.is_ok()); + + let range_result = RangeI8::<1, 10>::new(10); + assert!(range_result.is_err()); + } + + #[test] + fn test_range_i8_err() { + let range_result = RangeI8::<1, 10>::new(-1); + assert!(range_result.is_err()); + + let range_result = RangeI8::<1, 10>::new(11); + assert!(range_result.is_err()); + } +}