Skip to content

Commit

Permalink
Adapt error-handling to quick-xml 0.37
Browse files Browse the repository at this point in the history
  • Loading branch information
jqnatividad committed Oct 30, 2024
1 parent eb62a62 commit 294ead7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/ods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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}"),
}
}
}
Expand All @@ -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,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
};
Expand Down
11 changes: 10 additions & 1 deletion src/xlsb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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}"),
}
}
}
Expand Down Expand Up @@ -183,7 +187,12 @@ impl<RS: Read + Seek> Xlsb<RS> {
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(),
);
}
_ => (),
}
Expand Down
14 changes: 14 additions & 0 deletions src/xlsx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 {
Expand Down Expand Up @@ -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}"),
}
}
}
Expand All @@ -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,
}
}
Expand Down

0 comments on commit 294ead7

Please sign in to comment.