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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use quote::quote;
use syn::{Data, DeriveInput, Fields, Meta, Path};
use super::ValidatorHandler;
use crate::{
common::{
attributes::http_xx_url_attribute::HttpXXUrlAttribute, tri_allow::TriAllow,
type_enum::TypeEnum,
},
panic,
};
pub(crate) struct HttpUrlHandler;
#[allow(dead_code)]
#[derive(Debug)]
pub struct Struct {
url: TypeEnum,
is_https: TypeEnum,
}
const ITEM: Struct = Struct {
url: TypeEnum::Url, is_https: TypeEnum::Boolean
};
impl ValidatorHandler for HttpUrlHandler {
fn meta_handler(ast: DeriveInput, meta: Meta) -> syn::Result<proc_macro2::TokenStream> {
let type_attribute = HttpXXUrlAttribute::build_from_meta(&meta)?;
if let Data::Struct(data) = ast.data
&& let Fields::Named(_) = &data.fields
&& data.fields.len() == 2
{
let mut token_stream = proc_macro2::TokenStream::new();
let name = ast.ident;
let error_path: Path =
syn::parse2(quote! { validators_prelude::HttpURLError }).unwrap();
#[cfg(feature = "test")]
{
let v_local = type_attribute.local;
token_stream.extend(quote! {
impl #name {
pub(crate) const V_LOCAL: validators_prelude::TriAllow = #v_local;
}
});
}
let handle_local = {
match type_attribute.local {
TriAllow::Allow => {
quote! {}
},
_ => {
let check_local = if type_attribute.local.disallow() {
quote! {
if is_local {
return Err(#error_path::LocalDisallow);
}
}
} else {
quote! {
if !is_local {
return Err(#error_path::LocalMust);
}
}
};
quote! {
let is_local = {
match url.host().unwrap() {
validators_prelude::url::Host::Domain(domain) => validators_prelude::is_local_domain(domain),
validators_prelude::url::Host::Ipv4(ip) => validators_prelude::is_local_ipv4(ip),
validators_prelude::url::Host::Ipv6(ip) => validators_prelude::is_local_ipv6(ip),
}
};
#check_local
}
},
}
};
token_stream.extend(quote! {
impl #name {
fn v_parse_str(s: &str) -> Result<(validators_prelude::url::Url, bool), #error_path> {
let is_https = {
use validators_prelude::str_utils::StartsWithIgnoreAsciiCaseMultiple;
if let Some(index) = s.starts_with_ignore_ascii_case_with_lowercase_multiple(&["http:", "https:"]) {
match index {
0 => false,
1 => true,
_ => unreachable!()
}
} else {
return Err(#error_path::ProtocolError);
}
};
let url = validators_prelude::url::Url::parse(s)?;
#handle_local
Ok((url, is_https))
}
}
});
token_stream.extend(quote! {
impl ValidateString for #name {
type Error = #error_path;
#[inline]
fn parse_string<S: Into<validators_prelude::String>>(s: S) -> Result<Self, Self::Error> {
let (url, is_https) = Self::v_parse_str(s.into().as_str())?;
Ok(Self {
url,
is_https,
})
}
#[inline]
fn parse_str<S: AsRef<str>>(s: S) -> Result<Self, Self::Error> {
let (url, is_https) = Self::v_parse_str(s.as_ref())?;
Ok(Self {
url,
is_https,
})
}
#[inline]
fn validate_str<S: AsRef<str>>(s: S) -> Result<(), Self::Error> {
Self::v_parse_str(s.as_ref())?;
Ok(())
}
}
});
#[cfg(feature = "serde")]
{
if type_attribute.serde_options.serialize {
token_stream.extend(quote! {
impl validators_prelude::serde::Serialize for #name {
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: validators_prelude::serde::Serializer, {
validators_prelude::serde::Serialize::serialize(&self.url, serializer)
}
}
});
}
if type_attribute.serde_options.deserialize {
let expect = {
let mut s = String::from("a http/https url");
match type_attribute.local {
TriAllow::Allow => (),
TriAllow::Must => {
s.push_str(" which must be local");
},
TriAllow::Disallow => {
s.push_str(" which must not be local");
},
}
s
};
token_stream.extend(quote! {
impl<'de> validators_prelude::serde::Deserialize<'de> for #name {
#[inline]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: validators_prelude::serde::Deserializer<'de>, {
struct MyVisitor;
impl<'de> validators_prelude::serde::de::Visitor<'de> for MyVisitor {
type Value = #name;
#[inline]
fn expecting(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
f.write_str(#expect)
}
#[inline]
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: validators_prelude::serde::de::Error, {
<#name as ValidateString>::parse_str(v).map_err(validators_prelude::serde::de::Error::custom)
}
}
deserializer.deserialize_str(MyVisitor)
}
}
});
}
}
#[cfg(feature = "rocket")]
{
if type_attribute.rocket_options.from_form_field {
crate::common::rocket::impl_from_form_field(&mut token_stream, &name);
}
if type_attribute.rocket_options.from_param {
crate::common::rocket::impl_from_param(&mut token_stream, &name, &error_path);
}
}
return Ok(token_stream);
}
Err(panic::validator_for_specific_item(meta.path(), ITEM))
}
}