typesift-macros 0.1.0

Implementation detail of the typesift crate: the #[derive(TypeSift)] macro
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
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::{
    Attribute, Data, DataEnum, DeriveInput, Error, Field, Fields, Index, Member, Path,
    parse_macro_input, parse_quote,
};

/// Derives `typesift::TypeSift`: the value itself is offered first, then each field is visited in
/// declaration order.
///
/// Every type parameter gets a `TypeSift` bound. Types with lifetime parameters are rejected
/// because `TypeSift` requires `'static` types, and unions because the active field is unknown.
///
/// Fields accept one argument, and none of them removes the bound on type parameters:
///
/// - `#[typesift(skip)]` leaves the field out of the traversal, so its type needs no impl.
/// - `#[typesift(leaf)]` offers the field itself but does not look inside it, so its type needs
///   only to be `'static`. This is how a type from another crate can still be found.
/// - `#[typesift(with = path)]` hands the field to a function generic over `typesift::Sifter`,
///   which decides what of it the search sees.
#[proc_macro_derive(TypeSift, attributes(typesift))]
pub fn derive_type_sift(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    expand(input)
        .unwrap_or_else(Error::into_compile_error)
        .into()
}

fn expand(mut input: DeriveInput) -> syn::Result<TokenStream> {
    reject_typesift_attributes(&input.attrs)?;

    if let Some(lifetime) = input.generics.lifetimes().next() {
        return Err(Error::new_spanned(
            lifetime,
            "`TypeSift` requires `'static` types, so it cannot be derived for types with lifetime parameters",
        ));
    }

    let body = match &input.data {
        Data::Struct(data) => visit_struct(&data.fields)?,
        Data::Enum(data) => visit_enum(data)?,
        Data::Union(data) => {
            return Err(Error::new_spanned(
                data.union_token,
                "`TypeSift` cannot be derived for unions",
            ));
        }
    };

    for param in input.generics.type_params_mut() {
        param.bounds.push(parse_quote!(::typesift::TypeSift));
    }

    let name = &input.ident;
    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();

    // Generic names are prefixed with `__` so they cannot clash with the type's own parameters.
    Ok(quote! {
        impl #impl_generics ::typesift::TypeSift for #name #ty_generics #where_clause {
            fn visit<'__a, __T: 'static, __B, __F>(
                &'__a self,
                __visitor: &mut __F,
            ) -> ::core::ops::ControlFlow<__B>
            where
                __F: ::core::ops::FnMut(&'__a __T) -> ::core::ops::ControlFlow<__B>,
            {
                ::typesift::TypeSift::visit_self::<__T, __B, __F>(self, __visitor)?;
                #body
            }
        }
    })
}

fn visit_struct(fields: &Fields) -> syn::Result<TokenStream> {
    let mut visits = Vec::new();
    for (index, field) in fields.iter().enumerate() {
        let mode = field_mode(field)?;
        if matches!(mode, FieldMode::Skip) {
            continue;
        }
        let member = match &field.ident {
            Some(ident) => Member::Named(ident.clone()),
            None => Member::Unnamed(Index::from(index)),
        };
        visits.push(mode.apply(&quote!(&self.#member)));
    }

    Ok(quote! {
        #(#visits)*
        ::core::ops::ControlFlow::Continue(())
    })
}

fn visit_enum(data: &DataEnum) -> syn::Result<TokenStream> {
    if data.variants.is_empty() {
        return Ok(quote!(match *self {}));
    }

    let mut arms = Vec::new();
    for variant in &data.variants {
        reject_typesift_attributes(&variant.attrs)?;

        // Skipped fields are matched with `_`, so they get neither a binding nor a visit.
        let mut patterns = Vec::new();
        let mut visits = Vec::new();
        for (index, field) in variant.fields.iter().enumerate() {
            let mode = field_mode(field)?;
            if matches!(mode, FieldMode::Skip) {
                patterns.push(quote!(_));
            } else {
                let binding = format_ident!("__field{}", index);
                visits.push(mode.apply(&quote!(#binding)));
                patterns.push(quote!(#binding));
            }
        }

        let variant_name = &variant.ident;
        let pattern = match &variant.fields {
            Fields::Named(fields) => {
                let names = fields.named.iter().map(|field| &field.ident);
                quote!(Self::#variant_name { #(#names: #patterns),* })
            }
            Fields::Unnamed(_) => quote!(Self::#variant_name(#(#patterns),*)),
            Fields::Unit => quote!(Self::#variant_name),
        };

        arms.push(quote! {
            #pattern => {
                #(#visits)*
                ::core::ops::ControlFlow::Continue(())
            }
        });
    }

    Ok(quote! {
        match self {
            #(#arms)*
        }
    })
}

/// What the derive does with one field.
enum FieldMode {
    /// Hand the field to its own `TypeSift` impl.
    Visit,
    /// Leave the field out entirely: `#[typesift(skip)]`.
    Skip,
    /// Offer the field without looking inside it: `#[typesift(leaf)]`.
    Leaf,
    /// Hand the field to a function of the user's: `#[typesift(with = path)]`.
    With(Path),
}

impl FieldMode {
    /// The argument that selected this mode, for error messages.
    fn name(&self) -> &'static str {
        match self {
            FieldMode::Visit => "",
            FieldMode::Skip => "skip",
            FieldMode::Leaf => "leaf",
            FieldMode::With(_) => "with",
        }
    }

    /// The code that visits one field. `field` must evaluate to a reference that lives as long as
    /// `self`.
    fn apply(&self, field: &TokenStream) -> TokenStream {
        match self {
            FieldMode::Visit => quote! {
                ::typesift::TypeSift::visit::<__T, __B, __F>(#field, __visitor)?;
            },
            // The field's type needs no impl here, only `'static`, so the type check that
            // `TypeSift::visit_self` performs is inlined instead of called.
            FieldMode::Leaf => quote! {
                if let ::core::option::Option::Some(__matched) =
                    (#field as &dyn ::core::any::Any).downcast_ref::<__T>()
                {
                    __visitor(__matched)?;
                }
            },
            // `__visitor` is reborrowed, so later fields can still use it.
            FieldMode::With(path) => quote! {
                #path(
                    #field,
                    &mut ::typesift::Sift::<__T, __B, __F>::new(&mut *__visitor),
                )?;
            },
            FieldMode::Skip => TokenStream::new(),
        }
    }
}

/// Reads the `typesift` attributes of one field.
fn field_mode(field: &Field) -> syn::Result<FieldMode> {
    let mut mode = FieldMode::Visit;
    for attr in typesift_attributes(&field.attrs) {
        attr.parse_nested_meta(|meta| {
            let found = if meta.path.is_ident("skip") {
                FieldMode::Skip
            } else if meta.path.is_ident("leaf") {
                FieldMode::Leaf
            } else if meta.path.is_ident("with") {
                FieldMode::With(meta.value()?.parse()?)
            } else {
                return Err(
                    meta.error("unknown `typesift` argument, expected `skip`, `leaf` or `with`")
                );
            };

            let conflict = match (&mode, &found) {
                (FieldMode::Visit, _) => None,
                (existing, found) if existing.name() == found.name() => {
                    Some(format!("duplicate `{}`", found.name()))
                }
                (existing, found) => Some(format!(
                    "`{}` and `{}` cannot be combined",
                    existing.name(),
                    found.name()
                )),
            };

            match conflict {
                Some(message) => Err(meta.error(message)),
                None => {
                    mode = found;
                    Ok(())
                }
            }
        })?;
    }
    Ok(mode)
}

/// `typesift` attributes only have a meaning on fields, so anywhere else they are an error.
fn reject_typesift_attributes(attrs: &[Attribute]) -> syn::Result<()> {
    match typesift_attributes(attrs).next() {
        Some(attr) => Err(Error::new_spanned(
            attr,
            "`#[typesift(...)]` is only allowed on fields",
        )),
        None => Ok(()),
    }
}

fn typesift_attributes(attrs: &[Attribute]) -> impl Iterator<Item = &Attribute> {
    attrs.iter().filter(|attr| attr.path().is_ident("typesift"))
}

#[cfg(test)]
mod tests {
    use syn::{DeriveInput, parse_quote};

    use super::expand;

    fn error_message(input: DeriveInput) -> String {
        expand(input)
            .expect_err("the derive should fail")
            .to_string()
    }

    fn generated_code(input: DeriveInput) -> String {
        expand(input)
            .expect("the derive should succeed")
            .to_string()
    }

    #[test]
    fn typesift_attribute_is_rejected_outside_fields() {
        let on_struct = parse_quote! {
            #[typesift(skip)]
            struct Session {
                user: u64,
            }
        };
        assert_eq!(
            error_message(on_struct),
            "`#[typesift(...)]` is only allowed on fields"
        );

        let on_variant = parse_quote! {
            enum Event {
                #[typesift(leaf)]
                Login(u64),
            }
        };
        assert_eq!(
            error_message(on_variant),
            "`#[typesift(...)]` is only allowed on fields"
        );
    }

    #[test]
    fn arguments_are_known_and_appear_once() {
        let unknown = parse_quote! {
            struct Session {
                #[typesift(rename)]
                user: u64,
            }
        };
        assert_eq!(
            error_message(unknown),
            "unknown `typesift` argument, expected `skip`, `leaf` or `with`"
        );

        let duplicate_skip = parse_quote! {
            struct Session {
                #[typesift(skip, skip)]
                user: u64,
            }
        };
        assert_eq!(error_message(duplicate_skip), "duplicate `skip`");

        let duplicate_leaf = parse_quote! {
            struct Session {
                #[typesift(leaf)]
                #[typesift(leaf)]
                user: u64,
            }
        };
        assert_eq!(error_message(duplicate_leaf), "duplicate `leaf`");

        let combined = parse_quote! {
            enum Event {
                Login(#[typesift(skip, leaf)] u64),
            }
        };
        assert_eq!(
            error_message(combined),
            "`skip` and `leaf` cannot be combined"
        );

        let without_arguments = parse_quote! {
            struct Session {
                #[typesift]
                user: u64,
            }
        };
        assert!(error_message(without_arguments).contains("expected attribute arguments"));
    }

    #[test]
    fn skipped_fields_are_left_out_of_the_generated_code() {
        let generated = generated_code(parse_quote! {
            struct Session {
                user: u64,
                #[typesift(skip)]
                cache: Cache,
            }
        });
        assert!(generated.contains("user"));
        assert!(!generated.contains("cache"));

        let generated = generated_code(parse_quote! {
            enum Event {
                Login { user: u64, #[typesift(skip)] token: Token },
            }
        });
        assert!(generated.contains("token : _"));
    }

    #[test]
    fn leaf_fields_are_checked_by_type_instead_of_visited() {
        let generated = generated_code(parse_quote! {
            struct Invoice {
                #[typesift(leaf)]
                id: Uuid,
                amount: u64,
            }
        });
        assert!(generated.contains("downcast_ref"));
        assert!(generated.contains("self . id"));
        // The field is offered, never walked into.
        assert!(!generated.contains("visit :: < __T , __B , __F > (& self . id"));
        assert!(generated.contains("visit :: < __T , __B , __F > (& self . amount"));
    }

    #[test]
    fn with_takes_a_path_and_no_other_argument() {
        let duplicate = parse_quote! {
            struct Request {
                #[typesift(with = visit_headers, with = visit_headers)]
                headers: Headers,
            }
        };
        assert_eq!(error_message(duplicate), "duplicate `with`");

        let combined = parse_quote! {
            struct Request {
                #[typesift(skip, with = visit_headers)]
                headers: Headers,
            }
        };
        assert_eq!(
            error_message(combined),
            "`skip` and `with` cannot be combined"
        );

        let without_a_path = parse_quote! {
            struct Request {
                #[typesift(with)]
                headers: Headers,
            }
        };
        assert!(error_message(without_a_path).contains("expected"));

        let generated = generated_code(parse_quote! {
            struct Request {
                path: String,
                #[typesift(with = visit_headers)]
                headers: Headers,
            }
        });
        assert!(generated.contains("visit_headers"));
        assert!(generated.contains("Sift"));
        assert!(generated.contains("visit :: < __T , __B , __F > (& self . path"));
    }
}