File tree Expand file tree Collapse file tree 3 files changed +18
-4
lines changed Expand file tree Collapse file tree 3 files changed +18
-4
lines changed Original file line number Diff line number Diff line change @@ -3,13 +3,13 @@ use std::path::Path;
3
3
4
4
use fn_error_context:: error_context;
5
5
6
- #[ error_context( "failed to parse config at `{}`" , path. display( ) ) ]
7
- pub fn parse_config ( path : & Path ) -> anyhow:: Result < u32 > {
8
- let text = read_to_string ( path) ?;
6
+ #[ error_context( "failed to parse config at `{}`" , path. as_ref ( ) . display( ) ) ]
7
+ pub fn parse_config ( path : impl AsRef < Path > ) -> anyhow:: Result < u32 > {
8
+ let text = read_to_string ( path. as_ref ( ) ) ?;
9
9
Ok ( text. parse ( ) ?)
10
10
}
11
11
12
12
fn main ( ) -> anyhow:: Result < ( ) > {
13
- println ! ( "config: {}" , parse_config( "config" . as_ref ( ) ) ?) ;
13
+ println ! ( "config: {}" , parse_config( "config" ) ?) ;
14
14
Ok ( ( ) )
15
15
}
Original file line number Diff line number Diff line change
1
+ use fn_error_context:: error_context;
2
+
3
+ #[ error_context( "context {}" , arg. as_ref( ) ) ]
4
+ fn do_stuff ( arg : impl AsRef < str > ) -> anyhow:: Result < ( ) > {
5
+ anyhow:: bail!( "error {}" , arg. as_ref( ) )
6
+ }
7
+
8
+ fn main ( ) {
9
+ assert_eq ! (
10
+ format!( "{:#}" , do_stuff( "hello" ) . unwrap_err( ) ) ,
11
+ "context hello: error hello"
12
+ ) ;
13
+ }
Original file line number Diff line number Diff line change @@ -11,4 +11,5 @@ fn tests() {
11
11
tests. pass ( "tests/failure.rs" ) ;
12
12
tests. pass ( "tests/non_copy_arg.rs" ) ;
13
13
tests. pass ( "tests/non_copy_fmt_arg.rs" ) ;
14
+ tests. pass ( "tests/as_ref.rs" ) ;
14
15
}
You can’t perform that action at this time.
0 commit comments