Hi, I'm trying to define a custom Error
that, much like anyhow
, can have context attached.
Unlike anyhow
, I want the custom Error
to only accept a limited set of Error
s from other domains.
An impl From<E>
for each other Error
E
satisfies the case where I simply want to convert other Error
's to the CustomError
.
struct CustomError {
...
}
impl StdError for CustomError {};
impl From<lib_a::Error> for CustomError {
fn from(e: lib_a::Error) -> Self {
CustomError { ... }
}
}
impl From<lib_b::Error> for CustomError {
fn from(e: lib_b::Error) -> Self {
CustomError { ... }
}
}
But if I want to add context to the Error
using an anyhow
style extension trait and restrict the set of types that can be converted to CustomError
things get a bit more complicated.
let url: Url = "localhost".parse().context("Parsing a URL failed")?;
My attempt at an implementation is:
trait Context {
fn context(self, s: String) -> CustomError;
}
impl<T> Context for StdResult<T, lib_a::Error> {
fn context(self, s: String) -> StdResult<T, CustomError> {
todo!("Perform a specific lib_a::Error action");
}
}
impl<T> Context for StdResult<T, lib_b::Error> {
fn context(self, s: String) -> StdResult<T, CustomError> {
todo!("Perform a specific lib_b::Error action");
}
}
But this results in requiring type annotations at the call site due to conflicting impls.
error[E0283]: type annotations needed
--> ext_api/src/event.rs:174:10
|
174 | .context("Parsing a URL failed")?;
| ^^^^^^^^^^^
|
note: multiple `impl`s satisfying `std::result::Result<_, _>:
...
Does anyone have any ideas on how I can limit the set of Error
s that are allowed to have context?
Thanks