| 1 |
//! Macros for Actix system and runtime. |
| 2 |
//! |
| 3 |
//! The [`actix-rt`](https://docs.rs/actix-rt) crate must be available for macro output to compile. |
| 4 |
//! |
| 5 |
//! # Entry-point |
| 6 |
//! See docs for the [`#[main]`](macro@main) macro. |
| 7 |
//! |
| 8 |
//! # Tests |
| 9 |
//! See docs for the [`#[test]`](macro@test) macro. |
| 10 |
|
| 11 |
#![deny(rust_2018_idioms, nonstandard_style)] |
| 12 |
#![warn(future_incompatible)] |
| 13 |
#![doc(html_logo_url = "https://actix.rs/img/logo.png")] |
| 14 |
#![doc(html_favicon_url = "https://actix.rs/favicon.ico")] |
| 15 |
|
| 16 |
use proc_macro::TokenStream; |
| 17 |
use quote::quote; |
| 18 |
use syn::parse::Parser as _; |
| 19 |
|
| 20 |
type AttributeArgs = syn::punctuated::Punctuated<syn::Meta, syn::Token![,]>; |
| 21 |
|
| 22 |
/// Marks async entry-point function to be executed by Actix system. |
| 23 |
// #[allow(clippy::needless_doctest_main)] |
| 24 |
// #[cfg(not(test))] // Work around for rust-lang/rust#62127 |
| 25 |
#[proc_macro_attribute] |
| 26 |
pub fn main(args: TokenStream, item: TokenStream) -> TokenStream { |
| 27 |
let mut input = match syn::parse::<syn::ItemFn>(item.clone()) { |
| 28 |
Ok(input) => input, |
| 29 |
// on parse err, make IDEs happy; see fn docs |
| 30 |
Err(err) => return input_and_compile_error(item, err), |
| 31 |
}; |
| 32 |
|
| 33 |
let parser = AttributeArgs::parse_terminated; |
| 34 |
let args = match parser.parse(args.clone()) { |
| 35 |
Ok(args) => args, |
| 36 |
Err(err) => return input_and_compile_error(args, err), |
| 37 |
}; |
| 38 |
|
| 39 |
let attrs = &input.attrs; |
| 40 |
let vis = &input.vis; |
| 41 |
let sig = &mut input.sig; |
| 42 |
let body = &input.block; |
| 43 |
|
| 44 |
if sig.asyncness.is_none() { |
| 45 |
return syn::Error::new_spanned( |
| 46 |
sig.fn_token, |
| 47 |
"the async keyword is missing from the function declaration", |
| 48 |
) |
| 49 |
.to_compile_error() |
| 50 |
.into(); |
| 51 |
} |
| 52 |
|
| 53 |
let mut system = syn::parse_str::<syn::Path>("::actix_rt::System").unwrap(); |
| 54 |
|
| 55 |
for arg in &args { |
| 56 |
match arg { |
| 57 |
syn::Meta::NameValue(syn::MetaNameValue { |
| 58 |
path, |
| 59 |
value: |
| 60 |
syn::Expr::Lit(syn::ExprLit { |
| 61 |
lit: syn::Lit::Str(lit), |
| 62 |
.. |
| 63 |
}), |
| 64 |
.. |
| 65 |
}) => match path |
| 66 |
.get_ident() |
| 67 |
.map(|i| i.to_string().to_lowercase()) |
| 68 |
.as_deref() |
| 69 |
{ |
| 70 |
Some("system") => match lit.parse() { |
| 71 |
Ok(path) => system = path, |
| 72 |
Err(_) => { |
| 73 |
return syn::Error::new_spanned(lit, "Expected path") |
| 74 |
.to_compile_error() |
| 75 |
.into(); |
| 76 |
} |
| 77 |
}, |
| 78 |
_ => { |
| 79 |
return syn::Error::new_spanned(arg, "Unknown attribute specified") |
| 80 |
.to_compile_error() |
| 81 |
.into(); |
| 82 |
} |
| 83 |
}, |
| 84 |
|
| 85 |
_ => { |
| 86 |
return syn::Error::new_spanned(arg, "Unknown attribute specified") |
| 87 |
.to_compile_error() |
| 88 |
.into(); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
sig.asyncness = None; |
| 94 |
|
| 95 |
(quote! { |
| 96 |
#(#attrs)* |
| 97 |
#vis #sig { |
| 98 |
<#system>::new().block_on(async move { #body }) |
| 99 |
} |
| 100 |
}) |
| 101 |
.into() |
| 102 |
} |
| 103 |
|
| 104 |
/// Marks async test function to be executed in an Actix system. |
| 105 |
#[proc_macro_attribute] |
| 106 |
pub fn test(args: TokenStream, item: TokenStream) -> TokenStream { |
| 107 |
let mut input = match syn::parse::<syn::ItemFn>(item.clone()) { |
| 108 |
Ok(input) => input, |
| 109 |
// on parse err, make IDEs happy; see fn docs |
| 110 |
Err(err) => return input_and_compile_error(item, err), |
| 111 |
}; |
| 112 |
|
| 113 |
let parser = AttributeArgs::parse_terminated; |
| 114 |
let args = match parser.parse(args.clone()) { |
| 115 |
Ok(args) => args, |
| 116 |
Err(err) => return input_and_compile_error(args, err), |
| 117 |
}; |
| 118 |
|
| 119 |
let attrs = &input.attrs; |
| 120 |
let vis = &input.vis; |
| 121 |
let sig = &mut input.sig; |
| 122 |
let body = &input.block; |
| 123 |
let mut has_test_attr = false; |
| 124 |
|
| 125 |
for attr in attrs { |
| 126 |
if attr.path().is_ident("test") { |
| 127 |
has_test_attr = true; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
if sig.asyncness.is_none() { |
| 132 |
return syn::Error::new_spanned( |
| 133 |
input.sig.fn_token, |
| 134 |
"the async keyword is missing from the function declaration", |
| 135 |
) |
| 136 |
.to_compile_error() |
| 137 |
.into(); |
| 138 |
} |
| 139 |
|
| 140 |
sig.asyncness = None; |
| 141 |
|
| 142 |
let missing_test_attr = if has_test_attr { |
| 143 |
quote! {} |
| 144 |
} else { |
| 145 |
quote! { #[::core::prelude::v1::test] } |
| 146 |
}; |
| 147 |
|
| 148 |
let mut system = syn::parse_str::<syn::Path>("::actix_rt::System").unwrap(); |
| 149 |
|
| 150 |
for arg in &args { |
| 151 |
match arg { |
| 152 |
syn::Meta::NameValue(syn::MetaNameValue { |
| 153 |
path, |
| 154 |
value: |
| 155 |
syn::Expr::Lit(syn::ExprLit { |
| 156 |
lit: syn::Lit::Str(lit), |
| 157 |
.. |
| 158 |
}), |
| 159 |
.. |
| 160 |
}) => match path |
| 161 |
.get_ident() |
| 162 |
.map(|i| i.to_string().to_lowercase()) |
| 163 |
.as_deref() |
| 164 |
{ |
| 165 |
Some("system") => match lit.parse() { |
| 166 |
Ok(path) => system = path, |
| 167 |
Err(_) => { |
| 168 |
return syn::Error::new_spanned(lit, "Expected path") |
| 169 |
.to_compile_error() |
| 170 |
.into(); |
| 171 |
} |
| 172 |
}, |
| 173 |
_ => { |
| 174 |
return syn::Error::new_spanned(arg, "Unknown attribute specified") |
| 175 |
.to_compile_error() |
| 176 |
.into(); |
| 177 |
} |
| 178 |
}, |
| 179 |
_ => { |
| 180 |
return syn::Error::new_spanned(arg, "Unknown attribute specified") |
| 181 |
.to_compile_error() |
| 182 |
.into(); |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
(quote! { |
| 188 |
#missing_test_attr |
| 189 |
#(#attrs)* |
| 190 |
#vis #sig { |
| 191 |
<#system>::new().block_on(async { #body }) |
| 192 |
} |
| 193 |
}) |
| 194 |
.into() |
| 195 |
} |
| 196 |
|
| 197 |
/// Converts the error to a token stream and appends it to the original input. |
| 198 |
/// |
| 199 |
/// Returning the original input in addition to the error is good for IDEs which can gracefully |
| 200 |
/// recover and show more precise errors within the macro body. |
| 201 |
/// |
| 202 |
/// See <https://github.com/rust-analyzer/rust-analyzer/issues/10468> for more info. |
| 203 |
fn input_and_compile_error(mut item: TokenStream, err: syn::Error) -> TokenStream { |
| 204 |
let compile_err = TokenStream::from(err.to_compile_error()); |
| 205 |
item.extend(compile_err); |
| 206 |
item |
| 207 |
} |