rust-utils-macros 0.1.0

Procedural macros for the rust-utils crate
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
use super::{
    ok_or_compile_err,
    some_or_compile_err,
    gen_doc_attrs,
    quote_compile_err
};
use if_chain::if_chain;
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use syn::{
    parse::Parser,
    parse_quote,
    punctuated::Punctuated,
    spanned::Spanned,
    Attribute,
    Error as ParseError,
    Expr, FnArg,
    GenericArgument,
    Ident, ImplItemFn,
    ItemStruct, Lit,
    Meta, PathArguments,
    Result as ParseResult,
    Signature, Stmt,
    Token, TraitItemFn,
    Type, TypePath,
    Visibility
};
use quote::{quote, ToTokens};

// options for the #[chainable] attribute
#[derive(Clone)]
pub struct ChainableOptions {
    pub collapse_options: bool,
    pub use_into_impl: bool,
    pub doc_attrs: Vec<Attribute>
}

impl ChainableOptions {
    pub fn parse<T: Into<TokenStream2>>(is_field: bool, attr_args: T) -> ParseResult<Self> {
        let attr_args = attr_args.into();
        let mut doc_attrs = Vec::new();

        if attr_args.is_empty() {
            Ok(
                Self {
                    collapse_options: false,
                    use_into_impl: false,
                    doc_attrs: vec![]
                }
            )
        }
        else {
            let collapse_options_option = if is_field {
                "collapse_option"
            }
            else {
                "collapse_options"
            };

            let mut collapse_options = false;
            let mut use_into_impl = false;
            let options = <Punctuated<Meta, Token![,]>>::parse_terminated.parse2(attr_args.clone())?;

            for option in options {
                if let Meta::Path(path) = &option {
                    let name = path.require_ident()?;
                    if name == collapse_options_option {
                        collapse_options = true;
                    }
                    else if name == "use_into_impl" {
                        use_into_impl = true;
                    }
                    else {
                        let msg = format!(
                            "Unknown option \"{name}\"! Valid options are \
                            \"{collapse_options_option}\", \"use_into_impl\" \
                            and \"doc\" (struct fields only)"
                        );
                        return Err(ParseError::new(name.span(), msg));
                    }
                }
                else if let Meta::NameValue(meta_val) = &option {
                    let name = meta_val.path.require_ident()?;

                    if name == "doc" && is_field {
                        if_chain! {
                            if let Expr::Lit(literal) = &meta_val.value;
                            if let Lit::Str(str_val) = &literal.lit;

                            then {
                                doc_attrs = gen_doc_attrs(str_val.value());
                            }
                            else {
                                return Err(ParseError::new(attr_args.span(), "Invalid Input!"));
                            }
                        }
                    }
                    else {
                        let msg = format!(
                            "Unknown option \"{name}\"! Valid options are \
                            \"{collapse_options_option}\", \"use_into_impl\" \
                            and \"doc\" (struct fields only)"
                        );
                        return Err(ParseError::new(name.span(), msg));
                    }
                }
                else {
                    return Err(ParseError::new(attr_args.span(), "Invalid Input!"));
                }
            }

            Ok(Self {
                collapse_options,
                use_into_impl,
                doc_attrs
            })
        }
    }

    pub fn gen_method_from_field(&self, name: &Ident, field_type: &Type, type_vis: &Visibility) -> ImplItemFn {
        let mut in_arg: FnArg = parse_quote!(#name:#field_type);
            let mut set_stmt: Stmt = parse_quote!(self.#name = #name;);

            if self.collapse_options {
                if_chain! {
                    if let Type::Path(TypePath { path, .. }) = &field_type;
                    if let Some(type_segment) = path.segments.last();
                    if type_segment.ident == "Option";
                    if let PathArguments::AngleBracketed(type_args) = &type_segment.arguments;
                    if let GenericArgument::Type(inner_type) = type_args.args.first().unwrap();

                    then {
                        if self.use_into_impl {
                            in_arg = parse_quote!(#name:impl core::convert::Into<#inner_type>);
                            set_stmt = parse_quote!(self.#name = Some(#name.into());)
                        }
                        else {
                            in_arg = parse_quote!(#name:#inner_type);
                            set_stmt = parse_quote!(self.#name = Some(#name);)
                        }
                    }
                }
            }
            else if self.use_into_impl {
                in_arg = parse_quote!(#name:impl core::convert::Into<#field_type>);
                set_stmt = parse_quote!(self.#name = #name.into(););
            }

            let attrs_to_add = &self.doc_attrs;

            parse_quote! {
                #(#attrs_to_add)*
                #[must_use]
                #type_vis fn #name(mut self, #in_arg) -> Self {
                    #set_stmt
                    self
                }
            }
    }
}

pub fn chainable_struct_fields(mut struct_def: ItemStruct) -> TokenStream2 {
    // the generated methods
    let mut methods: Vec<ImplItemFn> = Vec::new();

    for field in &mut struct_def.fields {
        let field_name = some_or_compile_err!(field.ident.as_ref(), "Tuple structs are not supported");
        let mut field_attrs = Vec::new();
        let mut options_tokens = TokenStream2::new();

        // does the caller have any attributes on any of the fields?
        // if yes, process and remove them
        for attr in field.attrs.drain(..) {
            let mut my_attr = false;

            if let Meta::List(list) = &attr.meta {
                let name = ok_or_compile_err!(list.path.require_ident());

                if name == "chainable" {
                    options_tokens = list.tokens.clone();
                    my_attr = true;
                }
            }

            if !my_attr {
                field_attrs.push(attr);
            }
        }

        let options = ok_or_compile_err!(ChainableOptions::parse(true, options_tokens));

        methods.push(
            options.gen_method_from_field(field_name, &field.ty, &struct_def.vis)
        );

        field.attrs = field_attrs;
    }

    let type_name = &struct_def.ident;
    let (impl_generics, ty_generics, where_clause) = &struct_def.generics.split_for_impl();

    quote! {
        #struct_def

        impl #impl_generics #type_name #ty_generics #where_clause {
            #(#methods)*
        }
    }
}

pub fn chainable_trait_method(method: TraitItemFn, attr_args: TokenStream) -> TokenStream2 {
    let TraitItemFn {
        mut attrs,
        mut sig,
        ..
    } = method.clone();

    let options = ok_or_compile_err!(ChainableOptions::parse(false, attr_args));
    let fn_name = sig.ident.clone();
    let fn_name_str = fn_name.to_string();

    if fn_name_str.starts_with("set_") || fn_name_str.starts_with("add_") {
        let doc_attrs = extract_doc_attrs(&mut attrs);

        // set the name of the function
        sig.ident = ok_or_compile_err!(syn::parse_str(&fn_name_str[4..]));

        let args = process_fn_signature(options, &mut sig);

        quote! {
            #method

            #(#doc_attrs)*
            #(#attrs)*
            #[must_use]
            #sig {
                self.#fn_name(#(#args),*);
                self
            }
        }
    }
    else {
        quote_compile_err!("The method must start with \"set_\" or \"add_\"!")
    }
}

pub fn chainable_inh_method(method: ImplItemFn, attr_args: TokenStream) -> TokenStream2 {
    let ImplItemFn {
        mut attrs,
        vis,
        defaultness,
        mut sig,
        ..
    } = method.clone();

    let options = ok_or_compile_err!(ChainableOptions::parse(false, attr_args));
    let fn_name = sig.ident.clone();
    let fn_name_str = fn_name.to_string();

    if fn_name_str.starts_with("set_") || fn_name_str.starts_with("add_") {
        let doc_attrs = extract_doc_attrs(&mut attrs);

        // set the name of the function
        sig.ident = ok_or_compile_err!(syn::parse_str(&fn_name_str[4..]));

        let args = process_fn_signature(options, &mut sig);

        quote! {
            #method

            #(#doc_attrs)*
            #(#attrs)*
            #[must_use]
            #vis #defaultness #sig {
                self.#fn_name(#(#args),*);
                self
            }
        }
    }
    else {
        quote_compile_err!("The method must start with \"set_\" or \"add_\"!")
    }
}

// process the function signature and return the arguments required to
// call the setter/adder method and collapse optionals if the macro
// caller wants it done.
fn process_fn_signature(options: ChainableOptions, sig: &mut Signature) -> Vec<TokenStream2> {
    let mut in_args = Vec::new();
    let mut args = Vec::new();
    in_args.push(FnArg::Receiver(parse_quote! { mut self }));

    for arg in &sig.inputs {
        if let FnArg::Typed(typed_arg) = arg {
            let var_name = &typed_arg.pat;
            let var_type = &typed_arg.ty;

            // let's collapse all the Options to their inner types if the caller wants it
            if options.collapse_options {
                if_chain! {
                    if let Type::Path(TypePath { path, .. }) = &*typed_arg.ty;
                    if let Some(type_segment) = path.segments.last();
                    if type_segment.ident == "Option";
                    if let PathArguments::AngleBracketed(type_args) = &type_segment.arguments;
                    if let GenericArgument::Type(inner_type) = type_args.args.first().unwrap();

                    then {
                        let inner_type = if options.use_into_impl {
                            parse_quote!(impl core::convert::Into<#inner_type>)
                        }
                        else {
                            inner_type.clone()
                        };

                        let var_name = &typed_arg.pat;
                        in_args.push(
                            parse_quote! { #var_name:#inner_type }
                        );

                        args.push(
                            if options.use_into_impl {
                                parse_quote! { Some(#var_name.into()) }
                            }
                            else {
                                parse_quote! { Some(#var_name) }
                            }
                        );
                    }
                    else {
                        if_chain! {
                            if options.use_into_impl;
                            if let Type::Path(TypePath { path, .. }) = &*typed_arg.ty;
                            if let Some(type_segment) = path.segments.last();
                            if type_segment.ident != "Result";

                            then {
                                in_args.push(
                                    parse_quote!(#var_name:impl core::convert::Into<#var_type>)
                                );
                                args.push(
                                    parse_quote!(#var_name.into())
                                );
                            }
                            else {
                                in_args.push(FnArg::Typed(typed_arg.clone()));
                                args.push(typed_arg.pat.to_token_stream());
                            }
                        }
                    }
                }
            }
            else if options.use_into_impl {
                if_chain! {
                    if let Type::Path(TypePath { path, .. }) = &*typed_arg.ty;
                    if let Some(type_segment) = path.segments.last();
                    if type_segment.ident != "Result" && type_segment.ident != "Option";

                    then {
                        in_args.push(
                            parse_quote!(#var_name:impl core::convert::Into<#var_type>)
                        );
                        args.push(
                            parse_quote!(#var_name.into())
                        );
                    }
                    else {
                        in_args.push(FnArg::Typed(typed_arg.clone()));
                        args.push(typed_arg.pat.to_token_stream());
                    }
                }
            }
            else {
                in_args.push(FnArg::Typed(typed_arg.clone()));
                args.push(typed_arg.pat.to_token_stream());
            }
        }
    }

    sig.inputs = in_args.into_iter().collect();
    sig.output = parse_quote! { -> Self };
    args
}

fn extract_doc_attrs(attrs: &mut Vec<Attribute>) -> Vec<Attribute> {
    let mut new_attrs = Vec::new();
    let mut doc_attrs = Vec::new();

    for attr in attrs.drain(..) {
        let mut my_attr = false;
        if let Meta::NameValue(meta_val) = &attr.meta {
            if_chain! {
                if let Ok(name) = meta_val.path.require_ident();
                if name == "doc";
                if let Expr::Lit(expr) = &meta_val.value;
                if let Lit::Str(str_expr) = &expr.lit;

                then {
                    let doc_comment = str_expr.value();
                    for line in doc_comment.lines() {
                        doc_attrs.push(
                            parse_quote! { #[doc = #line] }
                        );
                    }

                    my_attr = true;
                }
            }
        }

        if !my_attr {
            new_attrs.push(attr);
        }
    }

    doc_attrs.push(
        parse_quote! {
            #[doc = ""]
        }
    );

    doc_attrs.push(
        parse_quote! {
            #[doc = "Chainable variant"]
        }
    );

    *attrs = new_attrs;
    doc_attrs
}