diff --git a/Cargo.toml b/Cargo.toml index 52a76565..c171af59 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "xml-rs" -version = "0.3.1" +version = "0.3.2" authors = ["Vladimir Matveev "] license = "MIT" description = "An XML library in pure Rust" diff --git a/src/reader/error.rs b/src/reader/error.rs index 4c8d08b8..92378e63 100644 --- a/src/reader/error.rs +++ b/src/reader/error.rs @@ -73,19 +73,22 @@ impl From for Error { pos: TextPosition::new(), kind: match e { UnexpectedEof => ErrorKind::UnexpectedEof, - Utf8(ref reason) => ErrorKind::Utf8(reason.clone()), - Io(ref io_error) => - ErrorKind::Io( - io::Error::new( - io_error.kind(), - error_description(io_error) - ) - ), + Utf8(reason) => ErrorKind::Utf8(reason), + Io(io_error) => ErrorKind::Io(io_error), } } } } +impl From for Error { + fn from(e: io::Error) -> Self { + Error { + pos: TextPosition::new(), + kind: ErrorKind::Io(e), + } + } +} + impl Clone for ErrorKind { fn clone(&self) -> Self { use self::ErrorKind::*; @@ -115,4 +118,4 @@ impl PartialEq for ErrorKind { } impl Eq for ErrorKind {} -fn error_description(e: &error::Error) -> &str { e.description() } \ No newline at end of file +fn error_description(e: &error::Error) -> &str { e.description() } diff --git a/tests/event_reader.rs b/tests/event_reader.rs index 4e9a72e1..e8dba7c8 100644 --- a/tests/event_reader.rs +++ b/tests/event_reader.rs @@ -2,12 +2,28 @@ extern crate xml; use std::env; use std::fmt; +use std::fs::File; use std::io::{BufRead, BufReader, Write, stderr}; +use std::path::Path; use std::sync::{Once, ONCE_INIT}; use xml::name::OwnedName; use xml::common::Position; -use xml::reader::{Result, XmlEvent, ParserConfig}; +use xml::reader::{Result, XmlEvent, ParserConfig, EventReader}; + +/// Dummy function that opens a file, parses it, and returns a `Result`. +/// There can be IO errors (from `File::open`) and XML errors (from the parser). +/// Having `impl From for xml::reader::Error` allows the user to +/// do this without defining their own error type. +#[allow(dead_code)] +fn count_event_in_file(name: &Path) -> Result { + let mut event_count = 0; + for event in EventReader::new(BufReader::new(try!(File::open(name)))) { + try!(event); + event_count += 1; + } + Ok(event_count) +} #[test] fn sample_1_short() {