forked from luminvent/yaserde
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneric.rs
More file actions
121 lines (106 loc) · 2.83 KB
/
generic.rs
File metadata and controls
121 lines (106 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#![allow(dead_code)]
use yaserde::*;
#[derive(YaSerialize, YaDeserialize, Debug, Default, Clone, Eq, PartialEq)]
pub struct Header {}
#[derive(YaSerialize, YaDeserialize, Debug, Default, Clone, Eq, PartialEq)]
#[yaserde(
rename = "Envelope",
namespaces = {
"s" = "http://schemas.xmlsoap.org/soap/envelope/",
},
prefix = "s"
)]
pub struct SoapEnvelope<BODY>
where
BODY: YaSerialize + YaDeserialize + Default,
{
#[yaserde(rename = "encodingStyle", prefix = "s", attribute = true)]
pub encoding_style: String,
#[yaserde(rename = "u", prefix = "xmlns", attribute = true)]
pub tnsattr: Option<String>,
#[yaserde(rename = "urn", prefix = "xmlns", attribute = true)]
pub urnattr: Option<String>,
#[yaserde(rename = "xsi", prefix = "xmlns", attribute = true)]
pub xsiattr: Option<String>,
#[yaserde(rename = "Header", prefix = "s")]
pub header: Option<Header>,
#[yaserde(rename = "Body", prefix = "s")]
pub body: BODY,
}
#[derive(YaSerialize, YaDeserialize, Debug, Default, Clone, Eq, PartialEq)]
#[yaserde(namespaces = {
"u" = "urn:schemas-upnp-org:service:AVTransport:1"
})]
pub struct SoapPlay {
#[yaserde(rename = "Play", prefix = "u", default = "default_play")]
pub body: Play,
}
fn default_play() -> Play {
Play::default()
}
#[derive(YaSerialize, YaDeserialize, Debug, Default, Clone, Eq, PartialEq)]
#[yaserde(rename = "Play", prefix = "u")]
pub struct Play {
#[yaserde(flatten = true, default = "default_play2")]
pub parameters: Play2,
}
fn default_play2() -> Play2 {
Play2::default()
}
#[derive(YaSerialize, YaDeserialize, Debug, Default, Clone, Eq, PartialEq)]
#[yaserde(
rename = "Play",
namespaces = {
"u" = "urn:schemas-upnp-org:service:AVTransport:1",
},
prefix = "u"
)]
pub struct Play2 {
#[yaserde(rename = "InstanceID", default = "default_i32")]
pub instance_id: i32,
#[yaserde(rename = "Speed", default = "default_i32")]
pub speed: i32,
}
fn default_i32() -> i32 {
i32::default()
}
#[derive(PrimitiveYaSerde, Debug, Default, Eq, PartialEq)]
struct Meters(i32);
#[test]
fn test_for_generic_newtype() {
let a = SoapEnvelope {
encoding_style: "".to_string(),
tnsattr: None,
urnattr: None,
xsiattr: None,
header: None,
body: Meters(10),
};
let s = ser::to_string(&a).unwrap();
let b: SoapEnvelope<Meters> = de::from_str(&s).unwrap();
assert_eq!(a, b);
println!("{:#?}", b);
}
#[test]
fn test_for_generic_nested_struct() {
let a = SoapEnvelope {
encoding_style: "".to_string(),
tnsattr: None,
urnattr: None,
xsiattr: None,
header: None,
body: SoapPlay {
body: Play {
parameters: Play2 {
instance_id: 20,
speed: 1,
},
},
},
};
let s = ser::to_string(&a).unwrap();
println!("{s}");
let b: SoapEnvelope<SoapPlay> = de::from_str(&s).unwrap();
assert_eq!(a, b);
println!("{:#?}", b);
}