1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use super::Validator;
use regex_crate::Regex;
use thiserror::Error;
#[derive(Clone, Copy, Debug, Error)]
#[error("Value is smaller than minimum of {0}")]
pub struct RegexMismatchError<'re>(&'re Regex);
#[derive(Clone, Debug)]
pub struct RegexValidator<'re> {
re: &'re Regex
}
impl<'re> RegexValidator<'re> {
pub fn new(re: &'re Regex) -> Self {
Self { re }
}
}
impl<'re, T: AsRef<str>> Validator<T> for RegexValidator<'re> {
type Err = RegexMismatchError<'re>;
fn validate(self, data: &T) -> Result<(), Self::Err> {
if !self.re.is_match(data.as_ref()) {
return Err(RegexMismatchError(&self.re));
}
Ok(())
}
}