Remove InvalidEncoding struct (#111)

This commit is contained in:
Lena Hellström 2017-02-10 12:19:58 -06:00 committed by Ty Overby
parent ffbe4387dd
commit cac9301615
3 changed files with 20 additions and 32 deletions

View File

@ -39,7 +39,10 @@ pub enum ErrorKind {
/// encoding, this error will be returned. This error is only possible
/// if a stream is corrupted. A stream produced from `encode` or `encode_into`
/// should **never** produce an InvalidEncoding error.
InvalidEncoding(InvalidEncoding),
InvalidEncoding{
desc: &'static str,
detail: Option<String>
},
/// If (de)serializing a message takes more than the provided size limit, this
/// error is returned.
SizeLimit,
@ -51,7 +54,7 @@ impl error::Error for ErrorKind {
fn description(&self) -> &str {
match *self {
ErrorKind::IoError(ref err) => error::Error::description(err),
ErrorKind::InvalidEncoding(ref ib) => ib.desc,
ErrorKind::InvalidEncoding{desc, ..} => desc,
ErrorKind::SequenceMustHaveLength => "bincode can't encode infinite sequences",
ErrorKind::SizeLimit => "the size limit for decoding has been reached",
ErrorKind::Custom(ref msg) => msg,
@ -62,7 +65,7 @@ impl error::Error for ErrorKind {
fn cause(&self) -> Option<&error::Error> {
match *self {
ErrorKind::IoError(ref err) => err.cause(),
ErrorKind::InvalidEncoding(_) => None,
ErrorKind::InvalidEncoding{..} => None,
ErrorKind::SequenceMustHaveLength => None,
ErrorKind::SizeLimit => None,
ErrorKind::Custom(_) => None,
@ -81,8 +84,10 @@ impl fmt::Display for ErrorKind {
match *self {
ErrorKind::IoError(ref ioerr) =>
write!(fmt, "IoError: {}", ioerr),
ErrorKind::InvalidEncoding(ref ib) =>
write!(fmt, "InvalidEncoding: {}", ib),
ErrorKind::InvalidEncoding{desc, detail: None}=>
write!(fmt, "InvalidEncoding: {}", desc),
ErrorKind::InvalidEncoding{desc, detail: Some(ref detail)}=>
write!(fmt, "InvalidEncoding: {} ({})", desc, detail),
ErrorKind::SequenceMustHaveLength =>
write!(fmt, "Bincode can only encode sequences and maps that have a knowable size ahead of time."),
ErrorKind::SizeLimit =>
@ -105,23 +110,6 @@ impl serde::ser::Error for Error {
}
}
#[derive(Eq, PartialEq, Clone, Debug)]
pub struct InvalidEncoding {
pub desc: &'static str,
pub detail: Option<String>,
}
impl fmt::Display 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)
}
}
}
/// Serializes an object directly into a `Writer`.
///
/// If the serialization would take more bytes than allowed by `size_limit`, an error

View File

@ -5,7 +5,7 @@ use serde_crate as serde;
use serde_crate::de::value::ValueDeserializer;
use serde_crate::de::Error as DeError;
use ::SizeLimit;
use super::{Result, Error, ErrorKind, InvalidEncoding};
use super::{Result, Error, ErrorKind};
/// A Deserializer that reads bytes from a buffer.
///
@ -59,10 +59,10 @@ impl<R: Read> Deserializer<R> {
try!(self.reader.by_ref().take(len as u64).read_to_end(&mut buffer));
String::from_utf8(buffer).map_err(|err|
ErrorKind::InvalidEncoding(InvalidEncoding {
ErrorKind::InvalidEncoding{
desc: "error while decoding utf8 string",
detail: Some(format!("Deserialize error: {}", err))
}).into())
}.into())
}
}
@ -99,10 +99,10 @@ impl<'a, R: Read> serde::Deserializer for &'a mut Deserializer<R> {
1 => visitor.visit_bool(true),
0 => visitor.visit_bool(false),
value => {
Err(ErrorKind::InvalidEncoding(InvalidEncoding {
Err(ErrorKind::InvalidEncoding{
desc: "invalid u8 when decoding bool",
detail: Some(format!("Expected 0 or 1, got {}", value))
}).into())
}.into())
}
}
}
@ -144,10 +144,10 @@ impl<'a, R: Read> serde::Deserializer for &'a mut Deserializer<R> {
{
use std::str;
let error = ErrorKind::InvalidEncoding(InvalidEncoding {
let error = ErrorKind::InvalidEncoding{
desc: "Invalid char encoding",
detail: None
}).into();
}.into();
let mut buf = [0];
@ -280,10 +280,10 @@ impl<'a, R: Read> serde::Deserializer for &'a mut Deserializer<R> {
match value {
0 => visitor.visit_none(),
1 => visitor.visit_some(&mut *self),
_ => Err(ErrorKind::InvalidEncoding(InvalidEncoding {
_ => Err(ErrorKind::InvalidEncoding{
desc: "invalid tag when decoding Option",
detail: Some(format!("Expected 0 or 1, got {}", value))
}).into()),
}.into()),
}
}

View File

@ -213,7 +213,7 @@ fn test_fixed_size_array() {
fn deserializing_errors() {
fn isize_invalid_deserialize<T: Debug>(res: Result<T>) {
match res.map_err(|e| *e) {
Err(ErrorKind::InvalidEncoding(_)) => {},
Err(ErrorKind::InvalidEncoding{..}) => {},
Err(ErrorKind::Custom(ref s)) if s.contains("invalid encoding") => {},
Err(ErrorKind::Custom(ref s)) if s.contains("invalid value") => {},
other => panic!("Expecting InvalidEncoding, got {:?}", other),