port error.rs to error_chain, bump version to 0.4.0

This commit is contained in:
lumi 2017-05-22 19:20:01 +02:00
parent 6323529cd7
commit 508c971416
4 changed files with 28 additions and 35 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "minidom" name = "minidom"
version = "0.3.3" version = "0.4.0"
authors = ["lumi <lumi@pew.im>", "Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>", "Bastien Orivel <eijebong+minidom@bananium.fr>"] authors = ["lumi <lumi@pew.im>", "Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>", "Bastien Orivel <eijebong+minidom@bananium.fr>"]
description = "A small, simple DOM implementation on top of xml-rs." description = "A small, simple DOM implementation on top of xml-rs."
homepage = "https://gitlab.com/lumi/minidom-rs" homepage = "https://gitlab.com/lumi/minidom-rs"
@ -15,3 +15,4 @@ gitlab = { repository = "lumi/minidom-rs" }
[dependencies] [dependencies]
xml-rs = "0.4.1" xml-rs = "0.4.1"
error-chain = "0.10.0"

View file

@ -7,7 +7,7 @@ use std::collections::btree_map;
use std::fmt; use std::fmt;
use error::Error; use error::{Error, ErrorKind, Result};
use xml::reader::{XmlEvent as ReaderEvent, EventReader}; use xml::reader::{XmlEvent as ReaderEvent, EventReader};
use xml::writer::{XmlEvent as WriterEvent, EventWriter, EmitterConfig}; use xml::writer::{XmlEvent as WriterEvent, EventWriter, EmitterConfig};
@ -100,7 +100,7 @@ impl fmt::Debug for Element {
impl FromStr for Element { impl FromStr for Element {
type Err = Error; type Err = Error;
fn from_str(s: &str) -> Result<Element, Error> { fn from_str(s: &str) -> Result<Element> {
let mut reader = EventReader::new(Cursor::new(s)); let mut reader = EventReader::new(Cursor::new(s));
Element::from_reader(&mut reader) Element::from_reader(&mut reader)
} }
@ -246,7 +246,7 @@ impl Element {
} }
/// Parse a document from an `EventReader`. /// Parse a document from an `EventReader`.
pub fn from_reader<R: Read>(reader: &mut EventReader<R>) -> Result<Element, Error> { pub fn from_reader<R: Read>(reader: &mut EventReader<R>) -> Result<Element> {
loop { loop {
let e = reader.next()?; let e = reader.next()?;
match e { match e {
@ -272,7 +272,7 @@ impl Element {
return Ok(root); return Ok(root);
}, },
ReaderEvent::EndDocument => { ReaderEvent::EndDocument => {
return Err(Error::EndOfDocument); bail!(ErrorKind::EndOfDocument);
}, },
_ => () // TODO: may need more errors _ => () // TODO: may need more errors
} }
@ -280,7 +280,7 @@ impl Element {
} }
#[cfg_attr(feature = "cargo-clippy", allow(wrong_self_convention))] #[cfg_attr(feature = "cargo-clippy", allow(wrong_self_convention))]
fn from_reader_inner<R: Read>(&mut self, reader: &mut EventReader<R>) -> Result<(), Error> { fn from_reader_inner<R: Read>(&mut self, reader: &mut EventReader<R>) -> Result<()> {
loop { loop {
let e = reader.next()?; let e = reader.next()?;
match e { match e {
@ -312,7 +312,7 @@ impl Element {
self.append_text_node(s); self.append_text_node(s);
}, },
ReaderEvent::EndDocument => { ReaderEvent::EndDocument => {
return Err(Error::EndOfDocument); bail!(ErrorKind::EndOfDocument);
}, },
_ => (), // TODO: may need to implement more _ => (), // TODO: may need to implement more
} }
@ -320,7 +320,7 @@ impl Element {
} }
/// Output a document to an `EventWriter`. /// Output a document to an `EventWriter`.
pub fn write_to<W: Write>(&self, writer: &mut EventWriter<W>) -> Result<(), Error> { pub fn write_to<W: Write>(&self, writer: &mut EventWriter<W>) -> Result<()> {
let name = if let Some(ref ns) = self.namespace { let name = if let Some(ref ns) = self.namespace {
Name::qualified(&self.name, ns, None) Name::qualified(&self.name, ns, None)
} }

View file

@ -7,33 +7,24 @@ use std::convert::From;
use xml::writer::Error as WriterError; use xml::writer::Error as WriterError;
use xml::reader::Error as ReaderError; use xml::reader::Error as ReaderError;
/// An enum representing the possible errors. error_chain! {
#[derive(Debug)] foreign_links {
pub enum Error { XmlWriterError(WriterError)
/// An io::Error. /// An error with writing an XML event, from xml::writer::EventWriter.
IoError(io::Error), ;
/// An error in the xml-rs `EventWriter`. XmlReaderError(ReaderError)
XmlWriterError(WriterError), /// An error with reading an XML event, from xml::reader::EventReader.
/// An error in the xml-rs `EventReader`. ;
XmlReaderError(ReaderError), IoError(io::Error)
/// The end of the document has been reached unexpectedly. /// An I/O error, from std::io.
EndOfDocument, ;
} }
impl From<io::Error> for Error { errors {
fn from(err: io::Error) -> Error { /// En error which is returned when the end of the document was reached prematurely.
Error::IoError(err) EndOfDocument {
} description("the end of the document has been reached prematurely")
} display("the end of the document has been reached prematurely")
}
impl From<WriterError> for Error {
fn from(err: WriterError) -> Error {
Error::XmlWriterError(err)
}
}
impl From<ReaderError> for Error {
fn from(err: ReaderError) -> Error {
Error::XmlReaderError(err)
} }
} }

View file

@ -65,6 +65,7 @@
//! ``` //! ```
extern crate xml; extern crate xml;
#[macro_use] extern crate error_chain;
pub mod error; pub mod error;
pub mod element; pub mod element;