diff --git a/src/tape/changer/sg_pt_changer.rs b/src/tape/changer/sg_pt_changer.rs index ed63b615..785fc9ce 100644 --- a/src/tape/changer/sg_pt_changer.rs +++ b/src/tape/changer/sg_pt_changer.rs @@ -28,6 +28,7 @@ use crate::{ SENSE_KEY_UNIT_ATTENTION, SENSE_KEY_NOT_READY, InquiryInfo, + ScsiError, scsi_ascii_to_string, scsi_inquiry, }, @@ -103,7 +104,7 @@ fn execute_scsi_command( if !retry { bail!("{} failed: {}", error_prefix, err); } - if let Some(ref sense) = err.sense { + if let ScsiError::Sense(ref sense) = err { if sense.sense_key == SENSE_KEY_NO_SENSE || sense.sense_key == SENSE_KEY_RECOVERED_ERROR || diff --git a/src/tools/sgutils2.rs b/src/tools/sgutils2.rs index 37bc2dff..987d5738 100644 --- a/src/tools/sgutils2.rs +++ b/src/tools/sgutils2.rs @@ -44,32 +44,38 @@ impl ToString for SenseInfo { } #[derive(Debug)] -pub struct ScsiError { - pub error: Error, - pub sense: Option, +pub enum ScsiError { + Error(Error), + Sense(SenseInfo), } impl std::fmt::Display for ScsiError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.error) + match self { + ScsiError::Error(err) => write!(f, "{}", err), + ScsiError::Sense(sense) => write!(f, "{}", sense.to_string()), + } } } impl std::error::Error for ScsiError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - self.error.source() + match self { + ScsiError::Error(err) => err.source(), + ScsiError::Sense(_) => None, + } } } impl From for ScsiError { fn from(error: anyhow::Error) -> Self { - Self { error, sense: None } + Self::Error(error) } } impl From for ScsiError { fn from(error: std::io::Error) -> Self { - Self { error: error.into(), sense: None } + Self::Error(error.into()) } } @@ -483,10 +489,7 @@ impl <'a, F: AsRawFd> SgRaw<'a, F> { } }; - return Err(ScsiError { - error: format_err!("{}", sense.to_string()), - sense: Some(sense), - }); + return Err(ScsiError::Sense(sense)); } SCSI_PT_RESULT_TRANSPORT_ERR => return Err(format_err!("scsi command failed: transport error").into()), SCSI_PT_RESULT_OS_ERR => {