Skip to content

Commit

Permalink
Add a special cases of structs: Empty and with $value field
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed Sep 1, 2022
1 parent 49d918d commit 6043d57
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions tests/serde-se.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ struct Nested {
float: f64,
}

#[derive(Serialize)]
struct Empty {}

#[derive(Serialize)]
struct Value {
#[serde(rename = "$value")]
float: f64,
string: &'static str,
}

#[derive(Serialize)]
enum ExternallyTagged {
Unit,
Expand All @@ -160,6 +170,12 @@ enum ExternallyTagged {
nested: Nested,
string: &'static str,
},
Empty {},
Value {
#[serde(rename = "$value")]
float: f64,
string: &'static str,
},
}

#[derive(Serialize)]
Expand All @@ -182,6 +198,12 @@ enum InternallyTagged {
nested: Nested,
string: &'static str,
},
Empty {},
Value {
#[serde(rename = "$value")]
float: f64,
string: &'static str,
},
}

#[derive(Serialize)]
Expand All @@ -203,6 +225,12 @@ enum AdjacentlyTagged {
nested: Nested,
string: &'static str,
},
Empty {},
Value {
#[serde(rename = "$value")]
float: f64,
string: &'static str,
},
}

#[derive(Serialize)]
Expand All @@ -224,6 +252,12 @@ enum Untagged {
nested: Nested,
string: &'static str,
},
Empty {},
Value {
#[serde(rename = "$value")]
float: f64,
string: &'static str,
},
}

mod with_root {
Expand Down Expand Up @@ -273,6 +307,15 @@ mod with_root {
string: "answer",
}
=> r#"<root><float>42</float><string>answer</string></root>"#);
serialize_as!(empty_struct:
Empty {}
=> "<root/>");
serialize_as!(value:
Value {
float: 42.0,
string: "answer"
}
=> r#"<root string="answer">42</root>"#);

mod enum_ {
use super::*;
Expand Down Expand Up @@ -311,6 +354,15 @@ mod with_root {
string: "answer",
}
=> r#"<Flatten><float>42</float><string>answer</string></Flatten>"#);
serialize_as!(empty_struct:
ExternallyTagged::Empty {}
=> "<Empty/>");
serialize_as!(value:
ExternallyTagged::Value {
float: 42.0,
string: "answer"
}
=> r#"<Value string="answer">42</Value>"#);
}

mod internally_tagged {
Expand Down Expand Up @@ -341,6 +393,15 @@ mod with_root {
string: "answer",
}
=> r#"<root><tag>Flatten</tag><float>42</float><string>answer</string></root>"#);
serialize_as!(empty_struct:
InternallyTagged::Empty {}
=> r#"<root tag="Empty"/>"#);
serialize_as!(value:
InternallyTagged::Value {
float: 42.0,
string: "answer"
}
=> r#"<root tag="Value" string="answer">42</root>"#);
}

mod adjacently_tagged {
Expand Down Expand Up @@ -374,6 +435,15 @@ mod with_root {
string: "answer",
}
=> r#"<root tag="Flatten"><content><float>42</float><string>answer</string></content></root>"#);
serialize_as!(empty_struct:
AdjacentlyTagged::Empty {}
=> r#"<root tag="Empty"><content/></root>"#);
serialize_as!(value:
AdjacentlyTagged::Value {
float: 42.0,
string: "answer",
}
=> r#"<root tag="Value"><content string="answer">42</content></root>"#);
}

mod untagged {
Expand Down Expand Up @@ -409,6 +479,15 @@ mod with_root {
string: "answer",
}
=> r#"<root><float>42</float><string>answer</string></root>"#);
serialize_as!(empty_struct:
Untagged::Empty {}
=> "<root/>");
serialize_as!(value:
Untagged::Value {
float: 42.0,
string: "answer"
}
=> r#"<root string="answer">42</root>"#);
}
}
}

0 comments on commit 6043d57

Please sign in to comment.