maybe-async-cfg 0.1.0

A procedure macro to unify sync and async implementations depending on the features of your 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
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
use std::{collections::HashMap, iter::FromIterator};

#[allow(unused_imports)]
use proc_macro::TokenStream;
use quote::{quote, ToTokens};
use syn::{
    parse_quote, punctuated::Punctuated, visit_mut::VisitMut, Attribute, Expr, ExprBlock,
    GenericArgument, GenericParam, Ident, Item, Macro, MetaNameValue, NestedMeta, PathArguments,
    PathSegment, Type, TypeParam, TypeParamBound, WherePredicate,
};

use crate::{
    macros::ConvertMode,
    params::MacroParameters,
    utils::{AttributeArgsInParens, PunctuatedList},
    visit_ext::{IdentMode, VisitMutExt, Visitor},
};

pub struct AsyncAwaitVisitor<'p> {
    convert_mode: ConvertMode,
    params: &'p mut MacroParameters,
    generics: Vec<HashMap<String, PathSegment>>,
}

impl<'p> AsyncAwaitVisitor<'p> {
    pub fn new(params: &'p mut MacroParameters, convert_mode: ConvertMode) -> Self {
        Self {
            convert_mode,
            params,
            generics: vec![],
        }
    }

    fn generics_get<S: AsRef<str>>(&self, key: S) -> Option<&PathSegment> {
        for gens in &self.generics {
            if let Some(ps) = gens.get(key.as_ref()) {
                return Some(ps);
            }
        }

        None
    }
}

fn search_future_trait_bound(bound: &TypeParamBound) -> Option<PathSegment> {
    if let TypeParamBound::Trait(trait_bound) = bound {
        let segment = &trait_bound.path.segments[trait_bound.path.segments.len() - 1];
        let name = segment.ident.to_string();
        if name.eq("Future") {
            // match Future<Output=Type>
            if let PathArguments::AngleBracketed(args) = &segment.arguments {
                // binding: Output=Type
                if let GenericArgument::Binding(binding) = &args.args[0] {
                    if let Type::Path(p) = &binding.ty {
                        return Some(p.path.segments[0].clone());
                    }
                }
            }
        }
    };

    None
}

impl<'p> AsyncAwaitVisitor<'p> {
    fn process_replace_features_meta(&self, meta: &mut syn::Meta) -> syn::Result<bool> {
        let mut changed = false;

        match meta {
            syn::Meta::NameValue(MetaNameValue {
                path,
                lit: syn::Lit::Str(s),
                ..
            }) => {
                if let Some(ident) = path.get_ident() {
                    if ident.to_string() == "feature" {
                        let prev = s.value();
                        if let Some(new) = self.params.replace_features_get(&prev) {
                            *s = syn::LitStr::new(new, s.span());
                            changed = true;
                        }
                    }
                }
            }
            syn::Meta::List(list) => {
                for nm in &mut list.nested {
                    if let syn::NestedMeta::Meta(m) = nm {
                        changed |= self.process_replace_features_meta(m)?;
                    }
                }
            }
            _ => {}
        }

        Ok(changed)
    }

    fn process_attribute_if(&mut self, attr: &mut Attribute, not: bool) -> syn::Result<()> {
        let args =
            syn::parse_macro_input::parse::<AttributeArgsInParens>(attr.tokens.clone().into())?;

        let arg = match &args.args.len() {
            0 => {
                return Err(syn::Error::new_spanned(
                    attr.to_token_stream(),
                    "Expected ident",
                ))
            }
            1 => &args.args[0],
            _ => {
                return Err(syn::Error::new_spanned(
                    args.args[1].to_token_stream(),
                    "Too many arguments",
                ))
            }
        };

        let key = match arg {
            NestedMeta::Lit(syn::Lit::Str(s)) => s.value(),
            NestedMeta::Meta(syn::Meta::Path(ref p)) => {
                if let Some(s) = p.get_ident() {
                    s.to_string()
                } else {
                    return Err(syn::Error::new_spanned(
                        arg.to_token_stream(),
                        "Wrong ident",
                    ));
                }
            }
            NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue {
                path,
                lit: syn::Lit::Str(value),
                ..
            })) if path.is_ident("key") => value.value(),
            _ => {
                return Err(syn::Error::new_spanned(
                    arg.to_token_stream(),
                    "Wrong ident",
                ))
            }
        };

        let success = if let Some(current_key) = self.params.key_get() {
            (current_key == &key) ^ not
        } else {
            false
        };

        let new_name = if success { "noop" } else { "remove" };
        attr.path = self.params.make_self_path(new_name);

        Ok(())
    }

    fn process_attrs(&mut self, attrs: &mut Vec<syn::Attribute>) -> syn::Result<()> {
        for attr in attrs.iter_mut() {
            if let Some(name) = self.params.is_our_attr(attr) {
                match name.as_str() {
                    "only_if" => self.process_attribute_if(attr, false)?,
                    "remove_if" => self.process_attribute_if(attr, true)?,
                    _ => {
                        // Attribute stays unchanged. Unknown attributes will be
                        // rejected by compiter later.
                    }
                }
            }
        }

        if !self.params.drop_attrs_is_empty() {
            attrs.retain(|attr| {
                if let Some(ident) = attr.path.get_ident() {
                    let ident = ident.to_string();
                    !self.params.drop_attrs_contains(&ident)
                } else {
                    true
                }
            });
        }

        if !self.params.replace_features_is_empty() {
            for attr in attrs {
                if let Some(ident) = attr.path.get_ident() {
                    if ident.to_string() == "cfg" {
                        if let Ok(mut meta) = attr.parse_meta() {
                            if self.process_replace_features_meta(&mut meta)? {
                                if let syn::Meta::List(syn::MetaList { nested, .. }) = meta {
                                    attr.tokens = quote!((#nested));
                                }
                            }
                        }
                    }
                }
            }
        }

        Ok(())
    }

    fn process_expr(&mut self, node: &mut Expr) -> syn::Result<()> {
        match self.convert_mode {
            ConvertMode::ToSync => {
                // async -> sync, remove async_impl blocks
                match node {
                    Expr::Await(expr) => {
                        *node = (*expr.base).clone()
                    }

                    Expr::Async(expr) => {
                        let inner = &expr.block;
                        let sync_expr = if inner.stmts.len() == 1 {
                            // remove useless braces when there is only one statement
                            let stmt = &inner.stmts.get(0).unwrap();
                            // convert statement to Expr
                            parse_quote!(#stmt)
                        } else {
                            Expr::Block(ExprBlock {
                                attrs: expr.attrs.clone(),
                                block: inner.clone(),
                                label: None,
                            })
                        };
                        *node = sync_expr;
                    }

                    _ => {}
                }
            }
            ConvertMode::ToAsync => {
                // stay async, just remove sync_impl blocks
                match node {
                    _ => {}
                }
            }
        };

        Ok(())
    }

    fn process_item(&mut self, node: &mut Item) -> syn::Result<()> {
        match self.convert_mode {
            ConvertMode::ToSync => {
                // find generic parameter of Future and replace it with its Output type
                if let Item::Fn(item_fn) = node {
                    let mut gens: HashMap<String, PathSegment> = HashMap::new();

                    // generic params: <T:Future<Output=()>, F>
                    for param in &item_fn.sig.generics.params {
                        // generic param: T:Future<Output=()>
                        if let GenericParam::Type(type_param) = param {
                            let generic_type_name = &type_param.ident;

                            // bound: Future<Output=()>
                            for bound in &type_param.bounds {
                                if let Some(ps) = search_future_trait_bound(bound) {
                                    gens.insert(generic_type_name.to_string(), ps);
                                }
                            }
                        }
                    }

                    if let Some(where_clause) = &item_fn.sig.generics.where_clause {
                        for predicate in &where_clause.predicates {
                            if let WherePredicate::Type(predicate_type) = predicate {
                                let generic_type_name =
                                    if let Type::Path(p) = &predicate_type.bounded_ty {
                                        &p.path.segments[0].ident
                                    } else {
                                        panic!("Please submit an issue");
                                    };

                                for bound in &predicate_type.bounds {
                                    if let Some(ps) = search_future_trait_bound(bound) {
                                        gens.insert(generic_type_name.to_string(), ps);
                                    }
                                }
                            }
                        }
                    }

                    self.generics.push(gens);
                }

                if let Item::Fn(item_fn) = node {
                    // remove generic type from generics <T, F>
                    let args = item_fn
                        .sig
                        .generics
                        .params
                        .iter()
                        .filter_map(|param| {
                            if let GenericParam::Type(type_param) = &param {
                                if let Some(_) = self.generics_get(type_param.ident.to_string()) {
                                    return None;
                                }
                            };
                            Some(param)
                        })
                        .collect::<Vec<_>>();

                    item_fn.sig.generics.params = Punctuated::from_iter(
                        args.into_iter().map(|p| p.clone()).collect::<Vec<_>>(),
                    );

                    // remove generic type from where clause
                    if let Some(where_clause) = &mut item_fn.sig.generics.where_clause {
                        let new_where_clause = where_clause
                            .predicates
                            .iter()
                            .filter_map(|predicate| {
                                if let WherePredicate::Type(predicate_type) = predicate {
                                    if let Type::Path(p) = &predicate_type.bounded_ty {
                                        if let Some(_) =
                                            self.generics_get(p.path.segments[0].ident.to_string())
                                        {
                                            return None;
                                        }
                                    }
                                };
                                Some(predicate)
                            })
                            .collect::<Vec<_>>();

                        where_clause.predicates = Punctuated::from_iter(
                            new_where_clause
                                .into_iter()
                                .map(|c| c.clone())
                                .collect::<Vec<_>>(),
                        );
                    };
                }
            }
            ConvertMode::ToAsync => {}
        };

        Ok(())
    }

    fn after_process_item(&mut self, node: &mut Item) -> syn::Result<()> {
        match self.convert_mode {
            ConvertMode::ToSync => {
                // find generic parameter of Future and replace it with its Output type
                if let Item::Fn(_item_fn) = node {
                    self.generics.pop();
                }
            }
            _ => {}
        }
        Ok(())
    }

    fn process_path_segment(&mut self, node: &mut PathSegment) -> syn::Result<()> {
        let ident = &mut node.ident;
        let ident_s = ident.to_string();

        // replace generic type with target type
        if let Some(ps) = self.generics_get(&ident_s) {
            *node = ps.clone();
            return Ok(());
        }

        Ok(())
    }

    fn process_ident(&mut self, ident: &mut Ident, mode: IdentMode) -> syn::Result<()> {
        if mode == IdentMode::Use {
            return Ok(());
        };

        if let Some(ir) = self.params.idents_get(ident.to_string()) {
            *ident = ir.ident_add_suffix(ident, self.convert_mode);
            return Ok(());
        }

        Ok(())
    }

    fn process_type_param(&mut self, node: &mut TypeParam) -> syn::Result<()> {
        let ident = &mut node.ident;

        if let Some(ir) = self.params.idents_get(&ident.to_string()) {
            *ident = ir.ident_add_suffix(ident, self.convert_mode);
        }

        Ok(())
    }

    fn process_use_tree(&mut self, node: &mut syn::UseTree) -> syn::Result<()> {
        match node {
            syn::UseTree::Path(syn::UsePath { ident, .. }) => {
                if let Some(ir) = self.params.idents_get(&ident.to_string()) {
                    if !ir.use_mode {
                        *ident = ir.ident_add_suffix(ident, self.convert_mode);
                    }
                }
            }
            syn::UseTree::Name(name) => {
                let ident = &mut name.ident;

                if let Some(ir) = self.params.idents_get(&ident.to_string()) {
                    if ir.use_mode {
                        *node = syn::UseTree::Rename(syn::UseRename {
                            ident: ident.clone(),
                            as_token: syn::Token![as](ident.span()),
                            rename: ir.ident_add_suffix(ident, self.convert_mode),
                        });
                    } else {
                        *ident = ir.ident_add_suffix(ident, self.convert_mode);
                    }
                }
            }
            _ => {}
        };

        Ok(())
    }
}

impl<'p> VisitMutExt for Visitor<AsyncAwaitVisitor<'p>> {
    fn process_attrs(&mut self, attrs: &mut Vec<Attribute>) -> syn::Result<()> {
        self.inner.process_attrs(attrs)
    }
    fn process_ident(&mut self, ident: &mut Ident, mode: IdentMode) -> syn::Result<()> {
        self.inner.process_ident(ident, mode)
    }
    fn process_expr(&mut self, node: &mut Expr) -> syn::Result<()> {
        self.inner.process_expr(node)
    }
    fn process_item(&mut self, node: &mut Item) -> syn::Result<()> {
        self.inner.process_item(node)
    }
    fn after_process_item(&mut self, node: &mut Item) -> syn::Result<()> {
        self.inner.after_process_item(node)
    }

    fn process_macro(&mut self, node: &mut Macro) -> syn::Result<()> {
        if let Some(ident) = node.path.get_ident() {
            if self
                .inner
                .params
                .standard_macros()
                .contains(&ident.to_string().as_str())
            {
                let mut args = syn::parse2::<PunctuatedList>(node.tokens.clone())?;

                for arg in &mut args.list {
                    self.visit_expr_mut(arg);
                }

                node.tokens = args.list.into_token_stream();
            }
        };
        Ok(())
    }
    fn process_path_segment(&mut self, node: &mut PathSegment) -> syn::Result<()> {
        self.inner.process_path_segment(node)
    }
    fn process_type_param(&mut self, node: &mut TypeParam) -> syn::Result<()> {
        self.inner.process_type_param(node)
    }
    fn process_use_tree(&mut self, node: &mut syn::UseTree) -> syn::Result<()> {
        self.inner.process_use_tree(node)
    }
}

impl<'p> AsyncAwaitVisitor<'p> {}