Capture byte value for error message

This commit is contained in:
David Tolnay 2017-01-22 04:57:12 -08:00
parent 1e05fc2145
commit 73a364d4fd
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 8 additions and 8 deletions

View File

@ -295,7 +295,7 @@ impl Visitor for StringVisitor {
{
match str::from_utf8(v) {
Ok(s) => Ok(s.to_owned()),
Err(_) => Err(Error::invalid_value(Unexpected::Bytes, &self)),
Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)),
}
}
@ -304,7 +304,7 @@ impl Visitor for StringVisitor {
{
match String::from_utf8(v) {
Ok(s) => Ok(s),
Err(_) => Err(Error::invalid_value(Unexpected::Bytes, &self)),
Err(e) => Err(Error::invalid_value(Unexpected::Bytes(&e.into_bytes()), &self)),
}
}
}
@ -1180,7 +1180,7 @@ impl<T, E> Deserialize for Result<T, E> where T: Deserialize, E: Deserialize {
_ => {
match str::from_utf8(value) {
Ok(value) => Err(Error::unknown_variant(value, VARIANTS)),
Err(_) => Err(Error::invalid_value(Unexpected::Bytes, &self)),
Err(_) => Err(Error::invalid_value(Unexpected::Bytes(value), &self)),
}
}
}

View File

@ -237,6 +237,9 @@ pub enum Unexpected<'a> {
/// The input contained a `&str` or `String` that was not expected.
Str(&'a str),
/// The input contained a `&[u8]` or `Vec<u8>` that was not expected.
Bytes(&'a [u8]),
/// The input contained a unit `()` that was not expected.
Unit,
@ -266,9 +269,6 @@ pub enum Unexpected<'a> {
/// The input contained a struct variant that was not expected.
StructVariant,
/// The input contained a `&[u8]` or `Vec<u8>` that was not expected.
Bytes,
}
impl<'a> fmt::Display for Unexpected<'a> {
@ -281,6 +281,7 @@ impl<'a> fmt::Display for Unexpected<'a> {
Float(f) => write!(formatter, "floating point `{}`", f),
Char(c) => write!(formatter, "character `{}`", c),
Str(s) => write!(formatter, "string {:?}", s),
Bytes(_) => write!(formatter, "byte array"),
Unit => write!(formatter, "unit value"),
Option => write!(formatter, "Option value"),
NewtypeStruct => write!(formatter, "newtype struct"),
@ -291,7 +292,6 @@ impl<'a> fmt::Display for Unexpected<'a> {
NewtypeVariant => write!(formatter, "newtype variant"),
TupleVariant => write!(formatter, "tuple variant"),
StructVariant => write!(formatter, "struct variant"),
Bytes => write!(formatter, "byte array"),
}
}
}
@ -942,7 +942,7 @@ pub trait Visitor: Sized {
where E: Error,
{
let _ = v;
Err(Error::invalid_type(Unexpected::Bytes, &self))
Err(Error::invalid_type(Unexpected::Bytes(v), &self))
}
/// `visit_byte_buf` deserializes a `Vec<u8>` into a `Value`.