Use more consistent error messages for bad flattening
This commit is contained in:
parent
c5a3128492
commit
7cf184624a
@ -2095,7 +2095,7 @@ impl<'a, 'de, E> Deserializer<'de> for FlatMapDeserializer<'a, 'de, E>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
Err(Error::custom("can only flatten structs, maps and struct enum variants"))
|
||||
Err(Error::custom("can only flatten structs and maps"))
|
||||
}
|
||||
|
||||
fn deserialize_enum<V>(
|
||||
|
@ -1043,9 +1043,7 @@ where
|
||||
M: SerializeMap + 'a
|
||||
{
|
||||
fn bad_type(self, what: Unsupported) -> M::Error {
|
||||
ser::Error::custom(format_args!(
|
||||
"cannot flatten serialize {} values", what
|
||||
))
|
||||
ser::Error::custom(format_args!("can only flatten structs and maps (got {})", what))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -17,8 +17,8 @@ use self::serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
use self::serde::de::{self, Unexpected};
|
||||
|
||||
extern crate serde_test;
|
||||
use self::serde_test::{assert_de_tokens, assert_de_tokens_error, assert_ser_tokens, assert_tokens,
|
||||
Token};
|
||||
use self::serde_test::{assert_de_tokens, assert_de_tokens_error, assert_ser_tokens,
|
||||
assert_ser_tokens_error, assert_tokens, Token};
|
||||
|
||||
trait MyDefault: Sized {
|
||||
fn my_default() -> Self;
|
||||
@ -1636,3 +1636,37 @@ fn test_complex_flatten() {
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_flatten_unsupported_type() {
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
struct Outer {
|
||||
outer: String,
|
||||
#[serde(flatten)]
|
||||
inner: String,
|
||||
}
|
||||
|
||||
assert_ser_tokens_error(
|
||||
&Outer {
|
||||
outer: "foo".into(),
|
||||
inner: "bar".into(),
|
||||
},
|
||||
&[
|
||||
Token::Map { len: None },
|
||||
Token::Str("outer"),
|
||||
Token::Str("foo"),
|
||||
],
|
||||
"can only flatten structs and maps (got a string)",
|
||||
);
|
||||
assert_de_tokens_error::<Outer>(
|
||||
&[
|
||||
Token::Map { len: None },
|
||||
Token::Str("outer"),
|
||||
Token::Str("foo"),
|
||||
Token::Str("a"),
|
||||
Token::Str("b"),
|
||||
Token::MapEnd
|
||||
],
|
||||
"can only flatten structs and maps",
|
||||
);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user