Skip to content

Commit a947a95

Browse files
committed
Use blanket impls to add convenience methods to all errors
This defines two traits, each defining a convenience method, `as_inline_error_chain` and `as_array_error_chain` respectively, as a more convenient way to construct `InlineErrorChain` and `ArrayErrorChain` adapters. Finally, it also defines corresponding blanket implementations for each one, so they automatically apply to any value whose type implements `std::error::Error`. This makes it possible to use `err.as_inline_error_chain()` instead of `InlineErrorChain::new(&err)`, and `err.as_array_error_chain()` instead of `ArrayErrorChain::new(&err)`.
1 parent 15f6904 commit a947a95

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ an I/O error occurred trying to open /some/path: file not found
4343
be logged directly:
4444

4545
```rust
46+
// Using the adapter constructor
47+
4648
// explicit key
4749
info!(
4850
log, "something happened"; "my-key" => InlineErrorChain::new(&err),
@@ -52,6 +54,18 @@ info!(
5254
info!(
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

5771
With the `derive` feature enabled, error types can `#[derive(SlogInlineError)]`

examples/basic.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use slog::info;
66
use slog::o;
77
use slog::Drain;
88
use slog::Logger;
9+
use slog_error_chain::AsInlineErrorChain;
910
use slog_error_chain::InlineErrorChain;
1011
use std::io;
1112
use 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
}

src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff 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)]
7891
mod 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
}

src/nested_values.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff 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)]
188201
mod 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");

0 commit comments

Comments
 (0)