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]
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>"]
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"

View file

@ -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<Element, Error> {
fn from_str(s: &str) -> Result<Element> {
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<R: Read>(reader: &mut EventReader<R>) -> Result<Element, Error> {
pub fn from_reader<R: Read>(reader: &mut EventReader<R>) -> Result<Element> {
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<R: Read>(&mut self, reader: &mut EventReader<R>) -> Result<(), Error> {
fn from_reader_inner<R: Read>(&mut self, reader: &mut EventReader<R>) -> 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<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 {
Name::qualified(&self.name, ns, None)
}

View file

@ -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<io::Error> for Error {
fn from(err: io::Error) -> Error {
Error::IoError(err)
}
}
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)
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")
}
}
}

View file

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