Fix the filtered error messages

This commit is contained in:
David Tolnay 2017-01-21 12:34:04 -08:00
commit 3c88a93fb2
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
3 changed files with 24 additions and 21 deletions

View File

@ -518,24 +518,25 @@ fn deserialize_item_enum(
let expecting = format!("enum {}", type_ident); let expecting = format!("enum {}", type_ident);
let variant_names_idents = variants.iter() let variant_names_idents: Vec<_> = variants.iter()
.enumerate() .enumerate()
.filter(|&(_, variant)| !variant.attrs.skip_deserializing()) .filter(|&(_, variant)| !variant.attrs.skip_deserializing())
.map(|(i, variant)| (variant.attrs.name().deserialize_name(), field_i(i))) .map(|(i, variant)| (variant.attrs.name().deserialize_name(), field_i(i)))
.collect(); .collect();
let variants_stmt = {
let variant_names = variant_names_idents.iter().map(|&(ref name, _)| name);
quote! {
const VARIANTS: &'static [&'static str] = &[ #(#variant_names),* ];
}
};
let variant_visitor = deserialize_field_visitor( let variant_visitor = deserialize_field_visitor(
variant_names_idents, variant_names_idents,
item_attrs, item_attrs,
true, true,
); );
let variant_names = variants.iter().map(|variant| variant.attrs.name().deserialize_name());
let variants_stmt = quote! {
const VARIANTS: &'static [&'static str] = &[ #(#variant_names),* ];
};
// Match arms to extract a variant from a string // Match arms to extract a variant from a string
let variant_arms = variants.iter() let variant_arms = variants.iter()
.enumerate() .enumerate()
@ -750,12 +751,19 @@ fn deserialize_struct_visitor(
fields: &[Field], fields: &[Field],
item_attrs: &attr::Item, item_attrs: &attr::Item,
) -> (Tokens, Tokens, Tokens) { ) -> (Tokens, Tokens, Tokens) {
let field_names_idents = fields.iter() let field_names_idents: Vec<_> = fields.iter()
.enumerate() .enumerate()
.filter(|&(_, field)| !field.attrs.skip_deserializing()) .filter(|&(_, field)| !field.attrs.skip_deserializing())
.map(|(i, field)| (field.attrs.name().deserialize_name(), field_i(i))) .map(|(i, field)| (field.attrs.name().deserialize_name(), field_i(i)))
.collect(); .collect();
let fields_stmt = {
let field_names = field_names_idents.iter().map(|&(ref name, _)| name);
quote! {
const FIELDS: &'static [&'static str] = &[ #(#field_names),* ];
}
};
let field_visitor = deserialize_field_visitor( let field_visitor = deserialize_field_visitor(
field_names_idents, field_names_idents,
item_attrs, item_attrs,
@ -770,11 +778,6 @@ fn deserialize_struct_visitor(
item_attrs, item_attrs,
); );
let field_names = fields.iter().map(|field| field.attrs.name().deserialize_name());
let fields_stmt = quote! {
const FIELDS: &'static [&'static str] = &[ #(#field_names),* ];
};
(field_visitor, fields_stmt, visit_map) (field_visitor, fields_stmt, visit_map)
} }

View File

@ -145,7 +145,7 @@ fn test_default_enum() {
assert_de_tokens( assert_de_tokens(
&DefaultEnum::Struct { a1: 1, a2: 2, a3: 3, a4: 0, a5: 123 }, &DefaultEnum::Struct { a1: 1, a2: 2, a3: 3, a4: 0, a5: 123 },
&[ &[
Token::EnumMapStart("DefaultEnum", "Struct", 5), Token::EnumMapStart("DefaultEnum", "Struct", 3),
Token::EnumMapSep, Token::EnumMapSep,
Token::Str("a1"), Token::Str("a1"),
@ -174,7 +174,7 @@ fn test_default_enum() {
assert_de_tokens( assert_de_tokens(
&DefaultEnum::Struct { a1: 1, a2: 0, a3: 123, a4: 0, a5: 123 }, &DefaultEnum::Struct { a1: 1, a2: 0, a3: 123, a4: 0, a5: 123 },
&[ &[
Token::EnumMapStart("DefaultEnum", "Struct", 5), Token::EnumMapStart("DefaultEnum", "Struct", 3),
Token::EnumMapSep, Token::EnumMapSep,
Token::Str("a1"), Token::Str("a1"),

View File

@ -849,7 +849,7 @@ declare_error_tests! {
Token::StructSep, Token::StructSep,
Token::Str("d"), Token::Str("d"),
], ],
Error::Message("unknown field `d`, expected `a` or `b`".to_owned()), Error::Message("unknown field `d`, expected `a`".to_owned()),
} }
test_skipped_field_is_unknown<StructDenyUnknown> { test_skipped_field_is_unknown<StructDenyUnknown> {
&[ &[
@ -857,7 +857,7 @@ declare_error_tests! {
Token::StructSep, Token::StructSep,
Token::Str("b"), Token::Str("b"),
], ],
Error::Message("unknown field `b`, expected `a` or `b`".to_owned()), Error::Message("unknown field `b`, expected `a`".to_owned()),
} }
test_skip_all_deny_unknown<StructSkipAllDenyUnknown> { test_skip_all_deny_unknown<StructSkipAllDenyUnknown> {
&[ &[
@ -865,25 +865,25 @@ declare_error_tests! {
Token::StructSep, Token::StructSep,
Token::Str("a"), Token::Str("a"),
], ],
Error::Message("unknown field `a`, expected `a`".to_owned()), Error::Message("unknown field `a`, there are no fields".to_owned()),
} }
test_unknown_variant<Enum> { test_unknown_variant<Enum> {
&[ &[
Token::EnumUnit("Enum", "Foo"), Token::EnumUnit("Enum", "Foo"),
], ],
Error::Message("unknown variant `Foo`, expected one of `Skipped`, `Unit`, `Simple`, `Seq`, `Map`".to_owned()), Error::Message("unknown variant `Foo`, expected one of `Unit`, `Simple`, `Seq`, `Map`".to_owned()),
} }
test_enum_skipped_variant<Enum> { test_enum_skipped_variant<Enum> {
&[ &[
Token::EnumUnit("Enum", "Skipped"), Token::EnumUnit("Enum", "Skipped"),
], ],
Error::Message("unknown variant `Skipped`, expected one of `Skipped`, `Unit`, `Simple`, `Seq`, `Map`".to_owned()), Error::Message("unknown variant `Skipped`, expected one of `Unit`, `Simple`, `Seq`, `Map`".to_owned()),
} }
test_enum_skip_all<EnumSkipAll> { test_enum_skip_all<EnumSkipAll> {
&[ &[
Token::EnumUnit("EnumSkipAll", "Skipped"), Token::EnumUnit("EnumSkipAll", "Skipped"),
], ],
Error::Message("unknown variant `Skipped`, expected `Skipped`".to_owned()), Error::Message("unknown variant `Skipped`, there are no variants".to_owned()),
} }
test_struct_seq_too_long<Struct> { test_struct_seq_too_long<Struct> {
&[ &[