validated_struct_macros 2.1.0

Macros for validated_struct
Documentation
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#![allow(clippy::too_many_arguments)]
use std::mem::MaybeUninit;

use proc_macro::TokenStream;
use quote::{quote, ToTokens};
use syn::{
    parenthesized,
    parse::{Parse, ParseStream},
    punctuated::Punctuated,
    Attribute, Expr, Ident, Token, Type, Visibility,
};

#[derive(Clone)]
enum FieldType {
    Concrete(syn::Type),
    Structure(StructSpec),
}
impl FieldType {
    fn ty(&self) -> syn::Type {
        match self {
            FieldType::Concrete(t) => t.clone(),
            FieldType::Structure(s) => syn::parse2(s.ident.to_token_stream()).unwrap(),
        }
    }
}

#[derive(Clone)]
struct FieldSpec {
    attributes: Vec<Attribute>,
    is_validated_map: bool,
    vis: Visibility,
    ident: Ident,
    ty: FieldType,
    constraint: Option<Expr>,
}
impl FieldSpec {
    fn recursive_accessors(&self) -> bool {
        if let FieldType::Structure(_) = &self.ty {
            true
        } else {
            self.is_validated_map
        }
    }
}
#[derive(Clone)]
struct StructSpec {
    #[allow(dead_code)]
    visibility: Visibility,
    attrs: Vec<Attribute>,
    recursive_attrs: Vec<Attribute>,
    ident: Ident,
    fields: Punctuated<FieldSpec, Token![,]>,
}
const SEPARATOR: char = if cfg!(feature = "dot_separator") {
    '.'
} else {
    '/'
};
impl StructSpec {
    fn flatten_go(
        mut self,
        list: &mut Vec<Self>,
        recursive_attrs: &mut Vec<Vec<Attribute>>,
    ) -> Self {
        let mut self_rec_attrs = Vec::new();
        std::mem::swap(&mut self_rec_attrs, &mut self.recursive_attrs);
        recursive_attrs.push(self_rec_attrs);
        self.attrs.extend(recursive_attrs.iter().flatten().cloned());
        for field in self.fields.iter_mut() {
            if let FieldType::Structure(s) = &mut field.ty {
                let mut tmp = MaybeUninit::uninit();
                std::mem::swap(s, unsafe { &mut *tmp.as_mut_ptr() });
                tmp = MaybeUninit::new(
                    unsafe { tmp.assume_init() }.flatten_go(list, recursive_attrs),
                );
                std::mem::swap(s, unsafe { &mut *tmp.as_mut_ptr() });
            }
        }
        recursive_attrs.pop();
        list.push(self.clone());
        self
    }
    fn flatten(self) -> Vec<Self> {
        let mut list = Vec::with_capacity(1);
        let mut rec_attrs = Vec::with_capacity(1);
        self.flatten_go(&mut list, &mut rec_attrs);
        list
    }
    fn structure(&self) -> impl quote::ToTokens {
        unzip_n::unzip_n!(10);
        let ident = &self.ident;
        let mut notifying = false;
        let sattrs: Vec<_> = self
            .attrs
            .iter()
            .filter_map(|a| {
                if a.path.is_ident("notifying") {
                    notifying = true;
                    None
                } else {
                    Some(a.clone())
                }
            })
            .collect();
        let (
            fields,
            args,
            associations,
            accessors,
            constructor_validations,
            constructor_rec_validations,
            serde_match,
            get_match,
            json_get_match,
            get_keys,
        ) = self
            .fields
            .iter()
            .map(|spec| {
                let id = &spec.ident;
                let field_name = id;
                let ty = spec.ty.ty();
                let predicate = spec.constraint.as_ref().map(|e| quote! {#e(&value)});
                let str_id = format!("{}", id);
                let set_id = quote::format_ident!("set_{}", id);
                let validate_id = quote::format_ident!("validate_{}", id);
                let validate_id_rec = quote::format_ident!("validate_{}_rec", id);
                (
                    field(spec, field_name),
                    quote! {#id: #ty},
                    quote! {#field_name: #id},
                    accessors(
                        spec,
                        id,
                        field_name,
                        &ty,
                        &set_id,
                        &validate_id,
                        &validate_id_rec,
                        &predicate,
                    ),
                    if predicate.is_some() {
                        Some(quote! {Self::#validate_id(self.#id())})
                    } else {
                        None
                    },
                    quote! {Self::#validate_id_rec(self.#id())},
                    serde_match(id, spec, &str_id, set_id, field_name),
                    get_match(spec, id, &str_id, field_name),
                    json_get_match(spec, id, &str_id, field_name),
                    keys_match(spec, field_name, &str_id),
                )
            })
            .collect::<Vec<_>>()
            .into_iter()
            .unzip_n_vec();
        let serde_access =
            serde_access(ident, &serde_match, &get_match, &get_keys, &json_get_match);
        let constructor_validations = constructor_validations
            .into_iter()
            .flatten()
            .collect::<Vec<_>>();
        main_implementation(
            &sattrs,
            ident,
            &fields,
            &constructor_validations,
            &constructor_rec_validations,
            &args,
            &associations,
            &accessors,
            &serde_access,
        )
    }
}

fn main_implementation(
    sattrs: &[Attribute],
    ident: &Ident,
    fields: &[proc_macro2::TokenStream],
    constructor_validations: &[proc_macro2::TokenStream],
    constructor_rec_validations: &[proc_macro2::TokenStream],
    args: &[proc_macro2::TokenStream],
    associations: &[proc_macro2::TokenStream],
    accessors: &[proc_macro2::TokenStream],
    serde_access: &Option<proc_macro2::TokenStream>,
) -> proc_macro2::TokenStream {
    quote! {
        #(#sattrs)*
        pub struct #ident {
            #(#fields),*
        }
        impl #ident {
            pub fn validate(&self) -> bool {
                true #(&& #constructor_validations)*
            }
            fn validate_rec(&self) -> bool {
                true #(&& #constructor_rec_validations)*
            }
            #[allow(clippy::too_many_arguments)]
            pub fn new(#(#args),*) -> Result<Self, Self> {
                let constructed = #ident {
                    #(#associations),*
                };
                if constructed.validate() {Ok(constructed)} else {Err(constructed)}
            }
            #(#accessors)*
        }
        #serde_access
    }
}

fn field(spec: &FieldSpec, field: &Ident) -> proc_macro2::TokenStream {
    let ty = spec.ty.ty();
    let attrs = &spec.attributes;
    let vis = &spec.vis;
    quote! {#(#attrs)* #vis #field: #ty}
}

fn serde_access(
    ident: &Ident,
    serde_match: &[proc_macro2::TokenStream],
    get_match: &[proc_macro2::TokenStream],
    get_keys: &[proc_macro2::TokenStream],
    json_get_match: &[proc_macro2::TokenStream],
) -> Option<proc_macro2::TokenStream> {
    let get_json = cfg!(feature = "serde_json").then(|| {
        quote! {
            fn get_json(& self, key: &str) -> Result<String, validated_struct::GetError>{
                use std::any::Any;
                match validated_struct::split_once(key, #SEPARATOR) {
                    #(#json_get_match)*
                    ("", key) if !key.is_empty() => self.get_json(key),
                    _ => Err(validated_struct::GetError::NoMatchingKey),
                }
            }
        }
    });
    cfg!(feature = "serde").then(|| quote! {
        impl #ident {
            pub fn from_deserializer<'d, D: serde::Deserializer<'d>>(
                d: D,
            ) -> Result<Self, Result<Self, D::Error>>
            where
                Self: serde::Deserialize<'d>,
            {
                match <Self as serde::Deserialize>::deserialize(d) {
                    Ok(value) => {
                        if value.validate_rec() {
                            Ok(value)
                        } else {
                            Err(Ok(value))
                        }
                    }
                    Err(e) => Err(Err(e)),
                }
            }
        }
        impl<'a> validated_struct::ValidatedMapAssociatedTypes<'a> for #ident {
            type Accessor = &'a dyn std::any::Any;
        }
        impl validated_struct::ValidatedMap for #ident {
            fn insert<'d, D: serde::Deserializer<'d>>(&mut self, key: &str, value: D) -> Result<(), validated_struct::InsertionError>
            where
                validated_struct::InsertionError: From<D::Error> {
                if let Some(e) = match validated_struct::split_once(key, #SEPARATOR) {
                    #(#serde_match)*
                    ("", key) if !key.is_empty() => self.insert(key, value).err(),
                    _ => Some("unknown key".into())
                } {return Err(e)};
                Ok(())
            }
            fn get<'a>(&'a self, key: &str) -> Result<&dyn std::any::Any, validated_struct::GetError>{
                use std::any::Any;
                match validated_struct::split_once(key, #SEPARATOR) {
                    #(#get_match)*
                    ("", key) if !key.is_empty() => self.get(key),
                    _ => Err(validated_struct::GetError::NoMatchingKey),
                }
            }
            #get_json
            type Keys = std::vec::Vec<String>;
            fn keys(&self) -> Self::Keys {
                let mut keys = std::vec::Vec::new();
                #(#get_keys)*
                keys
            }
        }
    })
}

fn accessors(
    spec: &FieldSpec,
    id: &Ident,
    field: &Ident,
    ty: &Type,
    set_id: &Ident,
    validate_id: &Ident,
    validate_id_rec: &Ident,
    predicate: &Option<proc_macro2::TokenStream>,
) -> proc_macro2::TokenStream {
    let doc_attrs: Vec<_> = spec
        .attributes
        .iter()
        .filter(|&attr| attr.path.is_ident("doc"))
        .cloned()
        .collect();
    let validate_id_rec_impl =
        implement_validation(spec, ty, predicate, validate_id_rec, validate_id);
    match predicate {
        Some(predicate) => quote! {
            #[inline(always)]
            #(#doc_attrs)*
            pub fn #id(&self) -> & #ty {
                &self.#field
            }
            #[allow(clippy::ptr_arg)]
            pub fn #validate_id(value: &#ty) -> bool {
                #predicate
            }
            #validate_id_rec_impl
            #(#doc_attrs)*
            pub fn #set_id(&mut self, mut value: #ty) -> Result<#ty, #ty> {
                if Self::#validate_id(&value) {
                    std::mem::swap(&mut self.#field, &mut value);
                    Ok(value)
                } else {
                    Err(value)
                }
            }
        },
        None => quote! {
            #[inline(always)]
            #(#doc_attrs)*
            pub fn #id(&self) -> & #ty {
                &self.#field
            }
            #validate_id_rec_impl
            #(#doc_attrs)*
            pub fn #set_id(&mut self, mut value: #ty) -> Result<#ty, #ty> {
                std::mem::swap(&mut self.#field, &mut value);
                Ok(value)
            }
        },
    }
}

fn keys_match(spec: &FieldSpec, field: &Ident, str_id: &str) -> proc_macro2::TokenStream {
    match spec.ty {
        FieldType::Concrete(_) => quote! {keys.push(#str_id.into());},
        FieldType::Structure(_) => quote! {
            keys.push(#str_id.into());
            keys.extend(self.#field.keys().into_iter().map(|s|format!("{}{}{}",#str_id, #SEPARATOR, s.as_str())));
        },
    }
}

fn get_match(
    spec: &FieldSpec,
    id: &Ident,
    str_id: &str,
    field: &Ident,
) -> proc_macro2::TokenStream {
    let get_exact = quote! {(#str_id, "") => Ok(self.#id() as &dyn Any),};
    if spec.recursive_accessors() {
        quote! {
            #get_exact
            (#str_id, key) => self.#field.get(key),
        }
    } else {
        get_exact
    }
}

fn json_get_match(
    spec: &FieldSpec,
    id: &Ident,
    str_id: &str,
    field: &Ident,
) -> proc_macro2::TokenStream {
    let get_exact = quote! {(#str_id, "") => serde_json::to_string(self.#id()).map_err(|e| validated_struct::GetError::Other(e.into())),};
    if spec.recursive_accessors() {
        quote! {
            #get_exact
            (#str_id, key) => self.#field.get_json(key),
        }
    } else {
        get_exact
    }
}

fn serde_match(
    id: &Ident,
    spec: &FieldSpec,
    str_id: &str,
    set_id: Ident,
    field: &Ident,
) -> proc_macro2::TokenStream {
    let serde_set_err = format!("Predicate rejected value for {}", id);
    let set_exact = quote! {
        (#str_id, "") => self.#set_id(serde::Deserialize::deserialize(value)?).is_err().then(||#serde_set_err.into()),
    };
    if spec.recursive_accessors() {
        quote! {
            #set_exact
            (#str_id, key) => self.#field.insert(key, value).err(),
        }
    } else {
        set_exact
    }
}

fn implement_validation(
    f: &FieldSpec,
    ty: &Type,
    predicate: &Option<proc_macro2::TokenStream>,
    validate_id_rec: &Ident,
    validate_id: &Ident,
) -> proc_macro2::TokenStream {
    if let FieldType::Structure(_) = f.ty {
        match predicate {
            Some(predicate) => quote! {
                fn #validate_id_rec(value: &#ty) -> bool {
                    value.validate_rec() && #predicate
                }
            },
            None => quote! {
                fn #validate_id_rec(value: &#ty) -> bool {
                    value.validate_rec()
                }
            },
        }
    } else {
        let validate_rec_inner = match *predicate {
            Some(_) => quote! {Self::#validate_id(value)},
            None => quote! {true},
        };
        quote! {
            #[allow(clippy::ptr_arg)]
            fn #validate_id_rec(value: &#ty) -> bool {
                #validate_rec_inner
            }
        }
    }
}

#[proc_macro]
pub fn validator(stream: TokenStream) -> TokenStream {
    let spec: StructSpec = syn::parse(stream).unwrap();
    let structure: Vec<_> = spec.flatten().iter().map(StructSpec::structure).collect();
    (quote! {
        #(#structure)*
    })
    .into()
}
mod display;
mod parsing;