File tree Expand file tree Collapse file tree 5 files changed +20
-24
lines changed Expand file tree Collapse file tree 5 files changed +20
-24
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ autotests = false
12
12
13
13
[dependencies ]
14
14
quote = " 1.0.3"
15
+ proc-macro2 = " 1.0.9"
15
16
16
17
[dependencies .syn ]
17
18
features = [" full" , " parsing" ]
Original file line number Diff line number Diff line change @@ -2,38 +2,17 @@ extern crate proc_macro;
2
2
3
3
use proc_macro:: TokenStream ;
4
4
use quote:: quote;
5
- use syn:: parse:: { Parse , ParseStream } ;
6
- use syn:: punctuated:: Punctuated ;
7
- use syn:: token:: Comma ;
8
5
9
6
#[ proc_macro_attribute]
10
7
pub fn error_context ( args : TokenStream , input : TokenStream ) -> TokenStream {
11
- let Args { fmt , args } = syn :: parse_macro_input! ( args as Args ) ;
8
+ let args: proc_macro2 :: TokenStream = args . into ( ) ;
12
9
let mut input = syn:: parse_macro_input!( input as syn:: ItemFn ) ;
13
10
14
11
let body = & input. block ;
15
12
let return_ty = & input. sig . output ;
16
13
input. block = syn:: parse_quote!( {
17
- ( || #return_ty #body) ( ) . map_err( |err| err. context( format!( #fmt , # args) ) . into( ) )
14
+ ( || #return_ty #body) ( ) . map_err( |err| err. context( format!( #args) ) . into( ) )
18
15
} ) ;
19
16
20
17
quote ! ( #input) . into ( )
21
18
}
22
-
23
- struct Args {
24
- fmt : syn:: LitStr ,
25
- args : Punctuated < syn:: Expr , Comma > ,
26
- }
27
-
28
- impl Parse for Args {
29
- fn parse ( input : ParseStream ) -> syn:: Result < Self > {
30
- let fmt = input. parse ( ) ?;
31
- let args = if let Some ( _) = input. parse :: < Option < Comma > > ( ) ? {
32
- Punctuated :: parse_separated_nonempty ( input) ?
33
- } else {
34
- Punctuated :: new ( )
35
- } ;
36
-
37
- Ok ( Args { fmt, args } )
38
- }
39
- }
Original file line number Diff line number Diff line change
1
+ use fn_error_context:: error_context;
2
+
3
+ #[ error_context( "context {arg}" , arg = arg) ]
4
+ fn do_stuff ( arg : u32 ) -> anyhow:: Result < ( ) > {
5
+ anyhow:: bail!( "error {}" , arg)
6
+ }
7
+
8
+ fn main ( ) {
9
+ assert_eq ! (
10
+ format!( "{:#}" , do_stuff( 1 ) . unwrap_err( ) ) ,
11
+ "context 1: error 1"
12
+ ) ;
13
+ }
Original file line number Diff line number Diff line change 1
- error: unexpected end of input, expected literal
1
+ error: requires at least a format string argument
2
2
--> $DIR/missing_fmt.rs:3:1
3
3
|
4
4
3 | #[error_context]
5
5
| ^^^^^^^^^^^^^^^^
6
+ |
7
+ = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
Original file line number Diff line number Diff line change @@ -14,4 +14,5 @@ fn tests() {
14
14
tests. pass ( "tests/as_ref.rs" ) ;
15
15
tests. compile_fail ( "tests/fmt_missing_arg.rs" ) ;
16
16
tests. compile_fail ( "tests/fmt_unused_arg.rs" ) ;
17
+ tests. pass ( "tests/fmt_named_arg.rs" ) ;
17
18
}
You can’t perform that action at this time.
0 commit comments