The functions of backblaze-b2 return a Result
with an error type B2Error
that doesn't implement the Error
trait.
To be able to use these function in other functions that return Result<(), Box<dyn Error>>
, I have written this wrapper:
use backblaze_b2::B2Error;
use std::fmt::{Display, Formatter};
use std::error::Error;
#[derive(Debug)]
pub struct ErrorWrapper {
b2_error: B2Error,
}
impl Display for ErrorWrapper {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(&self.b2_error, f)
}
}
impl Error for ErrorWrapper {}
pub trait IntoError {
fn into_error(self) -> ErrorWrapper;
}
impl IntoError for B2Error {
fn into_error(self) -> ErrorWrapper {
ErrorWrapper{
b2_error: self
}
}
}
Then I call the backblaze-b2 functions like this:
let auth = cred.authorize(&client).map_err(|e| e.into_error() )?;
I would like to get rid of the .map_err(|e| e.into_error() )
if possible.
However, I can't simply implement Error
for B2Error
because of the orphan rule. Is there another way to get rid of the .map_err(...)
?