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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
mod author;
mod category;
mod contributor;
mod entry;
mod feed;
mod generator;
mod link;
mod person;
mod source;
extern crate xml;
use xml::Element;
pub use ::author::Author;
pub use ::category::Category;
pub use ::contributor::Contributor;
pub use ::entry::Entry;
pub use ::feed::Feed;
pub use ::generator::Generator;
pub use ::link::Link;
pub use ::person::Person;
pub use ::source::Source;
const NS: &'static str = "http://www.w3.org/2005/Atom";
trait ElementUtils {
fn tag_with_text(&mut self, child_name: &'static str, child_body: &str);
fn tag_with_optional_text(&mut self, child_name: &'static str, child_body: &Option<String>);
fn attribute_with_text(&mut self, attribute_name: &'static str, attribute_value: &str);
fn attribute_with_optional_text(&mut self, attribute_name: &'static str, attribute_value: &Option<String>);
}
impl ElementUtils for Element {
fn tag_with_text(&mut self, child_name: &'static str, child_body: &str) {
self.tag(elem_with_text(child_name, child_body));
}
fn tag_with_optional_text(&mut self, child_name: &'static str, child_body: &Option<String>) {
if let Some(ref c) = *child_body {
self.tag_with_text(child_name, &c);
}
}
fn attribute_with_text(&mut self, attribute_name: &'static str, attribute_value: &str) {
self.set_attribute(attribute_name.to_string(), None, attribute_value.to_string());
}
fn attribute_with_optional_text(&mut self, attribute_name: &'static str, attribute_value: &Option<String>) {
if let Some(ref v) = *attribute_value {
self.attribute_with_text(attribute_name, &v);
}
}
}
fn elem_with_text(tag_name: &'static str, chars: &str) -> Element {
let mut elem = Element::new(tag_name.to_string(), Some(NS.to_string()), vec![]);
elem.text(chars.to_string());
elem
}
trait ViaXml where Self: Sized {
fn to_xml(&self) -> Element;
fn from_xml(elem: Element) -> Result<Self, &'static str>;
}
#[cfg(test)]
mod test {
use std::default::Default;
use std::fs::File;
use std::io::Read;
use std::str::FromStr;
use super::{Person, Entry, Feed, Link};
#[test]
fn test_basic_to_string() {
let author = Person {
name: "N. Blogger".to_string(),
..Default::default()
};
let entry = Entry {
title: "My first post!".to_string(),
content: Some("This is my first post".to_string()),
..Default::default()
};
let feed = Feed {
title: "My Blog".to_string(),
authors: vec![author],
entries: vec![entry],
..Default::default()
};
assert_eq!(feed.to_string(), "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed xmlns=\'http://www.w3.org/2005/Atom\'><id></id><title>My Blog</title><updated></updated><author><name>N. Blogger</name></author><entry><id></id><title>My first post!</title><updated></updated><content>This is my first post</content></entry></feed>");
}
#[test]
fn test_links() {
let feed = Feed {
links: vec![
Link {
href: "http://test.blog/blog.atom".to_string(),
rel: Some("self".to_string()),
..Default::default()
},
],
entries: vec![
Entry {
links: vec![
Link {
href: "http://test.blog/entry".to_string(),
rel: Some("alternate".to_string()),
..Default::default()
}
],
source: Some(Feed {
title: "Original Blog".to_string(),
links: vec![
Link {
href: "http://original.blog/feed.atom".to_string(),
rel: Some("self".to_string()),
..Default::default()
}
],
..Default::default()
}),
..Default::default()
},
],
..Default::default()
};
assert!(feed.to_string().bytes().count() > 0);
}
#[test]
fn test_from_file() {
let mut file = File::open("test-data/xkcd.xml").unwrap();
let mut atom_string = String::new();
file.read_to_string(&mut atom_string).unwrap();
let feed = Feed::from_str(&atom_string).unwrap();
assert!(feed.to_string().len() > 0);
}
#[test]
fn test_read_no_feeds() {
let atom_str = "";
assert!(Feed::from_str(atom_str).is_err());
}
#[test]
fn test_read_one_feed_no_properties() {
let atom_str = "\
<feed>\
</feed>";
assert!(Feed::from_str(atom_str).is_err());
}
#[test]
fn test_read_one_feed() {
let atom_str = r#"
<feed xmlns="http://www.w3.org/2005/Atom">
<id></id>
<title>Hello world!</title>
<updated></updated>
<description></description>
</feed>"#;
println!("{}", atom_str);
let feed = Feed::from_str(atom_str).unwrap();
assert_eq!("Hello world!", feed.title);
}
#[test]
fn test_read_with_pinode() {
let atom_str = r#"
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id></id>
<title>Title</title>
<updated></updated>
<description></description>
</feed>"#;
let feed = Feed::from_str(atom_str).unwrap();
assert_eq!("Title", feed.title);
}
}