Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement From<std::io::Error> for xml::reader::Error #121

Merged
merged 3 commits into from
May 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <[email protected]>"]
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