diff --git a/src/reader.rs b/src/reader.rs index 421931a..4e6d76f 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -1,6 +1,7 @@ use std::io::{Buffer, Reader, IoError}; use std::num::{cast, NumCast}; use std::error::{Error, FromError}; +use std::fmt; use rustc_serialize::Decoder; @@ -12,6 +13,17 @@ pub struct InvalidEncoding { detail: Option, } +impl fmt::String for InvalidEncoding { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + match *self { + InvalidEncoding { detail: None, desc } => + write!(fmt, "{}", desc), + InvalidEncoding { detail: Some(ref detail), desc } => + write!(fmt, "{} ({})", desc, detail) + } + } +} + /// An error that can be produced during decoding. /// /// If decoding from a Buffer, assume that the buffer has been left @@ -31,6 +43,19 @@ pub enum DecodingError { SizeLimit } +impl fmt::String for DecodingError { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + match *self { + DecodingError::IoError(ref ioerr) => + write!(fmt, "IoError: {}", ioerr), + DecodingError::InvalidEncoding(ref ib) => + write!(fmt, "InvalidEncoding: {}", ib), + DecodingError::SizeLimit => + write!(fmt, "SizeLimit") + } + } +} + pub type DecodingResult = Result; fn wrap_io(err: IoError) -> DecodingError {