From 399a1aee41fa3a41186ea73219c68f9d386c0872 Mon Sep 17 00:00:00 2001 From: tomoikey <55743826+tomoikey@users.noreply.github.com> Date: Wed, 30 Oct 2024 00:13:49 +0900 Subject: [PATCH 01/16] implement feat less_than --- src/rule/number.rs | 2 ++ src/rule/number/less_equal.rs | 39 +++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/rule/number/less_equal.rs diff --git a/src/rule/number.rs b/src/rule/number.rs index ff0f138..521f298 100644 --- a/src/rule/number.rs +++ b/src/rule/number.rs @@ -2,6 +2,7 @@ mod equal; mod even; mod greater; mod less; +mod less_equal; mod min_max; mod odd; @@ -9,5 +10,6 @@ pub use equal::*; pub use even::*; pub use greater::*; pub use less::*; +pub use less_equal::*; pub use min_max::*; pub use odd::*; diff --git a/src/rule/number/less_equal.rs b/src/rule/number/less_equal.rs new file mode 100644 index 0000000..1de91b1 --- /dev/null +++ b/src/rule/number/less_equal.rs @@ -0,0 +1,39 @@ +use crate::{And, Refined}; +use crate::rule::{EqualRuleU8, LessRuleU8}; + +pub type LessEqualU8 = Refined>; + +pub type LessEqualRuleU8 = And![EqualRuleU8, LessRuleU8]; + +macro_rules! declare_less_equal_rule { + ($ty: ty) => { + $crate::paste::item! { + pub type [] = $crate::Refined<[]>; + + pub type [] = $crate::And![$crate::rule::[], $crate::rule::[]]; + } + }; + ($t: ty, $($ts: ty),+) => { + declare_less_equal_rule!($t); + declare_less_equal_rule!($($ts), +); + }; +} + +declare_less_equal_rule!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize); + +#[cfg(test)] +mod test { + use crate::rule::LessEqualU8; + + #[test] + fn test_less_equal_than_50u8_ok() { + let less_equal_result = LessEqualU8::<50>::new(50); + assert!(less_equal_result.is_ok()); + } + + #[test] + fn test_less_equal_than_50u8_err() { + let less_equal_result = LessEqualU8::<50>::new(51); + assert!(less_equal_result.is_err()); + } +} \ No newline at end of file From 03ffcd4651895a32d26332b93e835dcae2aac885 Mon Sep 17 00:00:00 2001 From: tomoikey <55743826+tomoikey@users.noreply.github.com> Date: Wed, 30 Oct 2024 00:15:51 +0900 Subject: [PATCH 02/16] fix less_equal --- src/rule/number.rs | 1 + src/rule/number/greater_than.rs | 0 src/rule/number/less_equal.rs | 13 +++---------- 3 files changed, 4 insertions(+), 10 deletions(-) create mode 100644 src/rule/number/greater_than.rs diff --git a/src/rule/number.rs b/src/rule/number.rs index 521f298..2893a66 100644 --- a/src/rule/number.rs +++ b/src/rule/number.rs @@ -5,6 +5,7 @@ mod less; mod less_equal; mod min_max; mod odd; +mod greater_than; pub use equal::*; pub use even::*; diff --git a/src/rule/number/greater_than.rs b/src/rule/number/greater_than.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/rule/number/less_equal.rs b/src/rule/number/less_equal.rs index 1de91b1..94089a6 100644 --- a/src/rule/number/less_equal.rs +++ b/src/rule/number/less_equal.rs @@ -1,16 +1,9 @@ -use crate::{And, Refined}; -use crate::rule::{EqualRuleU8, LessRuleU8}; - -pub type LessEqualU8 = Refined>; - -pub type LessEqualRuleU8 = And![EqualRuleU8, LessRuleU8]; - macro_rules! declare_less_equal_rule { ($ty: ty) => { $crate::paste::item! { - pub type [] = $crate::Refined<[]>; + pub type [] = $crate::Refined<[]>; - pub type [] = $crate::And![$crate::rule::[], $crate::rule::[]]; + pub type [] = $crate::And![$crate::rule::[], $crate::rule::[]]; } }; ($t: ty, $($ts: ty),+) => { @@ -36,4 +29,4 @@ mod test { let less_equal_result = LessEqualU8::<50>::new(51); assert!(less_equal_result.is_err()); } -} \ No newline at end of file +} From d610eee6ebbd2dfbfa4bc4ca9da016826a7c148f Mon Sep 17 00:00:00 2001 From: tomoikey <55743826+tomoikey@users.noreply.github.com> Date: Wed, 30 Oct 2024 00:18:17 +0900 Subject: [PATCH 03/16] implement less equal and greater equal --- src/rule/number.rs | 3 ++- src/rule/number/greater_equal.rs | 32 ++++++++++++++++++++++++++++++++ src/rule/number/greater_than.rs | 0 src/rule/number/less_equal.rs | 2 +- 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 src/rule/number/greater_equal.rs delete mode 100644 src/rule/number/greater_than.rs diff --git a/src/rule/number.rs b/src/rule/number.rs index 2893a66..42e3aaa 100644 --- a/src/rule/number.rs +++ b/src/rule/number.rs @@ -1,15 +1,16 @@ mod equal; mod even; mod greater; +mod greater_equal; mod less; mod less_equal; mod min_max; mod odd; -mod greater_than; pub use equal::*; pub use even::*; pub use greater::*; +pub use greater_equal::*; pub use less::*; pub use less_equal::*; pub use min_max::*; diff --git a/src/rule/number/greater_equal.rs b/src/rule/number/greater_equal.rs new file mode 100644 index 0000000..44004cd --- /dev/null +++ b/src/rule/number/greater_equal.rs @@ -0,0 +1,32 @@ +macro_rules! declare_greater_equal_rule { + ($ty: ty) => { + $crate::paste::item! { + pub type [] = $crate::Refined<[]>; + + pub type [] = $crate::Or![$crate::rule::[], $crate::rule::[]]; + } + }; + ($t: ty, $($ts: ty),+) => { + declare_greater_equal_rule!($t); + declare_greater_equal_rule!($($ts), +); + }; +} + +declare_greater_equal_rule!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize); + +#[cfg(test)] +mod test { + use crate::rule::GreaterEqualU8; + + #[test] + fn test_greater_equal_than_50u8_ok() { + let greater_equal_result = GreaterEqualU8::<50>::new(50); + assert!(greater_equal_result.is_ok()); + } + + #[test] + fn test_greater_equal_than_50u8_err() { + let greater_equal_result = GreaterEqualU8::<50>::new(49); + assert!(greater_equal_result.is_err()); + } +} \ No newline at end of file diff --git a/src/rule/number/greater_than.rs b/src/rule/number/greater_than.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/rule/number/less_equal.rs b/src/rule/number/less_equal.rs index 94089a6..0477a32 100644 --- a/src/rule/number/less_equal.rs +++ b/src/rule/number/less_equal.rs @@ -3,7 +3,7 @@ macro_rules! declare_less_equal_rule { $crate::paste::item! { pub type [] = $crate::Refined<[]>; - pub type [] = $crate::And![$crate::rule::[], $crate::rule::[]]; + pub type [] = $crate::Or![$crate::rule::[], $crate::rule::[]]; } }; ($t: ty, $($ts: ty),+) => { From 7082e7dc4c6ce40dc8ee97ebeb4dbed880c016fe Mon Sep 17 00:00:00 2001 From: tomoikey <55743826+tomoikey@users.noreply.github.com> Date: Wed, 30 Oct 2024 00:19:04 +0900 Subject: [PATCH 04/16] update version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 432e4ec..33d1c36 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ repository = "https://github.com/tomoikey/refined-type" readme = "README.md" categories = ["accessibility", "development-tools", "rust-patterns"] license = "MIT" -version = "0.5.12" +version = "0.5.13" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html From 92f238d15818834fa28e2421c4d13e76d12b96a9 Mon Sep 17 00:00:00 2001 From: tomoikey <55743826+tomoikey@users.noreply.github.com> Date: Wed, 30 Oct 2024 00:20:01 +0900 Subject: [PATCH 05/16] update dependencies --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 33d1c36..b0c869e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,8 +13,8 @@ edition = "2021" [dependencies] paste = "1.0.15" -regex = "1.11.0" -serde = { version = "1.0.210", features = ["derive"] } +regex = "1.11.1" +serde = { version = "1.0.214", features = ["derive"] } [dev-dependencies] anyhow = "1.0.89" From f1d0eef35a5e9a7100a252300f417e4b1057c990 Mon Sep 17 00:00:00 2001 From: tomoikey <55743826+tomoikey@users.noreply.github.com> Date: Wed, 30 Oct 2024 00:24:11 +0900 Subject: [PATCH 06/16] add tests for less_equal greater_equal --- README.md | 72 ++++++++++++++++++++++++++++-------------------- tests/read_me.rs | 39 ++++++++++++++++++++++---- 2 files changed, 75 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index f5673b0..5ccab9d 100644 --- a/README.md +++ b/README.md @@ -381,6 +381,48 @@ fn equal_example() -> Result<(), Error> { } ``` +## LessEqual + +`LessEqual` is a type that signifies the target is less than or equal to a certain number. + +```rust +type Age = LessEqualU8<80>; + +fn less_equal_example() -> Result<(), Error> { + let age = Age::new(79)?; + assert_eq!(age.into_value(), 79); + + let age = Age::new(80)?; + assert_eq!(age.into_value(), 80); + + let age = Age::new(81); + assert!(age.is_err()); + + Ok(()) +} +``` + +## GreaterEqual + +`GreaterEqual` is a type that signifies the target is greater than or equal to a certain number. + +```rust +type Age = GreaterEqualU8<18>; + +fn greater_equal_example() -> Result<(), Error> { + let age = Age::new(19)?; + assert_eq!(age.into_value(), 19); + + let age = Age::new(18)?; + assert_eq!(age.into_value(), 18); + + let age = Age::new(17); + assert!(age.is_err()); + + Ok(()) +} +``` + # Iterator `refined_type` has several useful refined types for Iterators. @@ -604,36 +646,6 @@ impl SkipOption for NoSkip { } ``` ---- - -## `into_iter()` and `iter()` - -The Iterator I’ve prepared has `into_iter` and `iter` implemented. -Therefore, you can easily map or convert it to a different Iterator using `collect`. -Feel free to explore the capabilities of the Iterator you’ve been given! - -### `into_iter()` - -```rust -fn example_20() -> anyhow::Result<()> { - let ne_vec = NonEmptyVec::new(vec![1, 2, 3])?; - let ne_vec: NonEmptyVec = ne_vec.into_iter().map(|n| n * 2).map(|n| n * 3).collect(); - assert_eq!(ne_vec.into_value(), vec![6, 12, 18]); - Ok(()) -} -``` - -### `iter()` - -```rust -fn example_21() -> anyhow::Result<()> { - let ne_vec = NonEmptyVec::new(vec![1, 2, 3])?; - let ne_vec: NonEmptyVec = ne_vec.iter().map(|n| n * 2).map(|n| n * 3).collect(); - assert_eq!(ne_vec.into_value(), vec![6, 12, 18]); - Ok(()) -} -``` - ### `NonEmptyVec` to `NonEmptyVecDeque` using `collect()` ```rust diff --git a/tests/read_me.rs b/tests/read_me.rs index ffe602e..0c431ae 100644 --- a/tests/read_me.rs +++ b/tests/read_me.rs @@ -3,12 +3,7 @@ use serde_json::json; use refined_type::result::Error; use refined_type::rule::composer::Not; -use refined_type::rule::{ - EqualU8, ExistsVec, ForAllVec, GreaterU8, HeadVec, IndexRuleVec, IndexVec, InitVec, LastVec, - LengthDefinition, LengthEqual, LengthEqualRule, LengthGreater, LengthLess, LengthMinMax, - LessU8, MinMaxU8, NonEmptyString, NonEmptyStringRule, NonEmptyVec, NonEmptyVecDeque, Reverse, - Rule, SkipFirst, SkipVec, TailVec, -}; +use refined_type::rule::{EqualU8, ExistsVec, ForAllVec, GreaterEqualU8, GreaterU8, HeadVec, IndexRuleVec, IndexVec, InitVec, LastVec, LengthDefinition, LengthEqual, LengthEqualRule, LengthGreater, LengthLess, LengthMinMax, LessEqualU8, LessU8, MinMaxU8, NonEmptyString, NonEmptyStringRule, NonEmptyVec, NonEmptyVecDeque, Reverse, Rule, SkipFirst, SkipVec, TailVec}; use refined_type::{And, Or, Refined}; // define a struct for converting from JSON. @@ -312,6 +307,38 @@ fn equal_example() -> Result<(), Error> { Ok(()) } +#[test] +fn less_equal_example() -> Result<(), Error> { + type Age = LessEqualU8<80>; + + let age = Age::new(79)?; + assert_eq!(age.into_value(), 79); + + let age = Age::new(80)?; + assert_eq!(age.into_value(), 80); + + let age = Age::new(81); + assert!(age.is_err()); + + Ok(()) +} + +#[test] +fn greater_equal_example() -> Result<(), Error> { + type Age = GreaterEqualU8<18>; + + let age = Age::new(19)?; + assert_eq!(age.into_value(), 19); + + let age = Age::new(18)?; + assert_eq!(age.into_value(), 18); + + let age = Age::new(17); + assert!(age.is_err()); + + Ok(()) +} + #[test] fn example_11() -> Result<(), Error>> { let vec = vec!["Hello".to_string(), "World".to_string()]; From 9cc66eb510890dfff6ac8ff7563bc97345b8b76d Mon Sep 17 00:00:00 2001 From: tomoikey <55743826+tomoikey@users.noreply.github.com> Date: Wed, 30 Oct 2024 00:25:31 +0900 Subject: [PATCH 07/16] cargo fmt --- src/rule/number/greater_equal.rs | 2 +- tests/read_me.rs | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/rule/number/greater_equal.rs b/src/rule/number/greater_equal.rs index 44004cd..47949ef 100644 --- a/src/rule/number/greater_equal.rs +++ b/src/rule/number/greater_equal.rs @@ -29,4 +29,4 @@ mod test { let greater_equal_result = GreaterEqualU8::<50>::new(49); assert!(greater_equal_result.is_err()); } -} \ No newline at end of file +} diff --git a/tests/read_me.rs b/tests/read_me.rs index 0c431ae..708e2a7 100644 --- a/tests/read_me.rs +++ b/tests/read_me.rs @@ -3,7 +3,12 @@ use serde_json::json; use refined_type::result::Error; use refined_type::rule::composer::Not; -use refined_type::rule::{EqualU8, ExistsVec, ForAllVec, GreaterEqualU8, GreaterU8, HeadVec, IndexRuleVec, IndexVec, InitVec, LastVec, LengthDefinition, LengthEqual, LengthEqualRule, LengthGreater, LengthLess, LengthMinMax, LessEqualU8, LessU8, MinMaxU8, NonEmptyString, NonEmptyStringRule, NonEmptyVec, NonEmptyVecDeque, Reverse, Rule, SkipFirst, SkipVec, TailVec}; +use refined_type::rule::{ + EqualU8, ExistsVec, ForAllVec, GreaterEqualU8, GreaterU8, HeadVec, IndexRuleVec, IndexVec, + InitVec, LastVec, LengthDefinition, LengthEqual, LengthEqualRule, LengthGreater, LengthLess, + LengthMinMax, LessEqualU8, LessU8, MinMaxU8, NonEmptyString, NonEmptyStringRule, NonEmptyVec, + NonEmptyVecDeque, Reverse, Rule, SkipFirst, SkipVec, TailVec, +}; use refined_type::{And, Or, Refined}; // define a struct for converting from JSON. From 20db21880d1dac6899a6721167db949267910e05 Mon Sep 17 00:00:00 2001 From: tomoikey <55743826+tomoikey@users.noreply.github.com> Date: Wed, 30 Oct 2024 00:28:50 +0900 Subject: [PATCH 08/16] add comments greater equal and less equal --- src/rule/number/greater_equal.rs | 2 ++ src/rule/number/less_equal.rs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/rule/number/greater_equal.rs b/src/rule/number/greater_equal.rs index 47949ef..ca3d791 100644 --- a/src/rule/number/greater_equal.rs +++ b/src/rule/number/greater_equal.rs @@ -1,8 +1,10 @@ macro_rules! declare_greater_equal_rule { ($ty: ty) => { $crate::paste::item! { + /// A type that holds a value satisfying the `GreaterEqualRule` pub type [] = $crate::Refined<[]>; + /// Rule where the target value must be greater than or equal to `N` pub type [] = $crate::Or![$crate::rule::[], $crate::rule::[]]; } }; diff --git a/src/rule/number/less_equal.rs b/src/rule/number/less_equal.rs index 0477a32..4607920 100644 --- a/src/rule/number/less_equal.rs +++ b/src/rule/number/less_equal.rs @@ -1,8 +1,10 @@ macro_rules! declare_less_equal_rule { ($ty: ty) => { $crate::paste::item! { + /// A type that holds a value satisfying the `LessEqualRule` pub type [] = $crate::Refined<[]>; + /// Rule where the target value must be less than or equal to `N` pub type [] = $crate::Or![$crate::rule::[], $crate::rule::[]]; } }; From 2beb45ebd6d8cb68adf4a8d4b93d6351a7582c60 Mon Sep 17 00:00:00 2001 From: tomoikey <55743826+tomoikey@users.noreply.github.com> Date: Wed, 30 Oct 2024 00:29:59 +0900 Subject: [PATCH 09/16] add comments for greater_than and less_than --- src/rule/number/greater.rs | 2 ++ src/rule/number/less.rs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/rule/number/greater.rs b/src/rule/number/greater.rs index 59b9d07..370b127 100644 --- a/src/rule/number/greater.rs +++ b/src/rule/number/greater.rs @@ -1,8 +1,10 @@ macro_rules! define_greater_rule { ($t: ty) => { $crate::paste::item! { + /// A type that holds a value satisfying the `GreaterRule` pub type [] = $crate::Refined<[]>; + /// Rule where the target value must be greater than `THAN` #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub struct []; diff --git a/src/rule/number/less.rs b/src/rule/number/less.rs index 2cfae5c..de5a94c 100644 --- a/src/rule/number/less.rs +++ b/src/rule/number/less.rs @@ -1,8 +1,10 @@ macro_rules! define_less_rule { ($t: ty) => { $crate::paste::item! { + /// A type that holds a value satisfying the `LessRule` pub type [] = $crate::Refined<[]>; + /// Rule where the target value must be less than `THAN` #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub struct []; From 112fc8ab44309d0156eacf8fab59ea9ff85f1d30 Mon Sep 17 00:00:00 2001 From: tomoikey <55743826+tomoikey@users.noreply.github.com> Date: Wed, 30 Oct 2024 00:30:36 +0900 Subject: [PATCH 10/16] add comments minmax --- src/rule/number/min_max.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/rule/number/min_max.rs b/src/rule/number/min_max.rs index e371f31..d82489f 100644 --- a/src/rule/number/min_max.rs +++ b/src/rule/number/min_max.rs @@ -1,8 +1,10 @@ macro_rules! define_min_max_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 or equal to `MAX` pub type [] = $crate::And![ $crate::Or![$crate::rule::[], $crate::rule::[]], $crate::Or![$crate::rule::[], $crate::rule::[]] From d56c4c02476cfc87dc2ac5564f027a52a429c118 Mon Sep 17 00:00:00 2001 From: tomoikey <55743826+tomoikey@users.noreply.github.com> Date: Wed, 30 Oct 2024 00:32:36 +0900 Subject: [PATCH 11/16] refatoring minmax --- src/rule/number/min_max.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rule/number/min_max.rs b/src/rule/number/min_max.rs index d82489f..b2c00b6 100644 --- a/src/rule/number/min_max.rs +++ b/src/rule/number/min_max.rs @@ -6,8 +6,8 @@ macro_rules! define_min_max_rule { /// Rule where the target value must be greater than or equal to `MIN` and less than or equal to `MAX` pub type [] = $crate::And![ - $crate::Or![$crate::rule::[], $crate::rule::[]], - $crate::Or![$crate::rule::[], $crate::rule::[]] + $crate::rule::[], + $crate::rule::[] ]; } }; 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 12/16] 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()); + } +} From 3913009a458ba98dcc7557b6681fc2037657970c Mon Sep 17 00:00:00 2001 From: tomoikey <55743826+tomoikey@users.noreply.github.com> Date: Wed, 30 Oct 2024 00:40:29 +0900 Subject: [PATCH 13/16] fix comment of RangeRule --- src/rule/number/range.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rule/number/range.rs b/src/rule/number/range.rs index 679a99a..3569646 100644 --- a/src/rule/number/range.rs +++ b/src/rule/number/range.rs @@ -1,10 +1,10 @@ macro_rules! define_range_rule { ($t: ty) => { $crate::paste::item! { - /// A type that holds a value satisfying the `MinMaxRule` + /// A type that holds a value satisfying the `RangeRule` pub type [] = $crate::Refined<[]>; - /// Rule where the target value must be greater than or equal to `MIN` and less than `MAX` + /// Rule where the target value must be greater than or equal to `FROM` and less than `UNTIL` pub type [] = $crate::And![ $crate::rule::[], $crate::rule::[] From 7c2488c489517ed55b42b6a59b67bf84cfa2cde3 Mon Sep 17 00:00:00 2001 From: tomoikey <55743826+tomoikey@users.noreply.github.com> Date: Wed, 30 Oct 2024 00:46:39 +0900 Subject: [PATCH 14/16] implement Range --- README.md | 29 +++++++++++++++++++++++++++-- tests/read_me.rs | 26 ++++++++++++++++++++------ 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 5ccab9d..68b3809 100644 --- a/README.md +++ b/README.md @@ -381,7 +381,7 @@ fn equal_example() -> Result<(), Error> { } ``` -## LessEqual +## `LessEqual` `LessEqual` is a type that signifies the target is less than or equal to a certain number. @@ -402,7 +402,7 @@ fn less_equal_example() -> Result<(), Error> { } ``` -## GreaterEqual +## `GreaterEqual` `GreaterEqual` is a type that signifies the target is greater than or equal to a certain number. @@ -423,6 +423,31 @@ fn greater_equal_example() -> Result<(), Error> { } ``` +## `Range` + +`Range` is a type that signifies the target exists between a certain number and another number. + +```rust +type Age = RangeU8<18, 80>; + +fn range_example() -> Result<(), Error> { + let age = Age::new(17); + assert!(age.is_err()); + + let age = Age::new(18)?; + assert_eq!(age.into_value(), 18); + + let age = Age::new(79)?; + assert_eq!(age.into_value(), 79); + + let age = Age::new(80); + assert!(age.is_err()); + + Ok(()) +} +``` + + # Iterator `refined_type` has several useful refined types for Iterators. diff --git a/tests/read_me.rs b/tests/read_me.rs index 708e2a7..9c9e471 100644 --- a/tests/read_me.rs +++ b/tests/read_me.rs @@ -3,12 +3,7 @@ use serde_json::json; use refined_type::result::Error; use refined_type::rule::composer::Not; -use refined_type::rule::{ - EqualU8, ExistsVec, ForAllVec, GreaterEqualU8, GreaterU8, HeadVec, IndexRuleVec, IndexVec, - InitVec, LastVec, LengthDefinition, LengthEqual, LengthEqualRule, LengthGreater, LengthLess, - LengthMinMax, LessEqualU8, LessU8, MinMaxU8, NonEmptyString, NonEmptyStringRule, NonEmptyVec, - NonEmptyVecDeque, Reverse, Rule, SkipFirst, SkipVec, TailVec, -}; +use refined_type::rule::{EqualU8, ExistsVec, ForAllVec, GreaterEqualU8, GreaterU8, HeadVec, IndexRuleVec, IndexVec, InitVec, LastVec, LengthDefinition, LengthEqual, LengthEqualRule, LengthGreater, LengthLess, LengthMinMax, LessEqualU8, LessU8, MinMaxU8, NonEmptyString, NonEmptyStringRule, NonEmptyVec, NonEmptyVecDeque, RangeU8, Reverse, Rule, SkipFirst, SkipVec, TailVec}; use refined_type::{And, Or, Refined}; // define a struct for converting from JSON. @@ -344,6 +339,25 @@ fn greater_equal_example() -> Result<(), Error> { Ok(()) } +#[test] +fn range_example() -> Result<(), Error> { + type Age = RangeU8<18, 80>; + + let age = Age::new(17); + assert!(age.is_err()); + + let age = Age::new(18)?; + assert_eq!(age.into_value(), 18); + + let age = Age::new(79)?; + assert_eq!(age.into_value(), 79); + + let age = Age::new(80); + assert!(age.is_err()); + + Ok(()) +} + #[test] fn example_11() -> Result<(), Error>> { let vec = vec!["Hello".to_string(), "World".to_string()]; From aa257cdacbb0e7710d8803616f25e44115215966 Mon Sep 17 00:00:00 2001 From: tomoikey <55743826+tomoikey@users.noreply.github.com> Date: Wed, 30 Oct 2024 00:46:59 +0900 Subject: [PATCH 15/16] cargo fmt --all --- tests/read_me.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/read_me.rs b/tests/read_me.rs index 9c9e471..cd7093c 100644 --- a/tests/read_me.rs +++ b/tests/read_me.rs @@ -3,7 +3,12 @@ use serde_json::json; use refined_type::result::Error; use refined_type::rule::composer::Not; -use refined_type::rule::{EqualU8, ExistsVec, ForAllVec, GreaterEqualU8, GreaterU8, HeadVec, IndexRuleVec, IndexVec, InitVec, LastVec, LengthDefinition, LengthEqual, LengthEqualRule, LengthGreater, LengthLess, LengthMinMax, LessEqualU8, LessU8, MinMaxU8, NonEmptyString, NonEmptyStringRule, NonEmptyVec, NonEmptyVecDeque, RangeU8, Reverse, Rule, SkipFirst, SkipVec, TailVec}; +use refined_type::rule::{ + EqualU8, ExistsVec, ForAllVec, GreaterEqualU8, GreaterU8, HeadVec, IndexRuleVec, IndexVec, + InitVec, LastVec, LengthDefinition, LengthEqual, LengthEqualRule, LengthGreater, LengthLess, + LengthMinMax, LessEqualU8, LessU8, MinMaxU8, NonEmptyString, NonEmptyStringRule, NonEmptyVec, + NonEmptyVecDeque, RangeU8, Reverse, Rule, SkipFirst, SkipVec, TailVec, +}; use refined_type::{And, Or, Refined}; // define a struct for converting from JSON. From d04f6b120378a1b7f2fe365b37fffb243361c7b0 Mon Sep 17 00:00:00 2001 From: tomoikey <55743826+tomoikey@users.noreply.github.com> Date: Wed, 30 Oct 2024 01:04:11 +0900 Subject: [PATCH 16/16] refactoring --- src/rule/number/min_max.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/rule/number/min_max.rs b/src/rule/number/min_max.rs index b2c00b6..50d9eb8 100644 --- a/src/rule/number/min_max.rs +++ b/src/rule/number/min_max.rs @@ -5,9 +5,9 @@ macro_rules! define_min_max_rule { pub type [] = $crate::Refined<[]>; /// Rule where the target value must be greater than or equal to `MIN` and less than or equal to `MAX` - pub type [] = $crate::And![ - $crate::rule::[], - $crate::rule::[] + pub type [] = $crate::Or![ + $crate::rule::[], + $crate::rule::[] ]; } };