-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathregex.rs
More file actions
55 lines (43 loc) · 1.28 KB
/
regex.rs
File metadata and controls
55 lines (43 loc) · 1.28 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::{Expr, Ident, Result, meta::ParseNestedMeta};
use crate::validation::Validation;
pub struct Regex {
pub expression: Expr,
}
impl Validation for Regex {
fn parse(meta: &ParseNestedMeta<'_>) -> Result<Self> {
let mut expression: Option<Expr> = None;
if let Ok(value) = meta.value() {
expression = Some(value.parse()?);
} else {
meta.parse_nested_meta(|meta| {
if meta.path.is_ident("expression") {
expression = Some(meta.value()?.parse()?);
Ok(())
} else {
Err(meta.error("unknown parameter"))
}
})?;
}
let Some(expression) = expression else {
return Err(meta.error("missing expression parameter"));
};
Ok(Regex { expression })
}
fn is_async(&self) -> bool {
false
}
fn ident(&self) -> Ident {
format_ident!("Regex")
}
fn error_type(&self) -> TokenStream {
quote!(RegexError)
}
fn tokens(&self, expr: &TokenStream) -> TokenStream {
let expression = &self.expression;
quote! {
#expr.validate_regex(#expression)
}
}
}