Hi there!
I have multiple errortypes combined into an error enum inside a module of my project:
#[derive(Debug)]
pub enum ModuleError {
A(DependencyErrorA),
B(InternalError),
}
impl From<DependencyErrorA> for ModuleError {
fn from(error: DependencyErrorA) -> Self {
ModuleError::DependencyErrorA(error)
}
}
// ... and so on
Now in the root of my project i have some kind of "Super Error" which shall combine all errors of all submodules. For now it looks something like this:
use crate::module::ModuleError;
#[derive(Debug)]
pub enum TopLevelError {
ModuleError(ModuleError)
}
impl From<ModuleError> for TopLevelError {
fn from(error: ModuleError) -> Self {
TopLevelError::ModuleError(error)
}
}
And I have a result type:
pub type TopLevelResult<T> = Result<T, TopLevelError>;
How can i automatically 'cast' up the whole chain?
My code looks somewhat like this:
fn main() -> TopLevelResult<()> {
let connection = do_stuff()?; // may unwrap to a DependencyErrorA
let stuff = do_stuff_with_connection(&connection); // may unwrap to a InternalError
manage_stuff(&connection, 1234)?; // may unwrap to a ModuleError
let mut state = State::new(&connection);
state.scan()?; // may unwrap to a DependencyErrorA
loop { //.... and so on }
}
Do i have to reimplement the From
traits i implemented on the ModuleError
again? Or is there a nice way to convert a DependencyErrorA to a TopLevelError?
Thanks!