-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstruct.rs
More file actions
130 lines (104 loc) · 3.85 KB
/
struct.rs
File metadata and controls
130 lines (104 loc) · 3.85 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use convert_case::{Case, Casing};
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::{DataStruct, Field, Fields, Ident, Result};
use crate::validations::{email_tokens, length_tokens, parse_email, parse_length};
pub fn validate_struct_tokens(
ident: Ident,
error_ident: Ident,
data: DataStruct,
) -> Result<TokenStream> {
match data.fields {
Fields::Named(fields_named) => {
validate_named_struct_tokens(ident, error_ident, fields_named.named.into_iter())
}
Fields::Unnamed(_fields_unnamed) => todo!("fields unamed"),
Fields::Unit => todo!("fields unit"),
}
}
fn validate_named_struct_tokens(
ident: Ident,
error_ident: Ident,
fields: impl Iterator<Item = Field>,
) -> Result<TokenStream> {
let mut field_names = vec![];
let mut field_types = vec![];
let mut sync_validations = vec![];
// let async_validations = vec![];
for field in fields {
let Some(field_ident) = field.ident else {
continue;
};
let field_error_ident =
format_ident!("{}", &field_ident.to_string().to_case(Case::UpperCamel));
for attr in field.attrs {
if attr.path().is_ident("validate") {
attr.parse_nested_meta(|meta| {
if meta.path.is_ident("email") {
let email = parse_email(&meta)?;
field_names.push(field_error_ident.clone());
field_types.push(quote!(::fortifier::EmailError));
sync_validations.push(email_tokens(
email,
&error_ident,
&field_ident,
&field_error_ident,
));
Ok(())
} else if meta.path.is_ident("length") {
let length = parse_length(&meta)?;
field_names.push(field_error_ident.clone());
field_types.push(quote!(::fortifier::LengthError<usize>));
sync_validations.push(length_tokens(
length,
&error_ident,
&field_ident,
&field_error_ident,
));
Ok(())
} else {
Err(meta.error("unknown validate parameter"))
}
})?;
}
}
}
Ok(quote! {
use fortifier::*;
#[derive(Debug)]
enum #error_ident {
#( #field_names(#field_types) ),*
}
impl ::std::fmt::Display for #error_ident {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
write!(f, "{self:#?}")
}
}
impl ::std::error::Error for #error_ident {}
impl Validate for #ident {
type Error = #error_ident;
fn validate_sync(&self) -> Result<(), ValidationErrors<Self::Error>> {
use ::fortifier::*;
let mut errors = vec![];
#(#sync_validations)*
if !errors.is_empty() {
Err(errors.into())
} else {
Ok(())
}
}
fn validate_async(&self) -> ::std::pin::Pin<Box<impl Future<Output = Result<(), ValidationErrors<Self::Error>>>>> {
use ::fortifier::*;
Box::pin(async {
let mut errors = vec![];
// #(#async_validations)*
if !errors.is_empty() {
Err(errors.into())
} else {
Ok(())
}
})
}
}
})
}