Capture byte value for error message
This commit is contained in:
parent
1e05fc2145
commit
73a364d4fd
@ -295,7 +295,7 @@ impl Visitor for StringVisitor {
|
|||||||
{
|
{
|
||||||
match str::from_utf8(v) {
|
match str::from_utf8(v) {
|
||||||
Ok(s) => Ok(s.to_owned()),
|
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) {
|
match String::from_utf8(v) {
|
||||||
Ok(s) => Ok(s),
|
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) {
|
match str::from_utf8(value) {
|
||||||
Ok(value) => Err(Error::unknown_variant(value, VARIANTS)),
|
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)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -237,6 +237,9 @@ pub enum Unexpected<'a> {
|
|||||||
/// The input contained a `&str` or `String` that was not expected.
|
/// The input contained a `&str` or `String` that was not expected.
|
||||||
Str(&'a str),
|
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.
|
/// The input contained a unit `()` that was not expected.
|
||||||
Unit,
|
Unit,
|
||||||
|
|
||||||
@ -266,9 +269,6 @@ pub enum Unexpected<'a> {
|
|||||||
|
|
||||||
/// The input contained a struct variant that was not expected.
|
/// The input contained a struct variant that was not expected.
|
||||||
StructVariant,
|
StructVariant,
|
||||||
|
|
||||||
/// The input contained a `&[u8]` or `Vec<u8>` that was not expected.
|
|
||||||
Bytes,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> fmt::Display for Unexpected<'a> {
|
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),
|
Float(f) => write!(formatter, "floating point `{}`", f),
|
||||||
Char(c) => write!(formatter, "character `{}`", c),
|
Char(c) => write!(formatter, "character `{}`", c),
|
||||||
Str(s) => write!(formatter, "string {:?}", s),
|
Str(s) => write!(formatter, "string {:?}", s),
|
||||||
|
Bytes(_) => write!(formatter, "byte array"),
|
||||||
Unit => write!(formatter, "unit value"),
|
Unit => write!(formatter, "unit value"),
|
||||||
Option => write!(formatter, "Option value"),
|
Option => write!(formatter, "Option value"),
|
||||||
NewtypeStruct => write!(formatter, "newtype struct"),
|
NewtypeStruct => write!(formatter, "newtype struct"),
|
||||||
@ -291,7 +292,6 @@ impl<'a> fmt::Display for Unexpected<'a> {
|
|||||||
NewtypeVariant => write!(formatter, "newtype variant"),
|
NewtypeVariant => write!(formatter, "newtype variant"),
|
||||||
TupleVariant => write!(formatter, "tuple variant"),
|
TupleVariant => write!(formatter, "tuple variant"),
|
||||||
StructVariant => write!(formatter, "struct variant"),
|
StructVariant => write!(formatter, "struct variant"),
|
||||||
Bytes => write!(formatter, "byte array"),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -942,7 +942,7 @@ pub trait Visitor: Sized {
|
|||||||
where E: Error,
|
where E: Error,
|
||||||
{
|
{
|
||||||
let _ = v;
|
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`.
|
/// `visit_byte_buf` deserializes a `Vec<u8>` into a `Value`.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user