Skip to content

Commit 7c53a18

Browse files
committed
refactoring
1 parent 430a688 commit 7c53a18

File tree

5 files changed

+149
-194
lines changed

5 files changed

+149
-194
lines changed

src/rule/empty.rs

+7-194
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
mod empty_definition;
2+
mod iterator;
3+
mod number;
4+
mod string;
5+
16
use crate::result::Error;
27
use crate::rule::Rule;
38
use crate::Refined;
49

5-
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};
610
use std::fmt::Debug;
7-
use std::iter::Map;
811
use std::marker::PhantomData;
912
use std::ops::Add;
1013

14+
pub use empty_definition::EmptyDefinition;
15+
1116
/// The `Empty` type is a type that indicates that its subject is empty.
1217
/// The definition of empty is defined by `EmptyDefinition`.
1318
///
@@ -50,198 +55,6 @@ where
5055
pub struct EmptyRule<T> {
5156
_phantom_data: PhantomData<T>,
5257
}
53-
pub trait EmptyDefinition {
54-
fn empty(&self) -> bool;
55-
}
56-
57-
impl EmptyDefinition for String {
58-
fn empty(&self) -> bool {
59-
self == &"".to_string()
60-
}
61-
}
62-
63-
impl EmptyDefinition for &str {
64-
fn empty(&self) -> bool {
65-
self == &""
66-
}
67-
}
68-
69-
impl<T> EmptyDefinition for Vec<T> {
70-
fn empty(&self) -> bool {
71-
self.is_empty()
72-
}
73-
}
74-
75-
impl<T> EmptyDefinition for std::vec::IntoIter<T> {
76-
fn empty(&self) -> bool {
77-
self.len() == 0
78-
}
79-
}
80-
81-
impl<'a, T> EmptyDefinition for std::slice::Iter<'a, T> {
82-
fn empty(&self) -> bool {
83-
self.len() == 0
84-
}
85-
}
86-
87-
impl<T> EmptyDefinition for VecDeque<T> {
88-
fn empty(&self) -> bool {
89-
self.is_empty()
90-
}
91-
}
92-
93-
impl<T> EmptyDefinition for std::collections::vec_deque::IntoIter<T> {
94-
fn empty(&self) -> bool {
95-
self.len() == 0
96-
}
97-
}
98-
99-
impl<'a, T> EmptyDefinition for std::collections::vec_deque::Iter<'a, T> {
100-
fn empty(&self) -> bool {
101-
self.len() == 0
102-
}
103-
}
104-
105-
impl<F, B, I: ExactSizeIterator> EmptyDefinition for Map<I, F>
106-
where
107-
F: FnMut(I::Item) -> B,
108-
{
109-
fn empty(&self) -> bool {
110-
self.len() == 0
111-
}
112-
}
113-
114-
impl<T, S> EmptyDefinition for HashSet<T, S> {
115-
fn empty(&self) -> bool {
116-
self.is_empty()
117-
}
118-
}
119-
120-
impl<T> EmptyDefinition for std::collections::hash_set::IntoIter<T> {
121-
fn empty(&self) -> bool {
122-
self.len() == 0
123-
}
124-
}
125-
126-
impl<'a, T> EmptyDefinition for std::collections::hash_set::Iter<'a, T> {
127-
fn empty(&self) -> bool {
128-
self.len() == 0
129-
}
130-
}
131-
132-
impl<K, V, S> EmptyDefinition for HashMap<K, V, S> {
133-
fn empty(&self) -> bool {
134-
self.is_empty()
135-
}
136-
}
137-
138-
impl<K, V> EmptyDefinition for std::collections::hash_map::IntoIter<K, V> {
139-
fn empty(&self) -> bool {
140-
self.len() == 0
141-
}
142-
}
143-
144-
impl<'a, K, V> EmptyDefinition for std::collections::hash_map::Iter<'a, K, V> {
145-
fn empty(&self) -> bool {
146-
self.len() == 0
147-
}
148-
}
149-
150-
impl<T> EmptyDefinition for BTreeSet<T> {
151-
fn empty(&self) -> bool {
152-
self.is_empty()
153-
}
154-
}
155-
156-
impl<K, V> EmptyDefinition for BTreeMap<K, V> {
157-
fn empty(&self) -> bool {
158-
self.is_empty()
159-
}
160-
}
161-
162-
impl EmptyDefinition for u8 {
163-
fn empty(&self) -> bool {
164-
*self == 0
165-
}
166-
}
167-
168-
impl EmptyDefinition for u16 {
169-
fn empty(&self) -> bool {
170-
*self == 0
171-
}
172-
}
173-
174-
impl EmptyDefinition for u32 {
175-
fn empty(&self) -> bool {
176-
*self == 0
177-
}
178-
}
179-
180-
impl EmptyDefinition for u64 {
181-
fn empty(&self) -> bool {
182-
*self == 0
183-
}
184-
}
185-
186-
impl EmptyDefinition for u128 {
187-
fn empty(&self) -> bool {
188-
*self == 0
189-
}
190-
}
191-
192-
impl EmptyDefinition for usize {
193-
fn empty(&self) -> bool {
194-
*self == 0
195-
}
196-
}
197-
198-
impl EmptyDefinition for i8 {
199-
fn empty(&self) -> bool {
200-
*self == 0
201-
}
202-
}
203-
204-
impl EmptyDefinition for i16 {
205-
fn empty(&self) -> bool {
206-
*self == 0
207-
}
208-
}
209-
210-
impl EmptyDefinition for i32 {
211-
fn empty(&self) -> bool {
212-
*self == 0
213-
}
214-
}
215-
216-
impl EmptyDefinition for i64 {
217-
fn empty(&self) -> bool {
218-
*self == 0
219-
}
220-
}
221-
222-
impl EmptyDefinition for i128 {
223-
fn empty(&self) -> bool {
224-
*self == 0
225-
}
226-
}
227-
228-
impl EmptyDefinition for isize {
229-
fn empty(&self) -> bool {
230-
*self == 0
231-
}
232-
}
233-
234-
impl EmptyDefinition for f32 {
235-
fn empty(&self) -> bool {
236-
*self == 0f32
237-
}
238-
}
239-
240-
impl EmptyDefinition for f64 {
241-
fn empty(&self) -> bool {
242-
*self == 0f64
243-
}
244-
}
24558

24659
impl<T> Rule for EmptyRule<T>
24760
where

src/rule/empty/empty_definition.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub trait EmptyDefinition {
2+
fn empty(&self) -> bool;
3+
}

src/rule/empty/iterator.rs

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
use crate::rule::EmptyDefinition;
2+
3+
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};
4+
use std::iter::Map;
5+
6+
impl<T> EmptyDefinition for Vec<T> {
7+
fn empty(&self) -> bool {
8+
self.is_empty()
9+
}
10+
}
11+
12+
impl<T> EmptyDefinition for std::vec::IntoIter<T> {
13+
fn empty(&self) -> bool {
14+
self.len() == 0
15+
}
16+
}
17+
18+
impl<'a, T> EmptyDefinition for std::slice::Iter<'a, T> {
19+
fn empty(&self) -> bool {
20+
self.len() == 0
21+
}
22+
}
23+
24+
impl<T> EmptyDefinition for VecDeque<T> {
25+
fn empty(&self) -> bool {
26+
self.is_empty()
27+
}
28+
}
29+
30+
impl<T> EmptyDefinition for std::collections::vec_deque::IntoIter<T> {
31+
fn empty(&self) -> bool {
32+
self.len() == 0
33+
}
34+
}
35+
36+
impl<'a, T> EmptyDefinition for std::collections::vec_deque::Iter<'a, T> {
37+
fn empty(&self) -> bool {
38+
self.len() == 0
39+
}
40+
}
41+
42+
impl<F, B, I: ExactSizeIterator> EmptyDefinition for Map<I, F>
43+
where
44+
F: FnMut(I::Item) -> B,
45+
{
46+
fn empty(&self) -> bool {
47+
self.len() == 0
48+
}
49+
}
50+
51+
impl<T, S> EmptyDefinition for HashSet<T, S> {
52+
fn empty(&self) -> bool {
53+
self.is_empty()
54+
}
55+
}
56+
57+
impl<T> EmptyDefinition for std::collections::hash_set::IntoIter<T> {
58+
fn empty(&self) -> bool {
59+
self.len() == 0
60+
}
61+
}
62+
63+
impl<'a, T> EmptyDefinition for std::collections::hash_set::Iter<'a, T> {
64+
fn empty(&self) -> bool {
65+
self.len() == 0
66+
}
67+
}
68+
69+
impl<K, V, S> EmptyDefinition for HashMap<K, V, S> {
70+
fn empty(&self) -> bool {
71+
self.is_empty()
72+
}
73+
}
74+
75+
impl<K, V> EmptyDefinition for std::collections::hash_map::IntoIter<K, V> {
76+
fn empty(&self) -> bool {
77+
self.len() == 0
78+
}
79+
}
80+
81+
impl<'a, K, V> EmptyDefinition for std::collections::hash_map::Iter<'a, K, V> {
82+
fn empty(&self) -> bool {
83+
self.len() == 0
84+
}
85+
}
86+
87+
impl<T> EmptyDefinition for BTreeSet<T> {
88+
fn empty(&self) -> bool {
89+
self.is_empty()
90+
}
91+
}
92+
93+
impl<K, V> EmptyDefinition for BTreeMap<K, V> {
94+
fn empty(&self) -> bool {
95+
self.is_empty()
96+
}
97+
}

src/rule/empty/number.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use crate::rule::EmptyDefinition;
2+
3+
macro_rules! empty_definition {
4+
($t:ty) => {
5+
impl $crate::rule::EmptyDefinition for $t {
6+
fn empty(&self) -> bool {
7+
*self == 0
8+
}
9+
}
10+
};
11+
($t:ty, $($ts:ty), +) => {
12+
empty_definition!($t);
13+
empty_definition!($($ts), +);
14+
};
15+
}
16+
17+
empty_definition!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
18+
19+
impl EmptyDefinition for f32 {
20+
fn empty(&self) -> bool {
21+
*self == 0f32
22+
}
23+
}
24+
25+
impl EmptyDefinition for f64 {
26+
fn empty(&self) -> bool {
27+
*self == 0f64
28+
}
29+
}

src/rule/empty/string.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use crate::rule::EmptyDefinition;
2+
3+
impl EmptyDefinition for String {
4+
fn empty(&self) -> bool {
5+
self == &"".to_string()
6+
}
7+
}
8+
9+
impl EmptyDefinition for &str {
10+
fn empty(&self) -> bool {
11+
self == &""
12+
}
13+
}

0 commit comments

Comments
 (0)