Manually impl From<io::Error> for BinstallError

so that if the `io::Error` wraps a `BinstallError`, we would just unwrap
it and return the inner `BinstallError`.

Otherwise, just wrap the `io::Error` in a `BinstallError`.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-06-18 18:02:42 +10:00
parent 1161a60968
commit aba1ba7b6d
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

@ -75,7 +75,7 @@ pub enum BinstallError {
/// - Exit: 74 /// - Exit: 74
#[error(transparent)] #[error(transparent)]
#[diagnostic(severity(error), code(binstall::io))] #[diagnostic(severity(error), code(binstall::io))]
Io(#[from] std::io::Error), Io(std::io::Error),
/// An error interacting with the crates.io API. /// An error interacting with the crates.io API.
/// ///
@ -231,3 +231,23 @@ impl Termination for BinstallError {
code code
} }
} }
impl From<std::io::Error> for BinstallError {
fn from(err: std::io::Error) -> Self {
let is_inner_binstall_err = err
.get_ref()
.map(|e| e.is::<BinstallError>())
.unwrap_or_default();
if is_inner_binstall_err {
let inner = err
.into_inner()
.expect("err.get_ref() returns Some, so err.into_inner() should als return Some");
*inner
.downcast()
.expect("The inner err is tested to be BinstallError")
} else {
BinstallError::Io(err)
}
}
}