This repository has been archived by the owner on Aug 18, 2023. It is now read-only.
forked from tafia/quick-xml
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserde_attrs.rs
79 lines (69 loc) · 1.62 KB
/
serde_attrs.rs
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
#![cfg(feature = "serialize")]
extern crate quick_xml;
extern crate regex;
extern crate serde;
use quick_xml::se::to_string;
use regex::Regex;
use serde::Serialize;
use std::borrow::Cow;
#[derive(Serialize)]
#[serde(rename = "classroom")]
struct Classroom {
pub students: Students,
pub number: String,
pub adviser: Person,
}
#[derive(Serialize)]
struct Students {
#[serde(rename = "person")]
pub persons: Vec<Person>,
}
#[derive(Serialize)]
struct Person {
pub name: String,
pub age: u32,
}
#[derive(Serialize)]
#[serde(rename = "empty")]
struct Empty {}
#[test]
fn test_nested() {
let s1 = Person {
name: "sherlock".to_string(),
age: 20,
};
let s2 = Person {
name: "harry".to_string(),
age: 19,
};
let t = Person {
name: "albus".to_string(),
age: 88,
};
let doc = Classroom {
students: Students {
persons: vec![s1, s2],
},
number: "3-1".to_string(),
adviser: t,
};
let xml = quick_xml::se::to_string(&doc).unwrap();
let str = r#"<classroom number="3-1">
<students>
<person name="sherlock" age="20"/>
<person name="harry" age="19"/>
</students>
<adviser name="albus" age="88"/>
</classroom>"#;
assert_eq!(xml, inline(str));
}
fn inline(str: &str) -> Cow<str> {
let regex = Regex::new(r">\s+<").unwrap();
regex.replace_all(str, "><")
}
#[test]
fn test_empty() {
let e = Empty {};
let xml = to_string(&e).unwrap();
assert_eq!(xml, "<empty/>");
}