Replies: 1 comment
-
Your code is not valid for a large number of reasons. I guess you mean this: #![allow(dead_code)]
mod bottom {
use snafu::prelude::*;
#[derive(Debug, Snafu)]
pub enum Bottom {
A,
B,
}
pub fn doit() -> Result<(), Bottom> {
Err(Bottom::A)
}
}
mod middle {
use crate::bottom::{self, Bottom};
use snafu::prelude::*;
#[derive(Debug, Snafu)]
pub enum Middle {
A,
B,
#[snafu(transparent)]
Other {
source: Bottom,
},
}
pub fn doit() -> Result<(), Middle> {
Ok(bottom::doit()?)
}
}
mod top {
use crate::middle::{self, Middle};
use snafu::prelude::*;
#[derive(Debug, Snafu)]
pub enum Top {
A,
B,
#[snafu(transparent)]
Other {
source: Middle,
},
}
fn doit() -> Result<(), Top> {
// that should work with out a problem. --- (A)
middle::doit()?;
// that probably does not work without a `From` --- (B)
// bottom::doit()?
Ok(())
}
}
fn main() {} In general there is no solution for what you want, as Rust will not make multiple type conversions implicitly. The shortest thing you can do is to add the explicit type: bottom::doit().map_err(Middle::from)?; You could also create another variant for #[snafu(transparent)]
Other2 {
source: Bottom,
}, You could also use an IIFE (or a (|| {
bottom::doit()?;
Ok::<_, Middle>(())
})()?; Although most times I've done that I end up creating a new function anyway. From a different perspective it feels odd that you are breaking your own layering structure — why skip |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Is it possible to automagically convert between errors in different modules:
So in
(B)
I would like to convert with?
automagically fromResult<(), Bottom>
to an errorResult<(), Top>
.Is there a best practice, I ve read it makes sense to define in each modules its own errors etc.
Beta Was this translation helpful? Give feedback.
All reactions