1 #[macro_use]
2 extern crate yaserde;
3 #[macro_use]
4 extern crate yaserde_derive;
5 
6 use std::io::{Read, Write};
7 use yaserde::{YaDeserialize, YaSerialize};
8 
9 #[test]
basic_enum()10 fn basic_enum() {
11   #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)]
12   #[yaserde(rename = "base")]
13   pub struct XmlStruct {
14     color: Color,
15   }
16 
17   #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)]
18   #[yaserde(rename = "color")]
19   pub enum Color {
20     White,
21     Black,
22     #[yaserde(rename = "custom")]
23     Custom {
24       enabled: String,
25       u8_value: u8,
26       i8_value: i8,
27       u16_value: u16,
28       i16_value: i16,
29       u32_value: u32,
30       i32_value: i32,
31       u64_value: u64,
32       i64_value: i64,
33       f32_value: f32,
34       f64_value: f64,
35       color: RGBColor,
36       alpha: Alpha,
37       alphas: Vec<Alpha>,
38     },
39   }
40 
41   impl Default for Color {
42     fn default() -> Color {
43       Color::White
44     }
45   }
46 
47   assert_eq!(Color::default(), Color::White);
48 
49   #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)]
50   pub struct RGBColor {
51     red: String,
52     green: String,
53     blue: String,
54   }
55 
56   #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)]
57   pub enum Alpha {
58     Transparent,
59     Opaque,
60   }
61 
62   impl Default for Alpha {
63     fn default() -> Alpha {
64       Alpha::Transparent
65     }
66   }
67 
68   let model = XmlStruct {
69     color: Color::Black,
70   };
71 
72   let content = "<base><color>Black</color></base>";
73   serialize_and_validate!(model, content);
74   deserialize_and_validate!(content, model, XmlStruct);
75 
76   let model = XmlStruct {
77     color: Color::Custom {
78       enabled: "true".to_string(),
79       u8_value: 8,
80       i8_value: -8,
81       u16_value: 16,
82       i16_value: -16,
83       u32_value: 32,
84       i32_value: -32,
85       u64_value: 64,
86       i64_value: -64,
87       f32_value: 32.0,
88       f64_value: 64.0,
89       color: RGBColor {
90         red: "0".to_string(),
91         green: "128".to_string(),
92         blue: "255".to_string(),
93       },
94       alpha: Alpha::Opaque,
95       alphas: vec![Alpha::Opaque, Alpha::Transparent],
96     },
97   };
98 
99   let content = r#"
100 <base>
101 <color><enabled>true</enabled>
102 <u8_value>8</u8_value>
103 <i8_value>-8</i8_value>
104 <u16_value>16</u16_value>
105 <i16_value>-16</i16_value>
106 <u32_value>32</u32_value>
107 <i32_value>-32</i32_value>
108 <u64_value>64</u64_value>
109 <i64_value>-64</i64_value>
110 <f32_value>32</f32_value>
111 <f64_value>64</f64_value>
112 <color><red>0</red><green>128</green><blue>255</blue></color>
113 <alpha>Opaque</alpha>
114 <alphas>Opaque</alphas>
115 <alphas>Transparent</alphas>
116 </color>
117 </base>"#;
118 
119   serialize_and_validate!(model, content);
120   // TODO
121   // deserialize_and_validate!(content, model, XmlStruct);
122 }
123 
124 #[test]
attribute_enum()125 fn attribute_enum() {
126   #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)]
127   #[yaserde(rename = "base")]
128   pub struct XmlStruct {
129     #[yaserde(attribute)]
130     color: Color,
131   }
132 
133   #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)]
134   #[yaserde(rename = "color")]
135   pub enum Color {
136     #[yaserde(rename = "pink")]
137     Pink,
138   }
139 
140   impl Default for Color {
141     fn default() -> Color {
142       Color::Pink
143     }
144   }
145 
146   let model = XmlStruct { color: Color::Pink };
147 
148   let content = r#"<base color="pink" />"#;
149   serialize_and_validate!(model, content);
150   deserialize_and_validate!(content, model, XmlStruct);
151 }
152 
153 #[test]
unnamed_enum()154 fn unnamed_enum() {
155   #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)]
156   #[yaserde(rename = "base")]
157   pub struct XmlStruct {
158     color: Enum,
159   }
160 
161   #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)]
162   pub struct OtherStruct {
163     fi: i32,
164     se: i32,
165   }
166 
167   #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)]
168   pub enum Enum {
169     Simple,
170     Field(String),
171     FullPath(std::string::String),
172     Integer(i32),
173     UserStruct(OtherStruct),
174     OptionString(Option<String>),
175     OptionUserStruct(Option<OtherStruct>),
176     Strings(Vec<String>),
177     Ints(Vec<i32>),
178     Structs(Vec<OtherStruct>),
179     #[yaserde(rename = "renamed")]
180     ToRename(u32),
181     #[yaserde(rename = "renamed.with.dots")]
182     ToRenameDots(u32),
183   }
184 
185   impl Default for Enum {
186     fn default() -> Enum {
187       Enum::Simple
188     }
189   }
190 
191   let model = XmlStruct {
192     color: Enum::Field(String::from("some_text")),
193   };
194 
195   let content = "<base><color><Field>some_text</Field></color></base>";
196   serialize_and_validate!(model, content);
197   deserialize_and_validate!(content, model, XmlStruct);
198 
199   let model = XmlStruct {
200     color: Enum::FullPath(String::from("some_text")),
201   };
202 
203   let content = "<base><color><FullPath>some_text</FullPath></color></base>";
204   serialize_and_validate!(model, content);
205   deserialize_and_validate!(content, model, XmlStruct);
206 
207   let model = XmlStruct {
208     color: Enum::Integer(56),
209   };
210 
211   let content = "<base><color><Integer>56</Integer></color></base>";
212   serialize_and_validate!(model, content);
213   deserialize_and_validate!(content, model, XmlStruct);
214 
215   let model = XmlStruct {
216     color: Enum::UserStruct(OtherStruct { fi: 24, se: 42 }),
217   };
218 
219   let content = "<base><color><UserStruct><fi>24</fi><se>42</se></UserStruct></color></base>";
220   serialize_and_validate!(model, content);
221   deserialize_and_validate!(content, model, XmlStruct);
222 
223   let model = XmlStruct {
224     color: Enum::OptionString(Some(String::from("some_text"))),
225   };
226 
227   let content = "<base><color><OptionString>some_text</OptionString></color></base>";
228   serialize_and_validate!(model, content);
229   deserialize_and_validate!(content, model, XmlStruct);
230 
231   let model = XmlStruct {
232     color: Enum::OptionString(None),
233   };
234 
235   let content = "<base><color /></base>";
236   serialize_and_validate!(model, content);
237   // TODO
238   // deserialize_and_validate!(content, model, XmlStruct);
239 
240   let model = XmlStruct {
241     color: Enum::OptionUserStruct(Some(OtherStruct { fi: 12, se: 23 })),
242   };
243 
244   let content =
245     "<base><color><OptionUserStruct><fi>12</fi><se>23</se></OptionUserStruct></color></base>";
246   serialize_and_validate!(model, content);
247   deserialize_and_validate!(content, model, XmlStruct);
248 
249   let model = XmlStruct {
250     color: Enum::OptionUserStruct(None),
251   };
252 
253   let content = "<base><color /></base>";
254   serialize_and_validate!(model, content);
255   // TODO
256   // deserialize_and_validate!(content, model, XmlStruct);
257 
258   let model = XmlStruct {
259     color: Enum::Strings(vec![String::from("abc"), String::from("def")]),
260   };
261 
262   let content = "<base><color><Strings>abc</Strings><Strings>def</Strings></color></base>";
263   serialize_and_validate!(model, content);
264   deserialize_and_validate!(content, model, XmlStruct);
265 
266   let model = XmlStruct {
267     color: Enum::Ints(vec![23, 45]),
268   };
269 
270   let content = "<base><color><Ints>23</Ints><Ints>45</Ints></color></base>";
271   serialize_and_validate!(model, content);
272   deserialize_and_validate!(content, model, XmlStruct);
273 
274   let model = XmlStruct {
275     color: Enum::Structs(vec![
276       OtherStruct { fi: 12, se: 23 },
277       OtherStruct { fi: 34, se: 45 },
278     ]),
279   };
280 
281   let content = "<base><color><Structs><fi>12</fi><se>23</se></Structs><Structs><fi>34</fi><se>45</se></Structs></color></base>";
282   serialize_and_validate!(model, content);
283   deserialize_and_validate!(content, model, XmlStruct);
284 
285   let model = XmlStruct {
286     color: Enum::ToRename(87),
287   };
288 
289   let content = "<base><color><renamed>87</renamed></color></base>";
290   serialize_and_validate!(model, content);
291   deserialize_and_validate!(content, model, XmlStruct);
292 
293   let model = XmlStruct {
294     color: Enum::ToRenameDots(84),
295   };
296 
297   let content = "<base><color><renamed.with.dots>84</renamed.with.dots></color></base>";
298   serialize_and_validate!(model, content);
299   deserialize_and_validate!(content, model, XmlStruct);
300 }
301