Skip to content

Commit

Permalink
Implement std::error::Error for XmlError. Mark helper traits as `pub(…
Browse files Browse the repository at this point in the history
…crate)` to prevent their accidental leakage to public API.
  • Loading branch information
andy128k committed Jul 10, 2022
1 parent dd46a0f commit 8f0e623
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## 0.12.0 - Unreleased

- Wrap `quick_xml::XmlError` into a newtype [`#65`](https://github.com/rust-syndication/atom/pull/65)
- Implement `std::error::Error` for `XmlError`. Mark helper traits as `pub(crate)` to prevent their accidental leakage to public API [`#66`](https://github.com/rust-syndication/atom/pull/66)

## 0.11.0 - 2021-10-20

- Disable clock feature of chrono to mitigate RUSTSEC-2020-0159 [`#57`](https://github.com/rust-syndication/atom/pull/57)
Expand Down
16 changes: 14 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub enum Error {
impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match *self {
Error::Xml(XmlError(ref err)) => Some(err),
Error::Xml(ref err) => Some(err),
Error::Utf8(ref err) => Some(err),
Error::InvalidStartTag => None,
Error::Eof => None,
Expand All @@ -41,7 +41,7 @@ impl StdError for Error {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Error::Xml(XmlError(ref err)) => fmt::Display::fmt(err, f),
Error::Xml(ref err) => fmt::Display::fmt(err, f),
Error::Utf8(ref err) => fmt::Display::fmt(err, f),
Error::InvalidStartTag => write!(f, "input did not begin with an opening feed tag"),
Error::Eof => write!(f, "unexpected end of input"),
Expand Down Expand Up @@ -82,3 +82,15 @@ impl XmlError {
Self(err)
}
}

impl StdError for XmlError {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
self.0.source()
}
}

impl fmt::Display for XmlError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
2 changes: 1 addition & 1 deletion src/fromxml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ use quick_xml::Reader;

use crate::error::Error;

pub trait FromXml: Sized {
pub(crate) trait FromXml: Sized {
fn from_xml<R: BufRead>(reader: &mut Reader<R>, atts: Attributes<'_>) -> Result<Self, Error>;
}
6 changes: 3 additions & 3 deletions src/toxml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use quick_xml::Writer;

use crate::error::XmlError;

pub trait ToXml {
pub(crate) trait ToXml {
fn to_xml<W: Write>(&self, writer: &mut Writer<W>) -> Result<(), XmlError>;
}

Expand All @@ -15,7 +15,7 @@ impl<'a, T: ToXml> ToXml for &'a T {
}
}

pub trait ToXmlNamed {
pub(crate) trait ToXmlNamed {
fn to_xml_named<W, N>(&self, writer: &mut Writer<W>, name: N) -> Result<(), XmlError>
where
W: Write,
Expand All @@ -32,7 +32,7 @@ impl<'a, T: ToXmlNamed> ToXmlNamed for &'a T {
}
}

pub trait WriterExt {
pub(crate) trait WriterExt {
fn write_text_element<N, T>(&mut self, name: N, text: T) -> Result<(), XmlError>
where
N: AsRef<[u8]>,
Expand Down
20 changes: 13 additions & 7 deletions tests/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ fn read_invalid_attribute_namespace() {
assert!(matches!(result, Err(Error::Xml(_))));
}


#[test]
fn read_mismatched_tags() {
let result = Feed::read_from("<feed><a></b></feed>".as_bytes());
Expand Down Expand Up @@ -253,31 +252,38 @@ fn author_internal_invalid_tag() {

#[test]
fn source_internal_invalid_tag() {
let result = Feed::read_from("<feed><entry><source><invalid></source></entry></feed>".as_bytes());
let result =
Feed::read_from("<feed><entry><source><invalid></source></entry></feed>".as_bytes());
assert!(matches!(result, Err(Error::Xml(_))));
}

#[test]
fn content_invalid_xml_lang() {
let result = Feed::read_from("<feed><entry><content xml:lang=\"&;\"></content></entry></feed>".as_bytes());
let result = Feed::read_from(
"<feed><entry><content xml:lang=\"&;\"></content></entry></feed>".as_bytes(),
);
assert!(matches!(result, Err(Error::Xml(_))));
}

#[test]
fn content_invalid_xml_base() {
let result = Feed::read_from("<feed><entry><content xml:base=\"&;\"></content></entry></feed>".as_bytes());
let result = Feed::read_from(
"<feed><entry><content xml:base=\"&;\"></content></entry></feed>".as_bytes(),
);
assert!(matches!(result, Err(Error::Xml(_))));
}

#[test]
fn content_invalid_type() {
let result = Feed::read_from("<feed><entry><content type=\"&;\"></content></entry></feed>".as_bytes());
let result =
Feed::read_from("<feed><entry><content type=\"&;\"></content></entry></feed>".as_bytes());
assert!(matches!(result, Err(Error::Xml(_))));
}

#[test]
fn content_invalid_src() {
let result = Feed::read_from("<feed><entry><content src=\"&;\"></content></entry></feed>".as_bytes());
let result =
Feed::read_from("<feed><entry><content src=\"&;\"></content></entry></feed>".as_bytes());
assert!(matches!(result, Err(Error::Xml(_))));
}

Expand Down Expand Up @@ -309,4 +315,4 @@ fn generator_invalid_uri() {
fn generator_invalid_version() {
let result = Feed::read_from("<feed><generator version=\"&;\"></generator></feed>".as_bytes());
assert!(matches!(result, Err(Error::Xml(_))));
}
}

0 comments on commit 8f0e623

Please sign in to comment.