Remove InvalidEncoding struct (#111)
This commit is contained in:
parent
ffbe4387dd
commit
cac9301615
|
|
@ -39,7 +39,10 @@ pub enum ErrorKind {
|
||||||
/// encoding, this error will be returned. This error is only possible
|
/// encoding, this error will be returned. This error is only possible
|
||||||
/// if a stream is corrupted. A stream produced from `encode` or `encode_into`
|
/// if a stream is corrupted. A stream produced from `encode` or `encode_into`
|
||||||
/// should **never** produce an InvalidEncoding error.
|
/// 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
|
/// If (de)serializing a message takes more than the provided size limit, this
|
||||||
/// error is returned.
|
/// error is returned.
|
||||||
SizeLimit,
|
SizeLimit,
|
||||||
|
|
@ -51,7 +54,7 @@ impl error::Error for ErrorKind {
|
||||||
fn description(&self) -> &str {
|
fn description(&self) -> &str {
|
||||||
match *self {
|
match *self {
|
||||||
ErrorKind::IoError(ref err) => error::Error::description(err),
|
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::SequenceMustHaveLength => "bincode can't encode infinite sequences",
|
||||||
ErrorKind::SizeLimit => "the size limit for decoding has been reached",
|
ErrorKind::SizeLimit => "the size limit for decoding has been reached",
|
||||||
ErrorKind::Custom(ref msg) => msg,
|
ErrorKind::Custom(ref msg) => msg,
|
||||||
|
|
@ -62,7 +65,7 @@ impl error::Error for ErrorKind {
|
||||||
fn cause(&self) -> Option<&error::Error> {
|
fn cause(&self) -> Option<&error::Error> {
|
||||||
match *self {
|
match *self {
|
||||||
ErrorKind::IoError(ref err) => err.cause(),
|
ErrorKind::IoError(ref err) => err.cause(),
|
||||||
ErrorKind::InvalidEncoding(_) => None,
|
ErrorKind::InvalidEncoding{..} => None,
|
||||||
ErrorKind::SequenceMustHaveLength => None,
|
ErrorKind::SequenceMustHaveLength => None,
|
||||||
ErrorKind::SizeLimit => None,
|
ErrorKind::SizeLimit => None,
|
||||||
ErrorKind::Custom(_) => None,
|
ErrorKind::Custom(_) => None,
|
||||||
|
|
@ -81,8 +84,10 @@ impl fmt::Display for ErrorKind {
|
||||||
match *self {
|
match *self {
|
||||||
ErrorKind::IoError(ref ioerr) =>
|
ErrorKind::IoError(ref ioerr) =>
|
||||||
write!(fmt, "IoError: {}", ioerr),
|
write!(fmt, "IoError: {}", ioerr),
|
||||||
ErrorKind::InvalidEncoding(ref ib) =>
|
ErrorKind::InvalidEncoding{desc, detail: None}=>
|
||||||
write!(fmt, "InvalidEncoding: {}", ib),
|
write!(fmt, "InvalidEncoding: {}", desc),
|
||||||
|
ErrorKind::InvalidEncoding{desc, detail: Some(ref detail)}=>
|
||||||
|
write!(fmt, "InvalidEncoding: {} ({})", desc, detail),
|
||||||
ErrorKind::SequenceMustHaveLength =>
|
ErrorKind::SequenceMustHaveLength =>
|
||||||
write!(fmt, "Bincode can only encode sequences and maps that have a knowable size ahead of time."),
|
write!(fmt, "Bincode can only encode sequences and maps that have a knowable size ahead of time."),
|
||||||
ErrorKind::SizeLimit =>
|
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`.
|
/// Serializes an object directly into a `Writer`.
|
||||||
///
|
///
|
||||||
/// If the serialization would take more bytes than allowed by `size_limit`, an error
|
/// If the serialization would take more bytes than allowed by `size_limit`, an error
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ use serde_crate as serde;
|
||||||
use serde_crate::de::value::ValueDeserializer;
|
use serde_crate::de::value::ValueDeserializer;
|
||||||
use serde_crate::de::Error as DeError;
|
use serde_crate::de::Error as DeError;
|
||||||
use ::SizeLimit;
|
use ::SizeLimit;
|
||||||
use super::{Result, Error, ErrorKind, InvalidEncoding};
|
use super::{Result, Error, ErrorKind};
|
||||||
|
|
||||||
/// A Deserializer that reads bytes from a buffer.
|
/// 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));
|
try!(self.reader.by_ref().take(len as u64).read_to_end(&mut buffer));
|
||||||
|
|
||||||
String::from_utf8(buffer).map_err(|err|
|
String::from_utf8(buffer).map_err(|err|
|
||||||
ErrorKind::InvalidEncoding(InvalidEncoding {
|
ErrorKind::InvalidEncoding{
|
||||||
desc: "error while decoding utf8 string",
|
desc: "error while decoding utf8 string",
|
||||||
detail: Some(format!("Deserialize error: {}", err))
|
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),
|
1 => visitor.visit_bool(true),
|
||||||
0 => visitor.visit_bool(false),
|
0 => visitor.visit_bool(false),
|
||||||
value => {
|
value => {
|
||||||
Err(ErrorKind::InvalidEncoding(InvalidEncoding {
|
Err(ErrorKind::InvalidEncoding{
|
||||||
desc: "invalid u8 when decoding bool",
|
desc: "invalid u8 when decoding bool",
|
||||||
detail: Some(format!("Expected 0 or 1, got {}", value))
|
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;
|
use std::str;
|
||||||
|
|
||||||
let error = ErrorKind::InvalidEncoding(InvalidEncoding {
|
let error = ErrorKind::InvalidEncoding{
|
||||||
desc: "Invalid char encoding",
|
desc: "Invalid char encoding",
|
||||||
detail: None
|
detail: None
|
||||||
}).into();
|
}.into();
|
||||||
|
|
||||||
let mut buf = [0];
|
let mut buf = [0];
|
||||||
|
|
||||||
|
|
@ -280,10 +280,10 @@ impl<'a, R: Read> serde::Deserializer for &'a mut Deserializer<R> {
|
||||||
match value {
|
match value {
|
||||||
0 => visitor.visit_none(),
|
0 => visitor.visit_none(),
|
||||||
1 => visitor.visit_some(&mut *self),
|
1 => visitor.visit_some(&mut *self),
|
||||||
_ => Err(ErrorKind::InvalidEncoding(InvalidEncoding {
|
_ => Err(ErrorKind::InvalidEncoding{
|
||||||
desc: "invalid tag when decoding Option",
|
desc: "invalid tag when decoding Option",
|
||||||
detail: Some(format!("Expected 0 or 1, got {}", value))
|
detail: Some(format!("Expected 0 or 1, got {}", value))
|
||||||
}).into()),
|
}.into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -213,7 +213,7 @@ fn test_fixed_size_array() {
|
||||||
fn deserializing_errors() {
|
fn deserializing_errors() {
|
||||||
fn isize_invalid_deserialize<T: Debug>(res: Result<T>) {
|
fn isize_invalid_deserialize<T: Debug>(res: Result<T>) {
|
||||||
match res.map_err(|e| *e) {
|
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 encoding") => {},
|
||||||
Err(ErrorKind::Custom(ref s)) if s.contains("invalid value") => {},
|
Err(ErrorKind::Custom(ref s)) if s.contains("invalid value") => {},
|
||||||
other => panic!("Expecting InvalidEncoding, got {:?}", other),
|
other => panic!("Expecting InvalidEncoding, got {:?}", other),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue