File: parse_attributes.rs

package info (click to toggle)
rust-deluxe-macros 0.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 276 kB
  • sloc: makefile: 4
file content (362 lines) | stat: -rw-r--r-- 12,372 bytes parent folder | download | duplicates (2)
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
use std::borrow::Cow;

use deluxe_core::Errors;
use proc_macro2::{Span, TokenStream};

use crate::types::*;

#[derive(PartialEq, Eq, Clone, Copy)]
pub enum Mode {
    Parse,
    Extract,
}

impl Mode {
    fn into_token_mode(self) -> TokenMode {
        match self {
            Self::Parse => TokenMode::ParseAttributes,
            Self::Extract => TokenMode::ExtractAttributes,
        }
    }
}

struct AttrImpl<'i> {
    pub parse: TokenStream,
    pub crate_path: syn::Path,
    pub priv_path: syn::Path,
    pub attributes: Vec<syn::Path>,
    pub container_field: Option<&'i syn::Field>,
    pub container_lifetime: Option<syn::Lifetime>,
    pub container_ty: Option<syn::Type>,
}

#[inline]
fn impl_for_struct<'i>(
    input: &'i syn::DeriveInput,
    struct_: &syn::DataStruct,
    mode: Mode,
    errors: &Errors,
) -> Option<AttrImpl<'i>> {
    let struct_attr = errors.push_result(<Struct as deluxe_core::ParseAttributes<
        syn::DeriveInput,
    >>::parse_attributes(input));

    let crate_path = super::get_crate_path(struct_attr.as_ref().map(|s| s.crate_.clone()), errors)?;
    let crate_ = &crate_path;
    let priv_path: syn::Path = syn::parse_quote! { #crate_::____private };
    let priv_ = &priv_path;

    let parse = struct_attr
        .as_ref()
        .map(|s| {
            let ItemDef { inline, .. } = s.to_parsing_tokens(
                input,
                crate_,
                mode.into_token_mode(),
                &parse_quote_mixed! { inline(input) },
                &parse_quote_mixed! { allowed },
            );
            let pre = match &struct_.fields {
                syn::Fields::Named(_) => {
                    let field_names = struct_attr
                        .as_ref()
                        .map(|s| s.to_field_names_tokens(crate_, priv_))
                        .unwrap_or_else(|| quote_mixed! { &[] });
                    let accepts_all = struct_attr
                        .as_ref()
                        .and_then(|s| s.to_accepts_all_tokens(crate_))
                        .unwrap_or_else(|| quote_mixed! { false });
                    quote_mixed! {
                        let allowed = #field_names;
                        let validate = !(#accepts_all);
                        let prefix = "";
                    }
                }
                syn::Fields::Unnamed(_) => {
                    quote_mixed! {
                        let mut index = 0usize;
                    }
                }
                _ => quote::quote! {},
            };
            quote_mixed! {
                #pre
                #inline
            }
        })
        .unwrap_or_else(|| {
            quote_mixed! {
                #priv_::unreachable!()
            }
        });
    let (container_field, container_lifetime, container_ty) = struct_attr
        .as_ref()
        .map(|s| s.fields.as_slice())
        .unwrap_or_default()
        .iter()
        .find_map(|f| {
            f.container
                .as_ref()
                .map(|c| (Some(f.field), c.lifetime.clone(), c.ty.clone()))
        })
        .unwrap_or_default();
    Some(AttrImpl {
        parse,
        crate_path,
        priv_path,
        attributes: struct_attr.map(|s| s.attributes).unwrap_or_default(),
        container_field,
        container_lifetime,
        container_ty,
    })
}

#[inline]
fn impl_for_enum<'i>(
    input: &'i syn::DeriveInput,
    mode: Mode,
    errors: &Errors,
) -> Option<AttrImpl<'i>> {
    let enum_attr = errors.push_result(
        <Enum as deluxe_core::ParseAttributes<syn::DeriveInput>>::parse_attributes(input),
    );

    let crate_path = super::get_crate_path(enum_attr.as_ref().map(|e| e.crate_.clone()), errors)?;
    let crate_ = &crate_path;
    let priv_path: syn::Path = syn::parse_quote! { #crate_::____private };
    let priv_ = &priv_path;

    let parse = enum_attr
        .as_ref()
        .map(|e| {
            let parse = e.to_inline_parsing_tokens(crate_, mode.into_token_mode());
            let field_names = e.to_field_names_tokens(crate_, priv_);
            let accepts_all = e
                .to_accepts_all_tokens(crate_)
                .unwrap_or_else(|| quote_mixed! { false });
            quote_mixed! {
                let allowed = #field_names;
                let validate = !(#accepts_all);
                let prefix = "";
                #parse
            }
        })
        .unwrap_or_else(|| {
            quote_mixed! {
                #priv_::unreachable!()
            }
        });

    let (container_field, container_lifetime, container_ty) = enum_attr
        .as_ref()
        .map(|e| e.variants.as_slice())
        .unwrap_or_default()
        .iter()
        .find_map(|v| {
            v.fields.iter().find_map(|f| {
                f.container
                    .as_ref()
                    .map(|c| (Some(f.field), c.lifetime.clone(), c.ty.clone()))
            })
        })
        .unwrap_or_default();
    Some(AttrImpl {
        parse,
        crate_path,
        priv_path,
        attributes: enum_attr.map(|s| s.attributes).unwrap_or_default(),
        container_field,
        container_lifetime,
        container_ty,
    })
}

pub fn impl_parse_attributes(input: syn::DeriveInput, errors: &Errors, mode: Mode) -> TokenStream {
    let attr = match &input.data {
        syn::Data::Struct(struct_) => impl_for_struct(&input, struct_, mode, errors),
        syn::Data::Enum(_) => impl_for_enum(&input, mode, errors),
        syn::Data::Union(union_) => {
            errors.push_spanned(
                union_.union_token,
                match mode {
                    Mode::Parse => "union not supported with derive(ParseAttributes)",
                    Mode::Extract => "union not supported with derive(ExtractAttributes)",
                },
            );
            return Default::default();
        }
    };
    let AttrImpl {
        parse,
        crate_path: crate_,
        priv_path: priv_,
        attributes,
        container_field,
        mut container_lifetime,
        container_ty,
    } = match attr {
        Some(a) => a,
        None => return Default::default(),
    };

    let ident = &input.ident;
    let mut generics = input.generics.clone();

    let mut container_is_generic = false;
    let mut container_is_ref = false;
    let mut container_ty = container_ty.map(Cow::Owned);
    if let Some(container_field) = container_field {
        let mut ty = &container_field.ty;
        // try to guess some things about the container type.
        // first infer if this is an option, and if so, use its inner type
        if_chain::if_chain! {
            if let syn::Type::Path(path) = ty;
            if path.qself.is_none();
            if let Some(last) = path.path.segments.last();
            if last.ident == "Option";
            if let syn::PathArguments::AngleBracketed(args) = &last.arguments;
            if args.args.len() == 1;
            if let syn::GenericArgument::Type(t) = &args.args[0];
            then {
                ty = t;
            }
        }
        // use inner type and lifetime from reference
        if let syn::Type::Reference(ref_) = ty {
            if container_lifetime.is_none() {
                container_lifetime = ref_.lifetime.clone();
            }
            container_is_ref = true;
            ty = &*ref_.elem;
        }
        // if we still need a lifetime, and inner type has a lifetime, use that
        if_chain::if_chain! {
            if container_lifetime.is_none();
            if let syn::Type::Path(path) = ty;
            if let Some(last) = path.path.segments.last();
            if let syn::PathArguments::AngleBracketed(args) = &last.arguments;
            if let Some(syn::GenericArgument::Lifetime(lt)) = args.args.first();
            then {
                container_lifetime = Some(lt.clone());
            }
        }
        // see if the type matches a generic param
        if container_ty.is_none() {
            container_ty = Some(Cow::Borrowed(ty));
            if_chain::if_chain! {
                if let syn::Type::Path(path) = ty;
                if path.qself.is_none();
                if let Some(ident) = path.path.get_ident();
                if generics.type_params().any(|p| p.ident == *ident);
                then {
                    container_is_generic = true;
                }
            }
        }
    }
    // if there was no container field, make it generic and add our own type param
    let container_ty = container_ty.unwrap_or_else(|| {
        container_is_generic = true;
        let mut ty = String::from("T");
        // ensure the `T` is a unique ident
        while generics.type_params().any(|p| p.ident == ty) {
            ty.push('_');
        }
        let ty = syn::Ident::new(&ty, Span::mixed_site());
        generics.params.insert(0, syn::parse_quote! { #ty });
        Cow::Owned(parse_quote_mixed! { #ty })
    });

    // value types must be copied into the structure
    if container_field.is_some() && !container_is_ref {
        let where_clause = generics.make_where_clause();
        where_clause.predicates.push(syn::parse_quote! {
            #container_ty: #priv_::Clone
        });
    }

    // must be able to access attributes on the generic type
    if container_is_generic {
        let where_clause = generics.make_where_clause();
        where_clause.predicates.push(syn::parse_quote! {
            #container_ty: #crate_::HasAttributes
        });
    }

    // ParseAttributes needs a lifetime param since the attributes are always referenced
    if mode == Mode::Parse && container_lifetime.is_none() {
        let mut lt = String::from("t");
        while generics.lifetimes().any(|l| l.lifetime.ident == lt) {
            lt.push('_');
        }
        lt.insert(0, '\'');
        container_lifetime = Some(syn::Lifetime::new(&lt, Span::mixed_site()));
        generics
            .params
            .insert(0, syn::parse_quote! { #container_lifetime });
    }

    let (_, type_generics, _) = input.generics.split_for_impl();
    let (impl_generics, _, where_clause) = generics.split_for_impl();

    let matches = if attributes.is_empty() {
        quote_mixed! { true }
    } else {
        let matches = attributes.iter().map(|p| {
            let segs = p.segments.iter().map(|s| s.ident.to_string());
            quote_mixed! { &[#(#segs),*] }
        });
        quote_mixed! {
            #(#priv_::parse_helpers::path_matches(path, #matches))||*
        }
    };

    let sig = match mode {
        Mode::Parse => quote_mixed! {
            fn parse_attributes(obj: &#container_lifetime #container_ty) -> #crate_::Result<Self>
        },
        Mode::Extract => quote_mixed! {
            fn extract_attributes(obj: &mut #container_ty) -> #crate_::Result<Self>
        },
    };
    let trait_ = match mode {
        Mode::Parse => quote_mixed! {
            #crate_::ParseAttributes<#container_lifetime, #container_ty>
        },
        Mode::Extract => quote_mixed! {
            #crate_::ExtractAttributes<#container_ty>
        },
    };
    let get_tokens = match mode {
        Mode::Parse => quote_mixed! { ref_tokens },
        Mode::Extract => quote_mixed! { take_tokens },
    };
    let tokens_try = match mode {
        Mode::Parse => None,
        Mode::Extract => Some(quote_mixed! { ? }),
    };
    let path_name_unwrap = attributes.first().map(|path| {
        let path = deluxe_core::parse_helpers::key_to_string(path);
        quote_mixed! { .or(#priv_::Option::Some(#path)) }
    });

    quote_mixed! {
        impl #impl_generics #trait_ for #ident #type_generics #where_clause {
            #[inline]
            fn path_matches(path: &#priv_::Path) -> #priv_::bool {
                #matches
            }
            #sig {
                #priv_::parse_helpers::parse_struct_attr_tokens(
                    #priv_::parse_helpers::#get_tokens::<Self, _>(obj) #tokens_try,
                    |inputs, spans| {
                        let span = #priv_::parse_helpers::first_span(spans);
                        let path_name = #priv_::parse_helpers::first_path_name(spans) #path_name_unwrap;
                        let _mode = #crate_::ParseMode::Named(span);
                        #parse
                    }
                )
            }
        }
    }
}