oxinat_derive 0.8.0

oxinat xapi-oxidized procedural macros
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
use std::collections::HashSet;
use proc_macro::TokenStream as TokenStream1;

use attribute_derive::FromAttr;
use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{
    parse_macro_input, parse_str, punctuated::Punctuated, Attribute, Data, DataEnum, DataStruct, DeriveInput, Expr, Fields, GenericArgument, Ident, PathArguments, Type
};

use crate::Attributes;

macro_rules! new_ambiguous_ident {
    ($e:expr, $($vars:expr),+ $(,)?) => {
        Ident::new(
            &format!($e, $($vars,)+),
            Span::call_site()
        )
    };
    ($e:expr) => {
        Ident::new($e, Span::call_site())
    }
}

/// Represents attributes passed to `UriBuilder`
/// for building some URI path
#[derive(FromAttr, Debug)]
#[attribute(ident = match_path)]
#[attribute(error(missing_field = "`{field}` not specified"))]
struct MatchPatternAttrs {
    path:     String,
    requires: Option<String>,
}

/// Represents attributes passed to `UriBuilder`
/// for building path patterns.
#[derive(Debug, PartialEq, Eq)]
pub struct MatchPatternAttrsParsed {
    pub path:     String,
    pub params:   Vec<ParamAttrsParsed>,
    pub requires: Option<Expr>
}

/// Represents attributes passed to the fields of
/// a `UriBuilder` implemented struct.
#[derive(FromAttr, Debug, Default)]
#[attribute(ident = param, aliases=[parent, root])]
#[attribute(error(missing_field = "`{field}` not specified"))]
struct ParamAttrs {
    pub name:      Option<String>,
    pub map_from:  Option<String>,
    pub requires:  Option<String>,
    pub is_parent: Option<bool>,
}

/// Public facing struct from `ParamAttrs`
/// parsing.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ParamAttrsParsed {
    pub field_name: Ident,
    pub name:       String,
    pub kind:       Type,
    pub map_from:   Option<Expr>,
    pub requires:   Option<Expr>,
    pub is_option:  bool,
    pub is_param:   bool,
    pub is_parent:  bool,
}

/// Build a derived implementation of the target
/// struct or enum type for a `UriBuilder`.
pub fn build(input: TokenStream1) -> TokenStream1 {
    let input = parse_macro_input!(input as DeriveInput);
    match &input.data {
        Data::Struct(data) => build_struct(&input, data),
        Data::Enum(data)   => build_enum(&input, data),
        _ => panic!("unions are not supported")
    }.into()
}

fn build_enum(input: &DeriveInput, data: &DataEnum) -> TokenStream {
    let mut match_arms = quote! {};
    data.variants.iter().for_each(|variant| {
        let params      = parse_params(&variant.fields);
        let match_paths = parse_paths(&variant.attrs, &params);
        validate_paths_by_params(&match_paths, &params);

        let ident = &variant.ident;
        if params.is_empty() {
            // We will treat non-parameterized
            // variants with a general,
            // homogenous, case.
            let default_uri = ident.to_string().to_lowercase();
            match_arms.extend(quote! { Self::#ident => #default_uri.to_string(), });
            return;
        }
        for pattern in match_paths {
            let path = &pattern.path;
            let mut lhs = quote! {};
            let rhs = quote! { format!(#path) };

            params.iter().enumerate().for_each(|(idx, p)| {
                let index_name = new_ambiguous_ident!("p{}", idx);
                if p.is_option && pattern.params.contains(p) {
                    lhs.extend(quote! { Some(#index_name), })
                } else if p.is_option {
                    lhs.extend(quote! { None, })
                } else {
                    lhs.extend(quote! { #index_name, })
                }
            });
            match_arms.extend(quote! { Self::#ident(#lhs) => #rhs, })
        }
    });

    let (ident, generics) = (&input.ident, &input.generics);
    let where_clause      = &generics.where_clause;
    let crate_ident       = crate::get_crate_ident();

    let gen = quote! {
        impl #generics #crate_ident::UriBuilder for #ident #generics #where_clause {
            fn build(&self) -> crate::BuildResult {
                Ok(self.to_string().into())
            }
        }

        impl #generics std::fmt::Display for #ident #generics #where_clause {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
            {
                write!(f, "{}", match self {
                    #match_arms
                })
            }
        }
    };
    gen
}

/// Implements necessary methods for a struct to
/// qualify as a `UriBuilder`.
fn build_struct(input: &DeriveInput, data: &DataStruct) -> TokenStream {
    let crate_ident  = crate::get_crate_ident();
    let where_clause = &input.generics.where_clause;
    let params       = parse_params(&data.fields);
    let match_paths  = parse_paths(&input.attrs, &params);
    validate_paths_by_params(&match_paths, &params);

    let (ident, generics) = (&input.ident, &input.generics);
    let match_arms = build_matches(&match_paths, &params);

    let mut gen = quote! {
        impl #generics #crate_ident::UriBuilder for #ident #generics #where_clause {
            fn build(&self) -> #crate_ident::BuildResult {
                match self {
                    #match_arms
                }
            }
        }
    };
    gen.extend(build_methods(input, &params));
    // Impl `std::fmt::Display` to qualify
    // builder for being the potential victim of
    // being joined as a parent builder.
    gen.extend(quote! {
        impl #generics std::fmt::Display for #ident #generics #where_clause {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                write!(f, "{}", self
                    .build()
                    .expect("build must produce a string"))
            }
        }
    });
    gen
}

/// Builds the match arms for the `build` URI
/// builder table.
fn build_matches(paths: &[MatchPatternAttrsParsed], params: &[ParamAttrsParsed]) -> TokenStream {
    let mut match_arms = quote! {};
    for pattern in paths {
        // Edge case where a pattern presented
        // requires no parameters.
        if pattern.params.is_empty() {
            match_arms.extend(build_match_arm_shallow(pattern, params));
        // Inclusive formatting of a pattern where
        // fields present themselves as `Some` and
        // excluding param fields that are `None`
        } else {
            match_arms.extend(build_match_arm(pattern, params));
        }
    }
    match_arms.extend(quote! {
        _ => {
            Err(crate::uri::UriBuildError::UnrecognizedPattern.into())
        }
    });

    match_arms
}

/// Performs a deep construction of a match arm
/// used for the `match` statement responsible for
/// the eventual building of a `URI` from the 
/// implementation of a `UriBuilder`.
fn build_match_arm(pattern: &MatchPatternAttrsParsed, params: &[ParamAttrsParsed]) -> TokenStream {
    let path = &pattern.path;
    let mut lhs = quote! {};
    let mut rhs = quote! {};
    // We must break out the inner impl of the RHS
    // in order to allow field mapping where it is
    // required.
    let mut rhs_inner = quote! {};

    params.iter().filter(|p| p.is_param).enumerate().for_each(|(idx, p)| {
        let field_name = &p.field_name;
        let param_name = new_ambiguous_ident!(&p.name);
        let index_name = new_ambiguous_ident!("p{}", idx);
        // Simply apply the parameter field if no
        // special formatting is required.
        if !pattern.params.contains(p) && p.is_option {
            lhs.extend(quote! { #field_name: None, });
            return
        } else if !pattern.params.contains(p) {
            return
        }

        // Apply mapper function to format a value
        // from user-defined func.
        if let Some(mf) = &p.map_from {
            lhs.extend(quote! { #field_name: Some(#index_name), });
            rhs_inner.extend(quote! {
                let mapper = #mf; let #param_name = mapper(#index_name);
            });
        } else {
            lhs.extend(quote! { #field_name: Some(#param_name), });
        }

        if let Some(rq) = &p.requires {
            rhs_inner.extend(quote! {
                if !#rq(#param_name) {
                    return Err(crate::uri::UriBuildError::Validation.into())
                }
            })
        }
    });

    let mut conditional = quote! {};
    if let Some(rq) = &pattern.requires {
        conditional.extend(quote! { if #rq(self) })
    }

    // Finalize RHS after determination of
    // whether extra formatting rules are
    // required.
    rhs.extend(quote! {
        {
            #rhs_inner format!(#path)
        }
    });
    // Round off match pattern by ignoring any
    // non-parameter fields.
    lhs.extend(quote! { .. });
    quote! { Self { #lhs } #conditional => Ok(#rhs), }
}

/// Performs a shallow construction of a match arm
/// used for the `match` statement responsible for
/// the eventual building of a `URI` from the 
/// implementation of a `UriBuilder`.
fn build_match_arm_shallow(pattern: &MatchPatternAttrsParsed, params: &[ParamAttrsParsed]) -> TokenStream {
    let path = &pattern.path;
    let mut lhs = quote! {};
    let rhs = quote! { String::from(#path) };
    params.iter().filter(|p| p.is_param).for_each(|p| {
        let field_name = &p.field_name;
        lhs.extend(quote! { #field_name: None, })
    });
    lhs.extend(quote! { .. });
    quote! { Self { #lhs } => Ok(#rhs), }
}

/// Builds the `with_xx` methods associated with a
/// `UriBuilder` derivitive implmentation. Allows
/// for pre-construction declaration of
/// parameters.
fn build_methods(input: &DeriveInput, params: &[ParamAttrsParsed]) -> TokenStream {
    let mut with_methods = quote! {};
    params.iter().for_each(|param| {
        let method_name = new_ambiguous_ident!("with_{}", &param.name);
        let field_name  = &param.field_name;
        let kind        = &param.kind;

        if param.is_parent {
            with_methods.extend(quote! {
                /// Generated method to set the
                /// `#field_name` of `#ident`
                pub fn with_parent(mut self, value: #kind) -> Self {
                    self.#field_name = Some(value);
                    self
                }
                /// Creates a new instance of this
                /// type, presetting the parent
                /// to the passed value.
                pub fn from_parent(value: #kind) -> Self
                where
                    Self: Default,
                {
                    Self::default().with_parent(value)
                }
            })
        } else if param.is_option {
            with_methods.extend(quote! {
                /// Generated method to set the
                /// `#field_name` of `#ident`
                pub fn #method_name<V: Clone + Into<#kind>>(mut self, value: V) -> Self {
                    self.#field_name = Some(value.to_owned().into());
                    self
                }
            })
        } else {
            with_methods.extend(quote! {
                pub fn #method_name<V: Clone + Into<#kind>>(mut self, value: V) -> Self {
                    self.#field_name = value.to_owned().into();
                    self
                }
            })
        }
    });

    let (ident, generics) = (&input.ident, &input.generics);
    let where_clause      = &generics.where_clause;
    quote! {
        impl #generics #ident #generics #where_clause {
            #with_methods
        }
    }
}

/// Filters out attributes that are not
/// `match_path` attribute modifiers.
fn filter_match_paths(attr: &&Attribute) -> bool {
    attr.meta.path().segments[0].ident == "match_path"
}

/// Filters out attributes that are not `param` or
/// `parent` attribute modifiers
fn filter_params(attr: &&Attribute) -> bool {
    ["param", "parent"]
        .contains(&attr.meta.path().segments[0].ident.to_string().as_str())
}

/// Determines if the attribute is a `parent`
/// attribute modifier.
fn is_parent(attr: &Attribute) -> bool {
    attr.meta.path().segments[0].ident == "parent"
}

/// Determines if the type is an `Option`
/// declaration.
fn is_optional_type(kind: &Type) -> bool {
    match &kind {
        Type::Path(tp) => tp.path.segments[0].ident == "Option",
        _ => panic!("expected a type path")
    }
}

/// Extrudes the nested type declaration from an
/// option declaration.
fn parse_optional_type(kind: &Type) -> &Type {
    // If the initial `kind` parsed from
    // the field is an Option, we want
    // to instead get the underlying type.
    match &kind {
        Type::Path(tp) => {
            // Assuming the syntax is the
            // expected pattern, we can
            // then extract the underlying
            // type.
            match &tp.path.segments[0].arguments {
                PathArguments::AngleBracketed(ab) => {
                    match &ab.args[0] {
                        GenericArgument::Type(ty) => ty,
                        _ => panic!("expected a type")
                    }
                },
                _ => panic!("unexpected syntax")
            }
        },
        _ => unreachable!()
    }
}

/// Parse the param declared fields on a
/// `UriBuilder` derived implemenation.
fn parse_params(fields: &Fields) -> Vec<ParamAttrsParsed> {
    match fields {
        Fields::Named(f) => {
            f.named.to_owned()
        },
        Fields::Unit => Punctuated::new(),
        Fields::Unnamed(f) => {
            f.unnamed.to_owned()
        }
    }
    .iter()
    .enumerate()
    .map(|(idx, f)| {
        let ident = f
        .ident
        .clone()
        .unwrap_or(new_ambiguous_ident!("p{}", idx));
    let is_param = !f
        .attrs
        .iter()
        .filter(filter_params)
        .collect::<Vec<_>>()
        .is_empty();
    let attrs = f
            .attrs
            .iter()
            .find(filter_params)
            .and_then(|a| {
                let mut parsed = ParamAttrs::from_attribute(a)
                    .unwrap();
                parsed.is_parent = is_parent(a).into();
                parsed.into()
            });
        let attrs = attrs.unwrap_or_default();
        let kind  = f.ty.clone();

        let is_parent = attrs.is_parent.unwrap_or_default();
        let is_option = is_optional_type(&kind);
        let name = attrs
            .name
            .as_ref().unwrap_or(&ident.to_string())
            .to_owned();
        let kind = if is_option {
            parse_optional_type(&kind)
        } else {
            &kind
        }.to_owned();
        let map_from = attrs
            .map_from
            .as_ref()
            .map(|mf| parse_str::<Expr>(mf).expect("must be a parsable expression"));
        let requires = attrs
            .requires
            .as_ref()
            .map(|rq| parse_str::<Expr>(rq).expect("must be a parsable expression"));

        ParamAttrsParsed {
            field_name: ident,
            name,
            kind,
            map_from,
            requires,
            is_option,
            is_parent,
            is_param,
        }
    })
    .collect()
}


/// Parse the path patterns declared on a
/// `UriBuilder` derived implementation.
fn parse_paths(attrs: &Attributes, params: &[ParamAttrsParsed]) -> Vec<MatchPatternAttrsParsed> {
    attrs
        .iter()
        .filter(filter_match_paths)
        .map(MatchPatternAttrs::from_attribute)
        .map(|a| {
            let a = a.unwrap();
            let requires = a
                .requires
                .map(|rq| parse_str::<Expr>(&rq).expect("must be a parsable expression"));
            let mut parsed = MatchPatternAttrsParsed{
                path: a.path,
                params: vec![],
                requires
            };

            // Pair parameter metadata to the
            // path.
            parsed
                .path
                .split('/')
                .filter(|p| p.contains(['{', '}']))
                .map(|param_name| param_name.replace(['{', '}'], ""))
                .for_each(|param_name| {
                    let found = params
                        .iter()
                        .find(|p| p.name == param_name);
                    if let Some(f) = found {
                        parsed.params.push(f.to_owned())
                    }
                });

            parsed
        })
        .collect()
}

/// Validate all params have been declared
/// inline with path patterns.
fn validate_paths_by_params(paths: &[MatchPatternAttrsParsed], params: &[ParamAttrsParsed]) {
    let mut param_map = HashSet::new();
    for match_path in paths.iter() {
        match_path.path.split('/').for_each(|p| {
            if p.contains(['{', '}']) {
                param_map.insert(p.replace(['{', '}'], ""));
            }
        })
    }
    if !param_map.iter().all(|n| params.iter().any(|p| p.name == *n)) {
        let fields = param_map
            .iter()
            .filter(|n| !params.iter().any(|p| p.name == **n))
            .map(|p| p.to_owned())
            .collect::<Vec<_>>()
            .join(", ");
        panic!("missing parameter(s) declared in path patterns: ({fields})")
    }
}