Skip to content

Commit

Permalink
Merge pull request #121 from SimonSapin/io-error
Browse files Browse the repository at this point in the history
Implement From<std::io::Error> for xml::reader::Error
  • Loading branch information
netvl committed May 5, 2016
2 parents 1678143 + efaa34a commit f5b31f3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "xml-rs"
version = "0.3.1"
version = "0.3.2"
authors = ["Vladimir Matveev <vladimir.matweev@gmail.com>"]
license = "MIT"
description = "An XML library in pure Rust"
Expand Down
21 changes: 12 additions & 9 deletions src/reader/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,22 @@ impl From<util::CharReadError> 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<io::Error> 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::*;
Expand Down Expand Up @@ -115,4 +118,4 @@ impl PartialEq for ErrorKind {
}
impl Eq for ErrorKind {}

fn error_description(e: &error::Error) -> &str { e.description() }
fn error_description(e: &error::Error) -> &str { e.description() }
18 changes: 17 additions & 1 deletion tests/event_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::io::Error> 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<usize> {
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() {
Expand Down

0 comments on commit f5b31f3

Please sign in to comment.