File tree 2 files changed +56
-0
lines changed
2 files changed +56
-0
lines changed Original file line number Diff line number Diff line change
1
+ mod alpha_digit;
1
2
mod alphabet;
2
3
mod digit;
3
4
mod email;
4
5
6
+ pub use alpha_digit:: * ;
5
7
pub use alphabet:: { Alphabet , AlphabetRule } ;
6
8
pub use digit:: * ;
7
9
pub use email:: { Email , EmailRule } ;
Original file line number Diff line number Diff line change
1
+ use crate :: result:: Error ;
2
+ use crate :: rule:: Rule ;
3
+ use crate :: Refined ;
4
+ use regex:: Regex ;
5
+
6
+ pub type AlphaDigit = Refined < AlphaDigitRule > ;
7
+ pub struct AlphaDigitRule ;
8
+
9
+ impl Rule for AlphaDigitRule {
10
+ type Item = String ;
11
+ fn validate ( target : Self :: Item ) -> Result < Self :: Item , Error < Self :: Item > > {
12
+ let regex = Regex :: new ( r"[0-9a-zA-Z]*" ) . expect ( "Invalid regex" ) ;
13
+ let is_valid = regex
14
+ . find ( target. as_str ( ) )
15
+ . is_some_and ( |matched| matched. as_str ( ) == target. as_str ( ) ) ;
16
+ if is_valid {
17
+ Ok ( target)
18
+ } else {
19
+ Err ( Error :: new (
20
+ "The input `String` have some alpha_digit characters" ,
21
+ target,
22
+ ) )
23
+ }
24
+ }
25
+ }
26
+
27
+ #[ cfg( test) ]
28
+ mod test {
29
+ use crate :: rule:: AlphaDigit ;
30
+
31
+ #[ test]
32
+ fn test_alpha_digit_ok_1 ( ) {
33
+ let alpha_digit = AlphaDigit :: new ( "1234567890" . to_string ( ) ) ;
34
+ assert ! ( alpha_digit. is_ok( ) ) ;
35
+ }
36
+
37
+ #[ test]
38
+ fn test_alpha_digit_ok_2 ( ) {
39
+ let alpha_digit = AlphaDigit :: new ( "" . to_string ( ) ) ;
40
+ assert ! ( alpha_digit. is_ok( ) ) ;
41
+ }
42
+
43
+ #[ test]
44
+ fn test_alpha_digit_ok_3 ( ) {
45
+ let alpha_digit = AlphaDigit :: new ( "1234567890abc" . to_string ( ) ) ;
46
+ assert ! ( alpha_digit. is_ok( ) ) ;
47
+ }
48
+
49
+ #[ test]
50
+ fn test_alpha_digit_err ( ) {
51
+ let alpha_digit = AlphaDigit :: new ( "1234567890abcこんにちは" . to_string ( ) ) ;
52
+ assert ! ( alpha_digit. is_err( ) ) ;
53
+ }
54
+ }
You can’t perform that action at this time.
0 commit comments