-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnested.rs
More file actions
59 lines (48 loc) · 1.55 KB
/
nested.rs
File metadata and controls
59 lines (48 loc) · 1.55 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
56
57
58
59
use proc_macro2::TokenStream;
use quote::{ToTokens, format_ident, quote};
use syn::{Ident, Path, Result, meta::ParseNestedMeta};
use crate::validation::{Execution, Validation};
#[derive(Default)]
pub struct Nested {
error_type: TokenStream,
}
impl Nested {
pub fn new(error_type: TokenStream) -> Self {
Self { error_type }
}
}
impl Validation for Nested {
fn parse(meta: &ParseNestedMeta<'_>) -> Result<Self> {
let mut error_type: Option<Path> = None;
meta.parse_nested_meta(|meta| {
if meta.path.is_ident("error_type") {
error_type = Some(meta.value()?.parse()?);
Ok(())
} else {
Err(meta.error("unknown parameter"))
}
})?;
let Some(error_type) = error_type else {
return Err(meta.error("missing `error_type` parameter"));
};
Ok(Nested {
error_type: error_type.to_token_stream(),
})
}
fn ident(&self) -> Ident {
format_ident!("Nested")
}
fn error_type(&self) -> TokenStream {
self.error_type.clone()
}
fn expr(&self, execution: Execution, expr: &TokenStream) -> Option<TokenStream> {
match execution {
Execution::Sync => Some(quote! {
::fortifier::ValidateWithContext::validate_sync_with_context(&#expr, context)
}),
Execution::Async => Some(quote! {
::fortifier::ValidateWithContext::validate_async_with_context(&#expr, context).await
}),
}
}
}