-
Notifications
You must be signed in to change notification settings - Fork 61
/
report.rs
70 lines (65 loc) · 1.88 KB
/
report.rs
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
use quote::quote;
use syn::{spanned::Spanned, Item};
pub fn body(
_attr: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> syn::Result<proc_macro2::TokenStream> {
let item = syn::parse::<Item>(item)?;
match item {
Item::Fn(f) => {
let syn::ItemFn {
attrs,
vis,
sig,
block,
} = f;
let syn::Signature {
constness,
asyncness,
unsafety,
abi,
fn_token,
ident,
generics,
paren_token: _,
inputs,
variadic,
output,
} = sig;
let output = match output {
syn::ReturnType::Default => quote! {},
syn::ReturnType::Type(_, ty) => {
if cfg!(feature = "rust_1_61") {
quote! {
-> ::snafu::Report<<#ty as ::snafu::__InternalExtractErrorType>::Err>
}
} else {
quote! {
-> ::core::result::Result<(), snafu::Report<<#ty as ::snafu::__InternalExtractErrorType>::Err>>
}
}
}
};
Ok(quote! {
#(#attrs)*
#vis
#constness
#asyncness
#unsafety
#abi
#fn_token
#ident
#generics
(#inputs #variadic)
#output
{
snafu::Report::capture(|| #block)
}
})
}
_ => Err(syn::Error::new(
item.span(),
"`#[snafu::report]` may only be used on functions",
)),
}
}