File tree 5 files changed +40
-1
lines changed
5 files changed +40
-1
lines changed Original file line number Diff line number Diff line change 71
71
mod test {
72
72
use crate :: refined:: Refined ;
73
73
use crate :: result:: Error ;
74
- use crate :: rule:: NonEmptyStringRule ;
74
+ use crate :: rule:: { ClosedAlgebraic , NonEmptyStringRule } ;
75
75
76
76
#[ test]
77
77
fn test_refined_non_empty_string_ok ( ) -> Result < ( ) , Error < String > > {
@@ -93,4 +93,14 @@ mod test {
93
93
assert_eq ! ( format!( "{}" , non_empty_string) , "Hello" ) ;
94
94
Ok ( ( ) )
95
95
}
96
+
97
+ #[ test]
98
+ fn test_refined_plus_non_empty_string ( ) -> Result < ( ) , Error < String > > {
99
+ let non_empty_string_1 = Refined :: < NonEmptyStringRule > :: new ( "Hello" . to_string ( ) ) ?;
100
+ let non_empty_string_2 = Refined :: < NonEmptyStringRule > :: new ( "World" . to_string ( ) ) ?;
101
+ let nonempty_string = non_empty_string_1. plus ( non_empty_string_2) ;
102
+
103
+ assert_eq ! ( nonempty_string. into_value( ) , "HelloWorld" ) ;
104
+ Ok ( ( ) )
105
+ }
96
106
}
Original file line number Diff line number Diff line change
1
+ mod closed_algebraic;
1
2
pub mod composer;
2
3
mod empty;
3
4
mod non_empty;
4
5
mod string;
5
6
6
7
use crate :: result:: Error ;
8
+ pub use closed_algebraic:: ClosedAlgebraic ;
7
9
pub use empty:: * ;
8
10
pub use non_empty:: * ;
9
11
pub use string:: * ;
Original file line number Diff line number Diff line change
1
+ pub trait ClosedAlgebraic {
2
+ fn plus ( self , that : Self ) -> Self ;
3
+ }
Original file line number Diff line number Diff line change 1
1
use crate :: refined:: Refined ;
2
+ use crate :: rule:: closed_algebraic:: ClosedAlgebraic ;
2
3
use crate :: rule:: NonEmpty ;
3
4
4
5
/// This is a predicate type representing a non-empty string
5
6
pub type NonEmptyString = Refined < NonEmptyStringRule > ;
6
7
7
8
pub type NonEmptyStringRule = NonEmpty < String > ;
8
9
10
+ /// # Math Theory
11
+ /// NonEmpty + NonEmpty = NonEmpty
12
+ impl ClosedAlgebraic for NonEmptyString {
13
+ fn plus ( self , that : NonEmptyString ) -> NonEmptyString {
14
+ Refined :: new ( format ! ( "{}{}" , self . into_value( ) , that. into_value( ) ) ) . unwrap ( )
15
+ }
16
+ }
17
+
9
18
#[ cfg( test) ]
10
19
mod test {
11
20
use crate :: rule:: { NonEmptyStringRule , Rule } ;
Original file line number Diff line number Diff line change
1
+ use crate :: rule:: closed_algebraic:: ClosedAlgebraic ;
1
2
use crate :: rule:: NonEmpty ;
2
3
use crate :: Refined ;
4
+ use std:: fmt:: Debug ;
3
5
4
6
pub type NonEmptyVec < T > = Refined < NonEmptyVecRule < T > > ;
5
7
pub type NonEmptyVecRule < T > = NonEmpty < Vec < T > > ;
6
8
9
+ /// # Math Theory
10
+ /// NonEmptyVec + NonEmptyVec = NonEmptyVec
11
+ impl < T > ClosedAlgebraic for NonEmptyVec < T >
12
+ where
13
+ T : Debug ,
14
+ {
15
+ fn plus ( self , that : NonEmptyVec < T > ) -> NonEmptyVec < T > {
16
+ let mut result = self . into_value ( ) ;
17
+ result. append ( & mut that. into_value ( ) ) ;
18
+ Refined :: new ( result) . unwrap ( )
19
+ }
20
+ }
21
+
7
22
#[ cfg( test) ]
8
23
mod test {
9
24
use crate :: rule:: non_empty:: NonEmptyVecRule ;
You can’t perform that action at this time.
0 commit comments