rocket_codegen/attribute/entry/
main.rs

1use super::EntryAttr;
2
3use devise::{Spanned, Result};
4use devise::ext::SpanDiagnosticExt;
5use proc_macro2::{TokenStream, Span};
6
7/// `#[rocket::async_main]`: calls the attributed fn inside `rocket::async_main`
8pub struct Main;
9
10impl EntryAttr for Main {
11    const REQUIRES_ASYNC: bool = true;
12
13    fn function(f: &mut syn::ItemFn) -> Result<TokenStream> {
14        let (attrs, vis, block, sig) = (&f.attrs, &f.vis, &f.block, &mut f.sig);
15        if sig.ident != "main" {
16            // FIXME(diag): warning!
17            Span::call_site()
18                .warning("attribute is typically applied to `main` function")
19                .span_note(sig.ident.span(), "this function is not `main`")
20                .emit_as_item_tokens();
21        }
22
23        sig.asyncness = None;
24        Ok(quote_spanned!(block.span() => #(#attrs)* #vis #sig {
25            ::rocket::async_main(async move #block)
26        }))
27    }
28}