Skip to content

Commit 730ef32

Browse files
committed
implement Length
1 parent 72a57c4 commit 730ef32

File tree

4 files changed

+128
-2
lines changed

4 files changed

+128
-2
lines changed

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ repository = "https://github.com/tomoikey/refined_type"
66
readme = "README.md"
77
categories = ["accessibility", "development-tools", "rust-patterns"]
88
license = "MIT"
9-
version = "0.5.19"
9+
version = "0.5.20"
1010
edition = "2021"
1111

1212
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -18,4 +18,4 @@ serde = { version = "1.0.214", features = ["derive"] }
1818

1919
[dev-dependencies]
2020
anyhow = "1.0.92"
21-
serde_json = "1.0.128"
21+
serde_json = "1.0.128"

src/rule/length/equal.rs

+43
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,54 @@ use crate::Refined;
55
/// A type that holds a value satisfying the `LengthEqualRule`
66
pub type LengthEqual<const LENGTH: usize, ITEM> = Refined<LengthEqualRule<LENGTH, ITEM>>;
77

8+
/// A type that holds a value satisfying the `LengthEqualVecRule`
9+
pub type LengthEqualVec<const LENGTH: usize, ITEM> = Refined<LengthEqualVecRule<LENGTH, ITEM>>;
10+
11+
/// A type that holds a value satisfying the `LengthEqualVecDequeRule`
12+
pub type LengthEqualVecDeque<const LENGTH: usize, ITEM> =
13+
Refined<LengthEqualVecDequeRule<LENGTH, ITEM>>;
14+
15+
/// A type that holds a value satisfying the `LengthEqualHashMapRule`
16+
pub type LengthEqualHashMap<const LENGTH: usize, K, V> =
17+
Refined<LengthEqualHashMapRule<LENGTH, K, V>>;
18+
19+
/// A type that holds a value satisfying the `LengthEqualHashSetRule`
20+
pub type LengthEqualHashSet<const LENGTH: usize, ITEM> =
21+
Refined<LengthEqualHashSetRule<LENGTH, ITEM>>;
22+
23+
/// A type that holds a value satisfying the `LengthEqualStringRule`
24+
pub type LengthEqualString<const LENGTH: usize> = LengthEqual<LENGTH, String>;
25+
26+
/// A type that holds a value satisfying the `LengthEqualStrRule`
27+
pub type LengthEqualStr<'a, const LENGTH: usize> = LengthEqual<LENGTH, &'a str>;
28+
829
/// Rule where the input `ITEM` has a length equal to `LENGTH`
30+
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
931
pub struct LengthEqualRule<const LENGTH: usize, ITEM> {
1032
_phantom: std::marker::PhantomData<ITEM>,
1133
}
1234

35+
/// Rule where the input `Vec` has a length equal to `LENGTH`
36+
pub type LengthEqualVecRule<const LENGTH: usize, T> = LengthEqualRule<LENGTH, Vec<T>>;
37+
38+
/// Rule where the input `VecDeque` has a length equal to `LENGTH`
39+
pub type LengthEqualVecDequeRule<const LENGTH: usize, T> =
40+
LengthEqualRule<LENGTH, std::collections::VecDeque<T>>;
41+
42+
/// Rule where the input `HashMap` has a length equal to `LENGTH`
43+
pub type LengthEqualHashMapRule<const LENGTH: usize, K, V> =
44+
LengthEqualRule<LENGTH, std::collections::HashMap<K, V>>;
45+
46+
/// Rule where the input `HashSet` has a length equal to `LENGTH`
47+
pub type LengthEqualHashSetRule<const LENGTH: usize, T> =
48+
LengthEqualRule<LENGTH, std::collections::HashSet<T>>;
49+
50+
/// Rule where the input `String` has a length equal to `LENGTH`
51+
pub type LengthEqualStringRule<const LENGTH: usize> = LengthEqualRule<LENGTH, String>;
52+
53+
/// Rule where the input `&str` has a length equal to `LENGTH`
54+
pub type LengthEqualStrRule<'a, const LENGTH: usize> = LengthEqualRule<LENGTH, &'a str>;
55+
1356
impl<const LENGTH: usize, ITEM: LengthDefinition> Rule for LengthEqualRule<LENGTH, ITEM> {
1457
type Item = ITEM;
1558
fn validate(target: Self::Item) -> Result<Self::Item, Error<Self::Item>> {

src/rule/length/grater.rs

+43
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,54 @@ use crate::Refined;
55
/// A type that holds a value satisfying the `LengthGreaterRule`
66
pub type LengthGreater<const THAN: usize, ITEM> = Refined<LengthGreaterRule<THAN, ITEM>>;
77

8+
/// A type that holds a value satisfying the `LengthGreaterVecRule`
9+
pub type LengthGreaterVec<const THAN: usize, ITEM> = Refined<LengthGreaterVecRule<THAN, ITEM>>;
10+
11+
/// A type that holds a value satisfying the `LengthGreaterVecDequeRule`
12+
pub type LengthGreaterVecDeque<const THAN: usize, ITEM> =
13+
Refined<LengthGreaterVecDequeRule<THAN, ITEM>>;
14+
15+
/// A type that holds a value satisfying the `LengthGreaterHashMapRule`
16+
pub type LengthGreaterHashMap<const THAN: usize, K, V> =
17+
Refined<LengthGreaterHashMapRule<THAN, K, V>>;
18+
19+
/// A type that holds a value satisfying the `LengthGreaterHashSetRule`
20+
pub type LengthGreaterHashSet<const THAN: usize, ITEM> =
21+
Refined<LengthGreaterHashSetRule<THAN, ITEM>>;
22+
23+
/// A type that holds a value satisfying the `LengthGreaterStringRule`
24+
pub type LengthGreaterString<const THAN: usize> = LengthGreater<THAN, String>;
25+
26+
/// A type that holds a value satisfying the `LengthGreaterStrRule`
27+
pub type LengthGreaterStr<'a, const THAN: usize> = LengthGreater<THAN, &'a str>;
28+
829
/// Rule where the input `ITEM` has a length greater than `THAN`
30+
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
931
pub struct LengthGreaterRule<const THAN: usize, ITEM> {
1032
_phantom: std::marker::PhantomData<ITEM>,
1133
}
1234

35+
/// Rule where the input `Vec` has a length greater than `THAN`
36+
pub type LengthGreaterVecRule<const THAN: usize, ITEM> = LengthGreaterRule<THAN, Vec<ITEM>>;
37+
38+
/// Rule where the input `VecDeque` has a length greater than `THAN`
39+
pub type LengthGreaterVecDequeRule<const THAN: usize, ITEM> =
40+
LengthGreaterRule<THAN, std::collections::VecDeque<ITEM>>;
41+
42+
/// Rule where the input `HashMap` has a length greater than `THAN`
43+
pub type LengthGreaterHashMapRule<const THAN: usize, K, V> =
44+
LengthGreaterRule<THAN, std::collections::HashMap<K, V>>;
45+
46+
/// Rule where the input `HashSet` has a length greater than `THAN`
47+
pub type LengthGreaterHashSetRule<const THAN: usize, ITEM> =
48+
LengthGreaterRule<THAN, std::collections::HashSet<ITEM>>;
49+
50+
/// Rule where the input `String` has a length greater than `THAN`
51+
pub type LengthGreaterStringRule<const THAN: usize> = LengthGreaterRule<THAN, String>;
52+
53+
/// Rule where the input `&str` has a length greater than `THAN`
54+
pub type LengthGreaterStrRule<'a, const THAN: usize> = LengthGreaterRule<THAN, &'a str>;
55+
1356
impl<const THAN: usize, ITEM: LengthDefinition> Rule for LengthGreaterRule<THAN, ITEM> {
1457
type Item = ITEM;
1558
fn validate(target: Self::Item) -> Result<Self::Item, Error<Self::Item>> {

src/rule/length/less.rs

+40
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,51 @@ use crate::Refined;
55
/// A type that holds a value satisfying the `LengthLessRule`
66
pub type LengthLess<const THAN: usize, ITEM> = Refined<LengthLessRule<THAN, ITEM>>;
77

8+
/// A type that holds a value satisfying the `LengthLessVecRule`
9+
pub type LengthLessVec<const THAN: usize, ITEM> = Refined<LengthLessVecRule<THAN, ITEM>>;
10+
11+
/// A type that holds a value satisfying the `LengthLessVecDequeRule`
12+
pub type LengthLessVecDeque<const THAN: usize, ITEM> = Refined<LengthLessVecDequeRule<THAN, ITEM>>;
13+
14+
/// A type that holds a value satisfying the `LengthLessHashMapRule`
15+
pub type LengthLessHashMap<const THAN: usize, K, V> = Refined<LengthLessHashMapRule<THAN, K, V>>;
16+
17+
/// A type that holds a value satisfying the `LengthLessHashSetRule`
18+
pub type LengthLessHashSet<const THAN: usize, ITEM> = Refined<LengthLessHashSetRule<THAN, ITEM>>;
19+
20+
/// A type that holds a value satisfying the `LengthLessStringRule`
21+
pub type LengthLessString<const THAN: usize> = LengthLess<THAN, String>;
22+
23+
/// A type that holds a value satisfying the `LengthLessStrRule`
24+
pub type LengthLessStr<'a, const THAN: usize> = LengthLess<THAN, &'a str>;
25+
826
/// Rule where the input `ITEM` has a length less than `THAN`
27+
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
928
pub struct LengthLessRule<const THAN: usize, ITEM> {
1029
_phantom: std::marker::PhantomData<ITEM>,
1130
}
1231

32+
/// Rule where the input `Vec` has a length less than `THAN`
33+
pub type LengthLessVecRule<const THAN: usize, ITEM> = LengthLessRule<THAN, Vec<ITEM>>;
34+
35+
/// Rule where the input `VecDeque` has a length less than `THAN`
36+
pub type LengthLessVecDequeRule<const THAN: usize, ITEM> =
37+
LengthLessRule<THAN, std::collections::VecDeque<ITEM>>;
38+
39+
/// Rule where the input `HashMap` has a length less than `THAN`
40+
pub type LengthLessHashMapRule<const THAN: usize, K, V> =
41+
LengthLessRule<THAN, std::collections::HashMap<K, V>>;
42+
43+
/// Rule where the input `HashSet` has a length less than `THAN`
44+
pub type LengthLessHashSetRule<const THAN: usize, ITEM> =
45+
LengthLessRule<THAN, std::collections::HashSet<ITEM>>;
46+
47+
/// Rule where the input `String` has a length less than `THAN`
48+
pub type LengthLessStringRule<const THAN: usize> = LengthLessRule<THAN, String>;
49+
50+
/// Rule where the input `&str` has a length less than `THAN`
51+
pub type LengthLessStrRule<'a, const THAN: usize> = LengthLessRule<THAN, &'a str>;
52+
1353
impl<const THAN: usize, ITEM: LengthDefinition> Rule for LengthLessRule<THAN, ITEM> {
1454
type Item = ITEM;
1555
fn validate(target: Self::Item) -> Result<Self::Item, Error<Self::Item>> {

0 commit comments

Comments
 (0)