diff --git a/Cargo.toml b/Cargo.toml index b5ee1a13..8b6cf5e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "minidom" -version = "0.3.3" +version = "0.4.0" authors = ["lumi ", "Emmanuel Gil Peyrot ", "Bastien Orivel "] description = "A small, simple DOM implementation on top of xml-rs." homepage = "https://gitlab.com/lumi/minidom-rs" @@ -15,3 +15,4 @@ gitlab = { repository = "lumi/minidom-rs" } [dependencies] xml-rs = "0.4.1" +error-chain = "0.10.0" diff --git a/src/element.rs b/src/element.rs index c77ed096..3c5c9d52 100644 --- a/src/element.rs +++ b/src/element.rs @@ -7,7 +7,7 @@ use std::collections::btree_map; use std::fmt; -use error::Error; +use error::{Error, ErrorKind, Result}; use xml::reader::{XmlEvent as ReaderEvent, EventReader}; use xml::writer::{XmlEvent as WriterEvent, EventWriter, EmitterConfig}; @@ -100,7 +100,7 @@ impl fmt::Debug for Element { impl FromStr for Element { type Err = Error; - fn from_str(s: &str) -> Result { + fn from_str(s: &str) -> Result { let mut reader = EventReader::new(Cursor::new(s)); Element::from_reader(&mut reader) } @@ -246,7 +246,7 @@ impl Element { } /// Parse a document from an `EventReader`. - pub fn from_reader(reader: &mut EventReader) -> Result { + pub fn from_reader(reader: &mut EventReader) -> Result { loop { let e = reader.next()?; match e { @@ -272,7 +272,7 @@ impl Element { return Ok(root); }, ReaderEvent::EndDocument => { - return Err(Error::EndOfDocument); + bail!(ErrorKind::EndOfDocument); }, _ => () // TODO: may need more errors } @@ -280,7 +280,7 @@ impl Element { } #[cfg_attr(feature = "cargo-clippy", allow(wrong_self_convention))] - fn from_reader_inner(&mut self, reader: &mut EventReader) -> Result<(), Error> { + fn from_reader_inner(&mut self, reader: &mut EventReader) -> Result<()> { loop { let e = reader.next()?; match e { @@ -312,7 +312,7 @@ impl Element { self.append_text_node(s); }, ReaderEvent::EndDocument => { - return Err(Error::EndOfDocument); + bail!(ErrorKind::EndOfDocument); }, _ => (), // TODO: may need to implement more } @@ -320,7 +320,7 @@ impl Element { } /// Output a document to an `EventWriter`. - pub fn write_to(&self, writer: &mut EventWriter) -> Result<(), Error> { + pub fn write_to(&self, writer: &mut EventWriter) -> Result<()> { let name = if let Some(ref ns) = self.namespace { Name::qualified(&self.name, ns, None) } diff --git a/src/error.rs b/src/error.rs index dc818e2f..4e76c6a9 100644 --- a/src/error.rs +++ b/src/error.rs @@ -7,33 +7,24 @@ use std::convert::From; use xml::writer::Error as WriterError; use xml::reader::Error as ReaderError; -/// An enum representing the possible errors. -#[derive(Debug)] -pub enum Error { - /// An io::Error. - IoError(io::Error), - /// An error in the xml-rs `EventWriter`. - XmlWriterError(WriterError), - /// An error in the xml-rs `EventReader`. - XmlReaderError(ReaderError), - /// The end of the document has been reached unexpectedly. - EndOfDocument, -} +error_chain! { + foreign_links { + XmlWriterError(WriterError) + /// An error with writing an XML event, from xml::writer::EventWriter. + ; + XmlReaderError(ReaderError) + /// An error with reading an XML event, from xml::reader::EventReader. + ; + IoError(io::Error) + /// An I/O error, from std::io. + ; + } -impl From for Error { - fn from(err: io::Error) -> Error { - Error::IoError(err) - } -} - -impl From for Error { - fn from(err: WriterError) -> Error { - Error::XmlWriterError(err) - } -} - -impl From for Error { - fn from(err: ReaderError) -> Error { - Error::XmlReaderError(err) + errors { + /// En 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") + } } } diff --git a/src/lib.rs b/src/lib.rs index 9846e756..3539c99b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,6 +65,7 @@ //! ``` extern crate xml; +#[macro_use] extern crate error_chain; pub mod error; pub mod element;