diff --git a/src/ods.rs b/src/ods.rs index fd79e4e..3135843 100644 --- a/src/ods.rs +++ b/src/ods.rs @@ -60,6 +60,11 @@ pub enum OdsError { Password, /// Worksheet not found WorksheetNotFound(String), + + /// XML attribute error + AttrError(quick_xml::events::attributes::AttrError), + /// XML encoding error + EncodingError(quick_xml::encoding::EncodingError), } /// Ods reader options @@ -74,6 +79,8 @@ from_err!(zip::result::ZipError, OdsError, Zip); from_err!(quick_xml::Error, OdsError, Xml); from_err!(std::string::ParseError, OdsError, Parse); from_err!(std::num::ParseFloatError, OdsError, ParseFloat); +from_err!(quick_xml::events::attributes::AttrError, OdsError, Xml); +from_err!(quick_xml::encoding::EncodingError, OdsError, Xml); impl std::fmt::Display for OdsError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { @@ -94,6 +101,8 @@ impl std::fmt::Display for OdsError { } OdsError::Password => write!(f, "Workbook is password protected"), OdsError::WorksheetNotFound(name) => write!(f, "Worksheet '{name}' not found"), + OdsError::AttrError(e) => write!(f, "XML Attribute Error: {e}"), + OdsError::EncodingError(e) => write!(f, "XML Encoding Error: {e}"), } } } @@ -107,6 +116,8 @@ impl std::error::Error for OdsError { OdsError::Parse(e) => Some(e), OdsError::ParseInt(e) => Some(e), OdsError::ParseFloat(e) => Some(e), + OdsError::AttrError(e) => Some(e), + OdsError::EncodingError(e) => Some(e), _ => None, } } diff --git a/src/utils.rs b/src/utils.rs index 8d51e26..0f85df0 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -4,7 +4,7 @@ macro_rules! from_err { ($from:ty, $to:tt, $var:tt) => { impl From<$from> for $to { fn from(e: $from) -> $to { - $to::$var(e) + $to::$var(e.into()) } } }; diff --git a/src/xlsb/mod.rs b/src/xlsb/mod.rs index fca4bed..aeaddce 100644 --- a/src/xlsb/mod.rs +++ b/src/xlsb/mod.rs @@ -80,11 +80,14 @@ pub enum XlsbError { Password, /// Worksheet not found WorksheetNotFound(String), + /// XML Encoding error + Encoding(quick_xml::encoding::EncodingError), } from_err!(std::io::Error, XlsbError, Io); from_err!(zip::result::ZipError, XlsbError, Zip); from_err!(quick_xml::Error, XlsbError, Xml); +from_err!(quick_xml::encoding::EncodingError, XlsbError, Encoding); impl std::fmt::Display for XlsbError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { @@ -114,6 +117,7 @@ impl std::fmt::Display for XlsbError { } XlsbError::Password => write!(f, "Workbook is password protected"), XlsbError::WorksheetNotFound(name) => write!(f, "Worksheet '{name}' not found"), + XlsbError::Encoding(e) => write!(f, "Encoding error: {e}"), } } } @@ -183,7 +187,12 @@ impl Xlsb { key: QName(b"Target"), value: v, } => { - target = Some(xml.decoder().decode(&v)?.into_owned()); + target = Some( + xml.decoder() + .decode(&v) + .map_err(XlsbError::Encoding)? + .into_owned(), + ); } _ => (), } diff --git a/src/xlsx/mod.rs b/src/xlsx/mod.rs index 802a071..d51c70b 100644 --- a/src/xlsx/mod.rs +++ b/src/xlsx/mod.rs @@ -89,6 +89,10 @@ pub enum XlsxError { TableNotFound(String), /// The specified sheet is not a worksheet NotAWorksheet(String), + /// XML Encoding error + Encoding(quick_xml::encoding::EncodingError), + /// XML attribute error + XmlAttribute(quick_xml::events::attributes::AttrError), } from_err!(std::io::Error, XlsxError, Io); @@ -98,6 +102,12 @@ from_err!(quick_xml::Error, XlsxError, Xml); from_err!(std::string::ParseError, XlsxError, Parse); from_err!(std::num::ParseFloatError, XlsxError, ParseFloat); from_err!(std::num::ParseIntError, XlsxError, ParseInt); +from_err!(quick_xml::encoding::EncodingError, XlsxError, Encoding); +from_err!( + quick_xml::events::attributes::AttrError, + XlsxError, + XmlAttribute +); impl std::fmt::Display for XlsxError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { @@ -139,6 +149,8 @@ impl std::fmt::Display for XlsxError { XlsxError::Password => write!(f, "Workbook is password protected"), XlsxError::TableNotFound(n) => write!(f, "Table '{n}' not found"), XlsxError::NotAWorksheet(typ) => write!(f, "Expecting a worksheet, got {typ}"), + XlsxError::Encoding(e) => write!(f, "Encoding error: {e}"), + XlsxError::XmlAttribute(e) => write!(f, "XML attribute error: {e}"), } } } @@ -153,6 +165,8 @@ impl std::error::Error for XlsxError { XlsxError::Parse(e) => Some(e), XlsxError::ParseInt(e) => Some(e), XlsxError::ParseFloat(e) => Some(e), + XlsxError::Encoding(e) => Some(e), + XlsxError::XmlAttribute(e) => Some(e), _ => None, } }