Added ui tests, Limited serde(tag = "...") to structs with named field

Added ui test struct-representation/internally-tagged-unit
Added ui test struct-representation/internally-tagged-tuple
    
Limited the serde(tag = "...") to enums and structs with named field
This commit is contained in:
Johannes Willbold 2018-12-27 23:01:03 +01:00
parent 9e53405f43
commit 2359417804
6 changed files with 56 additions and 4 deletions

View File

@ -358,16 +358,30 @@ impl Container {
Meta(NameValue(ref m)) if m.ident == "tag" => {
if let Ok(s) = get_lit_str(cx, &m.ident, &m.ident, &m.lit) {
match item.data {
syn::Data::Enum(_) |
syn::Data::Struct(_, ..) => {
syn::Data::Enum(_) => {
internal_tag.set(&m.ident, s.value());
}
syn::Data::Struct(syn::DataStruct{ref fields, ..}) => {
match *fields {
syn::Fields::Named(_) => {
internal_tag.set(&m.ident, s.value());
},
syn::Fields::Unnamed(_) | syn::Fields::Unit => {
cx.error_spanned_by(
fields,
"#[serde(tag = \"...\")] can only be used on enums \
and structs with named fields",
);
}
}
}
syn::Data::Union(syn::DataUnion {
ref union_token, ..
}) => {
cx.error_spanned_by(
union_token,
"#[serde(tag = \"...\")] can only be used on enums",
"#[serde(tag = \"...\")] can only be used on enums \
and structs with named fields",
);
}
}

View File

@ -1411,7 +1411,6 @@ fn test_internally_tagged_struct() {
Token::StructEnd,
],
);
}
#[test]

View File

@ -0,0 +1,11 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)]
#[serde(tag = "type")]
struct S (
u8,
u8
);
fn main() {}

View File

@ -0,0 +1,12 @@
error: #[serde(tag = "...")] can only be used on enums and structs with named fields
--> $DIR/internally-taged-tuple.rs:6:10
|
6 | struct S (
| __________^
7 | | u8,
8 | | u8
9 | | );
| |_^
error: aborting due to previous error

View File

@ -0,0 +1,8 @@
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)]
#[serde(tag = "type")]
struct U;
fn main() {}

View File

@ -0,0 +1,8 @@
error: #[serde(tag = "...")] can only be used on enums and structs with named fields
--> $DIR/internally-tagged-unit.rs:4:10
|
4 | #[derive(Serialize)]
| ^^^^^^^^^
error: aborting due to previous error