Skip to content

Commit 0d81c93

Browse files
committed
impl Add for NonEmptyString
1 parent cf873cb commit 0d81c93

File tree

4 files changed

+37
-14
lines changed

4 files changed

+37
-14
lines changed

src/rule/empty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ where
1919
// `T` implements `EmptyDefinition`. Therefore, this `expect` is safe.
2020
Refined::new(self.into_value().empty())
2121
.ok()
22-
.expect("unreachable")
22+
.expect("This error is always unreachable")
2323
}
2424
}
2525

src/rule/non_empty.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,23 @@ mod non_empty_vec;
33

44
use crate::rule::composer::Not;
55
use crate::rule::EmptyRule;
6+
use crate::Refined;
67

78
pub use non_empty_string::*;
89
pub use non_empty_vec::*;
910

11+
pub type NonEmpty<T> = Refined<NonEmptyRule<T>>;
12+
1013
/// Rule where the data is non-empty
1114
/// ```rust
12-
/// use refined_type::rule::{NonEmpty, Rule};
13-
/// assert!(NonEmpty::<String>::validate("non empty".to_string()).is_ok());
14-
/// assert!(NonEmpty::<String>::validate("".to_string()).is_err());
15+
/// use refined_type::rule::{NonEmptyRule, Rule};
16+
/// assert!(NonEmptyRule::<String>::validate("non empty".to_string()).is_ok());
17+
/// assert!(NonEmptyRule::<String>::validate("".to_string()).is_err());
1518
///
16-
/// assert!(NonEmpty::<Vec<u8>>::validate(vec![1, 2, 3]).is_ok());
17-
/// assert!(NonEmpty::<Vec<u8>>::validate(Vec::new()).is_err());
19+
/// assert!(NonEmptyRule::<Vec<u8>>::validate(vec![1, 2, 3]).is_ok());
20+
/// assert!(NonEmptyRule::<Vec<u8>>::validate(Vec::new()).is_err());
1821
///
19-
/// assert!(NonEmpty::<u8>::validate(1).is_ok());
20-
/// assert!(NonEmpty::<u8>::validate(0).is_err());
22+
/// assert!(NonEmptyRule::<u8>::validate(1).is_ok());
23+
/// assert!(NonEmptyRule::<u8>::validate(0).is_err());
2124
/// ```
22-
pub type NonEmpty<T> = Not<EmptyRule<T>>;
25+
pub type NonEmptyRule<T> = Not<EmptyRule<T>>;
+23-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
11
use crate::refined::Refined;
2-
use crate::rule::NonEmpty;
2+
use crate::rule::NonEmptyRule;
3+
use std::ops::Add;
34

45
/// This is a predicate type representing a non-empty string
56
pub type NonEmptyString = Refined<NonEmptyStringRule>;
67

7-
pub type NonEmptyStringRule = NonEmpty<String>;
8+
impl Add for NonEmptyString {
9+
type Output = Self;
10+
11+
fn add(self, rhs: Self) -> Self::Output {
12+
Refined::new(format!("{}{}", self.into_value(), rhs.into_value()))
13+
.expect("This error is always unreachable")
14+
}
15+
}
16+
17+
pub type NonEmptyStringRule = NonEmptyRule<String>;
818

919
#[cfg(test)]
1020
mod test {
11-
use crate::rule::{NonEmptyStringRule, Rule};
21+
use crate::rule::{NonEmptyString, NonEmptyStringRule, Rule};
1222

1323
#[test]
1424
fn test_non_empty_string() {
1525
assert!(NonEmptyStringRule::validate("hello".to_string()).is_ok());
1626
assert!(NonEmptyStringRule::validate("".to_string()).is_err());
1727
}
28+
29+
#[test]
30+
fn test_add_string() -> anyhow::Result<()> {
31+
let non_empty_string_1 = NonEmptyString::new("Hello".to_string())?;
32+
let non_empty_string_2 = NonEmptyString::new("World".to_string())?;
33+
let non_empty_string = non_empty_string_1 + non_empty_string_2;
34+
35+
assert_eq!(non_empty_string.into_value(), "HelloWorld");
36+
Ok(())
37+
}
1838
}

src/rule/non_empty/non_empty_vec.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use crate::rule::NonEmpty;
1+
use crate::rule::NonEmptyRule;
22
use crate::Refined;
33

44
pub type NonEmptyVec<T> = Refined<NonEmptyVecRule<T>>;
5-
pub type NonEmptyVecRule<T> = NonEmpty<Vec<T>>;
5+
pub type NonEmptyVecRule<T> = NonEmptyRule<Vec<T>>;
66

77
#[cfg(test)]
88
mod test {

0 commit comments

Comments
 (0)