From 085a54381375302e6a46d227f1c219be39d71bee Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Thu, 5 May 2016 08:01:07 +0200 Subject: [PATCH 1/3] Implement From for xml::reader::Error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allows users to use `try!(File::open(…))` or other IO-related things in a function that also does XML parsing and returns `xml::reader::Result`. See the added function in `tests/event_reader.rs` for an example. --- src/reader/error.rs | 11 ++++++++++- tests/event_reader.rs | 18 +++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/reader/error.rs b/src/reader/error.rs index 4c8d08b8..eb69edd6 100644 --- a/src/reader/error.rs +++ b/src/reader/error.rs @@ -86,6 +86,15 @@ impl From for 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 +124,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() { From b1992f9412e40af07618c41e721b131c7a6c6024 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Thu, 5 May 2016 08:17:21 +0200 Subject: [PATCH 2/3] =?UTF-8?q?Don=E2=80=99t=20clone/recreate=20in=20`impl?= =?UTF-8?q?=20From=20for=20Error`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/reader/error.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/reader/error.rs b/src/reader/error.rs index eb69edd6..92378e63 100644 --- a/src/reader/error.rs +++ b/src/reader/error.rs @@ -73,14 +73,8 @@ 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), } } } From efaa34aebbe60e97da4e347507ac990b32bafed4 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Thu, 5 May 2016 08:17:56 +0200 Subject: [PATCH 3/3] Version 0.3.2 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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"