Skip to content

Commit 7cff2e8

Browse files
committed
refactoring
1 parent deb4ec3 commit 7cff2e8

File tree

3 files changed

+32
-26
lines changed

3 files changed

+32
-26
lines changed

src/rule/non_empty.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
mod non_empty_iter;
2+
mod non_empty_map;
23
mod non_empty_string;
34
mod non_empty_vec;
45

56
use crate::rule::composer::Not;
6-
use crate::rule::EmptyRule;
7+
use crate::rule::{EmptyDefinition, EmptyRule};
78
use crate::Refined;
89

910
pub use non_empty_iter::*;
11+
pub use non_empty_map::*;
1012
pub use non_empty_string::*;
1113
pub use non_empty_vec::*;
1214

@@ -25,3 +27,25 @@ pub type NonEmpty<T> = Refined<NonEmptyRule<T>>;
2527
/// assert!(NonEmptyRule::<u8>::validate(0).is_err());
2628
/// ```
2729
pub type NonEmptyRule<T> = Not<EmptyRule<T>>;
30+
31+
impl<I: ExactSizeIterator + EmptyDefinition> NonEmpty<I> {
32+
pub fn map<B, F>(self, f: F) -> NonEmptyMap<I, F>
33+
where
34+
Self: Sized,
35+
F: FnMut(I::Item) -> B,
36+
{
37+
let map_into_iter = self.into_value().map(f);
38+
Refined::new(map_into_iter)
39+
.ok()
40+
.expect("This error is always unreachable")
41+
}
42+
43+
pub fn collect<B: FromIterator<I::Item> + EmptyDefinition>(self) -> NonEmpty<B>
44+
where
45+
Self: Sized,
46+
{
47+
Refined::new(FromIterator::from_iter(self.into_value()))
48+
.ok()
49+
.expect("This error is always unreachable")
50+
}
51+
}

src/rule/non_empty/non_empty_map.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use crate::rule::NonEmptyRule;
2+
use crate::Refined;
3+
use std::iter::Map;
4+
5+
pub type NonEmptyMap<I, F> = Refined<NonEmptyMapRule<I, F>>;
6+
pub type NonEmptyMapRule<I, F> = NonEmptyRule<Map<I, F>>;

src/rule/non_empty/non_empty_vec.rs

+1-25
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,10 @@
11
use crate::rule::{EmptyDefinition, NonEmptyIntoIter, NonEmptyRule};
22
use crate::Refined;
33

4-
use std::iter::Map;
54
use std::ops::Add;
65

7-
impl<I: ExactSizeIterator + EmptyDefinition> Refined<NonEmptyRule<I>> {
8-
pub fn map<B, F>(self, f: F) -> Refined<NonEmptyRule<Map<I, F>>>
9-
where
10-
Self: Sized,
11-
F: FnMut(I::Item) -> B,
12-
{
13-
let map_into_iter = self.into_value().map(f);
14-
Refined::new(map_into_iter)
15-
.ok()
16-
.expect("This error is always unreachable")
17-
}
18-
19-
pub fn collect<B: FromIterator<I::Item> + EmptyDefinition>(self) -> Refined<NonEmptyRule<B>>
20-
where
21-
Self: Sized,
22-
{
23-
Refined::new(FromIterator::from_iter(self.into_value()))
24-
.ok()
25-
.expect("This error is always unreachable")
26-
}
27-
}
28-
296
pub type NonEmptyVec<T> = Refined<NonEmptyVecRule<T>>;
7+
pub type NonEmptyVecRule<T> = NonEmptyRule<Vec<T>>;
308

319
impl<T> NonEmptyVec<T>
3210
where
@@ -52,8 +30,6 @@ impl<T> Add for NonEmptyVec<T> {
5230
}
5331
}
5432

55-
pub type NonEmptyVecRule<T> = NonEmptyRule<Vec<T>>;
56-
5733
#[cfg(test)]
5834
mod test {
5935
use crate::rule::non_empty::NonEmptyVecRule;

0 commit comments

Comments
 (0)