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
mod main;
mod launch;
mod test;

use devise::{Diagnostic, Spanned, Result};
use devise::ext::SpanDiagnosticExt;
use proc_macro2::{TokenStream, Span};

// Common trait implemented by `async` entry generating attributes.
trait EntryAttr {
    /// Whether the attribute requires the attributed function to be `async`.
    const REQUIRES_ASYNC: bool;

    /// Return a new or rewritten function, using block as the main execution.
    fn function(f: &mut syn::ItemFn) -> Result<TokenStream>;
}

fn _async_entry<A: EntryAttr>(
    _args: proc_macro::TokenStream,
    input: proc_macro::TokenStream
) -> Result<TokenStream> {
    let mut function: syn::ItemFn = syn::parse(input)
        .map_err(Diagnostic::from)
        .map_err(|d| d.help("attribute can only be applied to functions"))?;

    if A::REQUIRES_ASYNC && function.sig.asyncness.is_none() {
        return Err(Span::call_site()
            .error("attribute can only be applied to `async` functions")
            .span_note(function.sig.span(), "this function must be `async`"));
    }

    if !function.sig.inputs.is_empty() {
        return Err(Span::call_site()
            .error("attribute can only be applied to functions without arguments")
            .span_note(function.sig.span(), "this function must take no arguments"));
    }

    A::function(&mut function)
}

macro_rules! async_entry {
    ($name:ident, $kind:ty, $default:expr) => (
        pub fn $name(a: proc_macro::TokenStream, i: proc_macro::TokenStream) -> TokenStream {
            _async_entry::<$kind>(a, i).unwrap_or_else(|d| {
                let d = d.emit_as_item_tokens();
                let default = $default;
                quote!(#d #default)
            })
        }
    )
}

async_entry!(async_test_attribute, test::Test, quote!());
async_entry!(main_attribute, main::Main, quote!(fn main() {}));
async_entry!(launch_attribute, launch::Launch, quote!(fn main() {}));