Skip to content

Commit

Permalink
implement range
Browse files Browse the repository at this point in the history
  • Loading branch information
tomoikey committed Oct 29, 2024
1 parent d56c4c0 commit 2d37c5a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/rule/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod less;
mod less_equal;
mod min_max;
mod odd;
mod range;

pub use equal::*;
pub use even::*;
Expand All @@ -15,3 +16,4 @@ pub use less::*;
pub use less_equal::*;
pub use min_max::*;
pub use odd::*;
pub use range::*;
46 changes: 46 additions & 0 deletions src/rule/number/range.rs
Original file line number Diff line number Diff line change
@@ -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 [<Range $t:camel>]<const FROM: $t, const UNTIL: $t> = $crate::Refined<[<RangeRule $t:camel>]<FROM, UNTIL>>;

/// Rule where the target value must be greater than or equal to `MIN` and less than `MAX`
pub type [<RangeRule $t:camel>]<const FROM: $t, const UNTIL: $t> = $crate::And![
$crate::rule::[<GreaterEqualRule $t:camel>]<FROM>,
$crate::rule::[<LessRule $t:camel>]<UNTIL>
];
}
};
($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());
}
}

0 comments on commit 2d37c5a

Please sign in to comment.