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
#![feature(drain_filter)]

use parse::{replace_async_for, AttrGenerator, ExprGenerator};
use proc_macro::TokenStream as TokenStream1;
use proc_macro2::TokenStream;
use quote::ToTokens;
use syn::parse2;

mod break_visit;
mod gen_visit;
mod parse;
mod process;

#[proc_macro]
pub fn async_generator(input: TokenStream1) -> TokenStream1 {
    let input: TokenStream = input.into();
    let input: ExprGenerator = match parse2(replace_async_for(input)) {
        Ok(input) => input,
        Err(err) => {
            return err.to_compile_error().into();
        }
    };

    input.process().into_token_stream().into()
}

#[proc_macro_attribute]
pub fn generator(_args: TokenStream1, input: TokenStream1) -> TokenStream1 {
    let input: TokenStream = input.into();
    let input = replace_async_for(input);
    let input: AttrGenerator = match parse2(input) {
        Ok(input) => input,
        Err(err) => {
            return err.to_compile_error().into();
        }
    };

    input
        .process()
        .map_or_else(|e| e.to_compile_error(), ToTokens::into_token_stream)
        .into()
}