From 055caa8e24bc64da170e956c929200ccae5ada20 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sun, 18 Feb 2018 19:41:03 +0100 Subject: [PATCH 1/3] add tests for some XML errors --- src/tests.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/tests.rs b/src/tests.rs index e7877158..0db96b70 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -197,3 +197,24 @@ fn namespace_inherited_prefixed2() { assert_eq!(child.name(), "message"); assert_eq!(child.ns(), Some("jabber:client".to_owned())); } + +#[test] +fn xml_error() { + match "".parse::() { + Err(::error::Error(::error::ErrorKind::XmlError(_), _)) => (), + err => panic!("No or wrong error: {:?}", err) + } + + match "() { + Err(::error::Error(::error::ErrorKind::XmlError(_), _)) => (), + err => panic!("No or wrong error: {:?}", err) + } +} + +#[test] +fn invalid_element_error() { + match "".parse::() { + Err(::error::Error(::error::ErrorKind::InvalidElement, _)) => (), + err => panic!("No or wrong error: {:?}", err) + } +} From 236582b24ea098afd7c78703d01dff1cac8f7891 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sun, 18 Feb 2018 19:54:09 +0100 Subject: [PATCH 2/3] update quick_xml and migrate from error-chain to failure --- Cargo.toml | 5 ++-- src/element.rs | 16 +++++------ src/error.rs | 73 +++++++++++++++++++++++++++++++------------------- src/lib.rs | 5 ++-- src/tests.rs | 6 ++--- 5 files changed, 62 insertions(+), 43 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9a29a616..a4ea886e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,5 +20,6 @@ license = "MIT" gitlab = { repository = "lumi/minidom-rs" } [dependencies] -quick-xml = "0.10.0" -error-chain = "0.11.0" +quick-xml = "0.11.0" +failure = "0.1.1" +failure_derive = "0.1.1" diff --git a/src/element.rs b/src/element.rs index d126aa1d..0f3fa9d1 100644 --- a/src/element.rs +++ b/src/element.rs @@ -7,7 +7,7 @@ use std::str; use std::rc::Rc; use std::borrow::Cow; -use error::{Error, ErrorKind, Result}; +use error::{Error, Result}; use quick_xml::reader::Reader as EventReader; use quick_xml::events::{Event, BytesStart}; @@ -309,7 +309,7 @@ impl Element { break build_element(reader, e)?; }, Event::Eof => { - bail!(ErrorKind::EndOfDocument); + return Err(Error::EndOfDocument); }, Event::Text { .. } | Event::End { .. } | @@ -349,23 +349,23 @@ impl Element { match elem.prefix() { Some(prefix) => { if possible_prefix != prefix.as_bytes() { - bail!(ErrorKind::InvalidElementClosed); + return Err(Error::InvalidElementClosed); } }, None => { - bail!(ErrorKind::InvalidElementClosed); + return Err(Error::InvalidElementClosed); }, } if name != elem.name().as_bytes() { - bail!(ErrorKind::InvalidElementClosed); + return Err(Error::InvalidElementClosed); } }, None => { if elem.prefix().is_some() { - bail!(ErrorKind::InvalidElementClosed); + return Err(Error::InvalidElementClosed); } if possible_prefix != elem.name().as_bytes() { - bail!(ErrorKind::InvalidElementClosed); + return Err(Error::InvalidElementClosed); } }, } @@ -676,7 +676,7 @@ fn split_element_name>(s: S) -> Result<(Option, String)> { match name_parts.len() { 2 => Ok((Some(name_parts[0].to_owned()), name_parts[1].to_owned())), 1 => Ok((None, name_parts[0].to_owned())), - _ => bail!(ErrorKind::InvalidElement), + _ => Err(Error::InvalidElement), } } diff --git a/src/error.rs b/src/error.rs index 4079365f..48f33440 100644 --- a/src/error.rs +++ b/src/error.rs @@ -2,34 +2,51 @@ use std::convert::From; -error_chain! { - foreign_links { - XmlError(::quick_xml::errors::Error) - /// An error from quick_xml. - ; - Utf8Error(::std::str::Utf8Error) - /// An UTF-8 conversion error. - ; - IoError(::std::io::Error) - /// An I/O error, from std::io. - ; - } +/// Our main error type. +#[derive(Debug, Fail)] +pub enum Error { + /// An error from quick_xml. + #[fail(display = "XML error: {}", _0)] + XmlError(#[cause] ::quick_xml::errors::Error), - errors { - /// An error which is returned when the end of the document was reached prematurely. - EndOfDocument { - description("the end of the document has been reached prematurely") - display("the end of the document has been reached prematurely") - } - /// An error which is returned when an element is closed when it shouldn't be - InvalidElementClosed { - description("The XML is invalid, an element was wrongly closed") - display("the XML is invalid, an element was wrongly closed") - } - /// An error which is returned when an elemet's name contains more than one colon - InvalidElement { - description("The XML element is invalid") - display("the XML element is invalid") - } + /// An UTF-8 conversion error. + #[fail(display = "UTF-8 error: {}", _0)] + Utf8Error(#[cause] ::std::str::Utf8Error), + + /// An I/O error, from std::io. + #[fail(display = "IO error: {}", _0)] + IoError(#[cause] ::std::io::Error), + + /// An error which is returned when the end of the document was reached prematurely. + #[fail(display = "the end of the document has been reached prematurely")] + EndOfDocument, + + /// An error which is returned when an element is closed when it shouldn't be + #[fail(display = "the XML is invalid, an element was wrongly closed")] + InvalidElementClosed, + + /// An error which is returned when an elemet's name contains more than one colon + #[fail(display = "the XML element is invalid")] + InvalidElement, +} + +impl From<::quick_xml::errors::Error> for Error { + fn from(err: ::quick_xml::errors::Error) -> Error { + Error::XmlError(err) } } + +impl From<::std::str::Utf8Error> for Error { + fn from(err: ::std::str::Utf8Error) -> Error { + Error::Utf8Error(err) + } +} + +impl From<::std::io::Error> for Error { + fn from(err: ::std::io::Error) -> Error { + Error::IoError(err) + } +} + +/// Our simplified Result type. +pub type Result = ::std::result::Result; diff --git a/src/lib.rs b/src/lib.rs index b11fe060..8cf4ab7e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,7 +65,8 @@ //! ``` extern crate quick_xml; -#[macro_use] extern crate error_chain; +extern crate failure; +#[macro_use] extern crate failure_derive; pub mod error; pub mod element; @@ -74,6 +75,6 @@ mod namespace_set; #[cfg(test)] mod tests; -pub use error::{Error, ErrorKind, Result, ResultExt}; +pub use error::{Error, Result}; pub use element::{Element, Node, Children, ChildrenMut, ElementBuilder}; pub use convert::{IntoElements, IntoAttributeValue, ElementEmitter}; diff --git a/src/tests.rs b/src/tests.rs index 0db96b70..7d6c523e 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -201,12 +201,12 @@ fn namespace_inherited_prefixed2() { #[test] fn xml_error() { match "".parse::() { - Err(::error::Error(::error::ErrorKind::XmlError(_), _)) => (), + Err(::error::Error::XmlError(_)) => (), err => panic!("No or wrong error: {:?}", err) } match "() { - Err(::error::Error(::error::ErrorKind::XmlError(_), _)) => (), + Err(::error::Error::XmlError(_)) => (), err => panic!("No or wrong error: {:?}", err) } } @@ -214,7 +214,7 @@ fn xml_error() { #[test] fn invalid_element_error() { match "".parse::() { - Err(::error::Error(::error::ErrorKind::InvalidElement, _)) => (), + Err(::error::Error::InvalidElement) => (), err => panic!("No or wrong error: {:?}", err) } } From 0eba989122a6df1d0d5c75b6707249b9e6425c44 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sun, 18 Feb 2018 20:56:40 +0100 Subject: [PATCH 3/3] bump version to 0.8.0 and add CHANGELOG.md entry --- CHANGELOG.md | 3 +++ Cargo.toml | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 369ef8ad..2e1c16ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +Version 0.8.0, released 2018-02-18: + * Additions + - Link Mauve replaced error\_chain with failure ( https://gitlab.com/lumi/minidom-rs/merge_requests/27 ) Version 0.6.2, released 2017-08-27: * Additions - Link Mauve added an implementation of IntoElements for all Into ( https://gitlab.com/lumi/minidom-rs/merge_requests/19 ) diff --git a/Cargo.toml b/Cargo.toml index a4ea886e..a05552ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "minidom" -version = "0.7.0" +version = "0.8.0" authors = [ "lumi ", "Emmanuel Gil Peyrot ",