Skip to content

Commit 1356d6c

Browse files
committed
implement AlphaDigit
1 parent c992970 commit 1356d6c

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/rule/string.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
mod alpha_digit;
12
mod alphabet;
23
mod digit;
34
mod email;
45

6+
pub use alpha_digit::*;
57
pub use alphabet::{Alphabet, AlphabetRule};
68
pub use digit::*;
79
pub use email::{Email, EmailRule};

src/rule/string/alpha_digit.rs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
}

0 commit comments

Comments
 (0)