Skip to content

Commit 6043d57

Browse files
committed
Add a special cases of structs: Empty and with $value field
1 parent 49d918d commit 6043d57

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

tests/serde-se.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ struct Nested {
140140
float: f64,
141141
}
142142

143+
#[derive(Serialize)]
144+
struct Empty {}
145+
146+
#[derive(Serialize)]
147+
struct Value {
148+
#[serde(rename = "$value")]
149+
float: f64,
150+
string: &'static str,
151+
}
152+
143153
#[derive(Serialize)]
144154
enum ExternallyTagged {
145155
Unit,
@@ -160,6 +170,12 @@ enum ExternallyTagged {
160170
nested: Nested,
161171
string: &'static str,
162172
},
173+
Empty {},
174+
Value {
175+
#[serde(rename = "$value")]
176+
float: f64,
177+
string: &'static str,
178+
},
163179
}
164180

165181
#[derive(Serialize)]
@@ -182,6 +198,12 @@ enum InternallyTagged {
182198
nested: Nested,
183199
string: &'static str,
184200
},
201+
Empty {},
202+
Value {
203+
#[serde(rename = "$value")]
204+
float: f64,
205+
string: &'static str,
206+
},
185207
}
186208

187209
#[derive(Serialize)]
@@ -203,6 +225,12 @@ enum AdjacentlyTagged {
203225
nested: Nested,
204226
string: &'static str,
205227
},
228+
Empty {},
229+
Value {
230+
#[serde(rename = "$value")]
231+
float: f64,
232+
string: &'static str,
233+
},
206234
}
207235

208236
#[derive(Serialize)]
@@ -224,6 +252,12 @@ enum Untagged {
224252
nested: Nested,
225253
string: &'static str,
226254
},
255+
Empty {},
256+
Value {
257+
#[serde(rename = "$value")]
258+
float: f64,
259+
string: &'static str,
260+
},
227261
}
228262

229263
mod with_root {
@@ -273,6 +307,15 @@ mod with_root {
273307
string: "answer",
274308
}
275309
=> r#"<root><float>42</float><string>answer</string></root>"#);
310+
serialize_as!(empty_struct:
311+
Empty {}
312+
=> "<root/>");
313+
serialize_as!(value:
314+
Value {
315+
float: 42.0,
316+
string: "answer"
317+
}
318+
=> r#"<root string="answer">42</root>"#);
276319

277320
mod enum_ {
278321
use super::*;
@@ -311,6 +354,15 @@ mod with_root {
311354
string: "answer",
312355
}
313356
=> r#"<Flatten><float>42</float><string>answer</string></Flatten>"#);
357+
serialize_as!(empty_struct:
358+
ExternallyTagged::Empty {}
359+
=> "<Empty/>");
360+
serialize_as!(value:
361+
ExternallyTagged::Value {
362+
float: 42.0,
363+
string: "answer"
364+
}
365+
=> r#"<Value string="answer">42</Value>"#);
314366
}
315367

316368
mod internally_tagged {
@@ -341,6 +393,15 @@ mod with_root {
341393
string: "answer",
342394
}
343395
=> r#"<root><tag>Flatten</tag><float>42</float><string>answer</string></root>"#);
396+
serialize_as!(empty_struct:
397+
InternallyTagged::Empty {}
398+
=> r#"<root tag="Empty"/>"#);
399+
serialize_as!(value:
400+
InternallyTagged::Value {
401+
float: 42.0,
402+
string: "answer"
403+
}
404+
=> r#"<root tag="Value" string="answer">42</root>"#);
344405
}
345406

346407
mod adjacently_tagged {
@@ -374,6 +435,15 @@ mod with_root {
374435
string: "answer",
375436
}
376437
=> r#"<root tag="Flatten"><content><float>42</float><string>answer</string></content></root>"#);
438+
serialize_as!(empty_struct:
439+
AdjacentlyTagged::Empty {}
440+
=> r#"<root tag="Empty"><content/></root>"#);
441+
serialize_as!(value:
442+
AdjacentlyTagged::Value {
443+
float: 42.0,
444+
string: "answer",
445+
}
446+
=> r#"<root tag="Value"><content string="answer">42</content></root>"#);
377447
}
378448

379449
mod untagged {
@@ -409,6 +479,15 @@ mod with_root {
409479
string: "answer",
410480
}
411481
=> r#"<root><float>42</float><string>answer</string></root>"#);
482+
serialize_as!(empty_struct:
483+
Untagged::Empty {}
484+
=> "<root/>");
485+
serialize_as!(value:
486+
Untagged::Value {
487+
float: 42.0,
488+
string: "answer"
489+
}
490+
=> r#"<root string="answer">42</root>"#);
412491
}
413492
}
414493
}

0 commit comments

Comments
 (0)