我建议您使用特定类型或添加自己的变体。这个
UnrarError
被设计为泛型,而不应该是泛型。
请尝试以下操作:
#[derive(Debug, Error)]
pub enum MyError {
#[error(transparent)]
Io(#[from] io::Error),
#[error(transparent)]
Unrar(#[from] unrar::error::UnrarError<OpenArchive>),
#[error(transparent)]
UnrarProcessing(#[from] unrar::error::UnrarError<Vec<Entry>>),
#[error("directory already exists")]
DirectoryExists,
}
或者在这种情况下我更喜欢怎么做:
#[derive(Debug, Error)]
pub enum MyError {
#[error(transparent)]
Io(#[from] io::Error),
#[error("unrar error")]
Unrar,
#[error("directory already exists")]
DirectoryExists,
}
impl<T> From<unrar::error::UnrarError<T>> for MyError {
fn from(err: unrar::error::UnrarError<T>) -> Self {
// Get details from the error you want,
// or even implement for both T variants.
Self::Unrar
}
}