File tree 1 file changed +17
-0
lines changed
1 file changed +17
-0
lines changed Original file line number Diff line number Diff line change 1
1
use crate :: refined:: Refined ;
2
+ use crate :: result:: Error ;
2
3
use crate :: rule:: NonEmptyRule ;
3
4
use std:: ops:: Add ;
5
+ use std:: str:: FromStr ;
4
6
5
7
/// This is a predicate type representing a non-empty string
6
8
pub type NonEmptyString = Refined < NonEmptyStringRule > ;
7
9
10
+ impl FromStr for NonEmptyString {
11
+ type Err = Error < String > ;
12
+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
13
+ Refined :: new ( s. to_string ( ) )
14
+ }
15
+ }
16
+
8
17
impl Add for NonEmptyString {
9
18
type Output = Self ;
10
19
@@ -19,6 +28,7 @@ pub type NonEmptyStringRule = NonEmptyRule<String>;
19
28
#[ cfg( test) ]
20
29
mod test {
21
30
use crate :: rule:: { NonEmptyString , NonEmptyStringRule , Rule } ;
31
+ use std:: str:: FromStr ;
22
32
23
33
#[ test]
24
34
fn test_non_empty_string ( ) {
@@ -35,4 +45,11 @@ mod test {
35
45
assert_eq ! ( non_empty_string. into_value( ) , "HelloWorld" ) ;
36
46
Ok ( ( ) )
37
47
}
48
+
49
+ #[ test]
50
+ fn test_from_str ( ) -> anyhow:: Result < ( ) > {
51
+ let non_empty_string = NonEmptyString :: from_str ( "Hello" ) ?;
52
+ assert_eq ! ( non_empty_string. into_value( ) , "Hello" ) ;
53
+ Ok ( ( ) )
54
+ }
38
55
}
You can’t perform that action at this time.
0 commit comments