File tree Expand file tree Collapse file tree 4 files changed +51
-1
lines changed
Expand file tree Collapse file tree 4 files changed +51
-1
lines changed Original file line number Diff line number Diff line change @@ -43,6 +43,8 @@ an I/O error occurred trying to open /some/path: file not found
4343be logged directly:
4444
4545``` rust
46+ // Using the adapter constructor
47+
4648// explicit key
4749info! (
4850 log , " something happened" ; " my-key" => InlineErrorChain :: new (& err ),
@@ -52,6 +54,18 @@ info!(
5254info! (
5355 log , " something happened" ; InlineErrorChain :: new (& err ),
5456);
57+
58+ // Using the blanket method implementation
59+
60+ // explicit key
61+ info! (
62+ log , " something happened" ; " my-key" => err . as_inline_error_chain (),
63+ );
64+
65+ // key omitted; will log with the key "error"
66+ info! (
67+ log , " something happened" ; err . as_inline_error_chain (),
68+ );
5569```
5670
5771With the ` derive ` feature enabled, error types can ` #[derive(SlogInlineError)] `
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ use slog::info;
66use slog:: o;
77use slog:: Drain ;
88use slog:: Logger ;
9+ use slog_error_chain:: AsInlineErrorChain ;
910use slog_error_chain:: InlineErrorChain ;
1011use std:: io;
1112use std:: path:: PathBuf ;
@@ -37,6 +38,6 @@ fn main() {
3738 ) ;
3839 info ! (
3940 log, "logging error with InlineErrorChain, implicit key" ;
40- InlineErrorChain :: new ( & err) ,
41+ err. as_inline_error_chain ( ) ,
4142 ) ;
4243}
Original file line number Diff line number Diff line change @@ -74,6 +74,19 @@ impl fmt::Display for InlineErrorChain<'_> {
7474 }
7575}
7676
77+ /// A trait for conveniently constructing [`InlineErrorChain`]s from [`Error`]s.
78+ pub trait AsInlineErrorChain : Error {
79+ /// A convenient wrapper around [`InlineErrorChain::new`].
80+ fn as_inline_error_chain < ' a > ( & ' a self ) -> InlineErrorChain < ' a > ;
81+ }
82+
83+ /// Implementation for all [`Error`] types
84+ impl < E : Error > AsInlineErrorChain for E {
85+ fn as_inline_error_chain < ' a > ( & ' a self ) -> InlineErrorChain < ' a > {
86+ InlineErrorChain :: new ( self )
87+ }
88+ }
89+
7790#[ cfg( test) ]
7891mod tests {
7992 use std:: io;
@@ -108,5 +121,11 @@ mod tests {
108121 InlineErrorChain :: new( & err) . to_string( ) ,
109122 "error b: error a: test error"
110123 ) ;
124+
125+ // Confirm AsInlineErrorChain works as intended
126+ assert_eq ! (
127+ err. as_inline_error_chain( ) . to_string( ) ,
128+ "error b: error a: test error"
129+ ) ;
111130 }
112131}
Original file line number Diff line number Diff line change @@ -184,6 +184,19 @@ impl SerdeValue for ArrayErrorChain<'_> {
184184 }
185185}
186186
187+ /// A trait for conveniently constructing [`ArrayErrorChain`]s from [`Error`]s.
188+ pub trait AsArrayErrorChain : Error {
189+ /// A convenient wrapper around [`ArrayErrorChain::new`].
190+ fn as_array_error_chain < ' a > ( & ' a self ) -> ArrayErrorChain < ' a > ;
191+ }
192+
193+ /// Implementation for all [`Error`] types
194+ impl < E : Error > AsArrayErrorChain for E {
195+ fn as_array_error_chain < ' a > ( & ' a self ) -> ArrayErrorChain < ' a > {
196+ ArrayErrorChain :: new ( self )
197+ }
198+ }
199+
187200#[ cfg( test) ]
188201mod tests {
189202 use super :: * ;
@@ -277,6 +290,9 @@ mod tests {
277290 let chain = ArrayErrorChain :: new ( & err) ;
278291 assert_eq ! ( chain. to_string( ) , "test error" ) ;
279292
293+ // Check AsArrayErrorChain implementation
294+ assert_eq ! ( err. as_array_error_chain( ) . to_string( ) , "test error" ) ;
295+
280296 let mut out = StringSerializer :: default ( ) ;
281297 chain. serialize_fallback ( "unused" , & mut out) . unwrap ( ) ;
282298 assert_eq ! ( out. 0 , "test error" ) ;
You can’t perform that action at this time.
0 commit comments