-
Notifications
You must be signed in to change notification settings - Fork 22
/
mod.rs
214 lines (196 loc) · 5.92 KB
/
mod.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
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
use std::collections::BTreeMap;
use std::io::Write;
use std::str;
use quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
use quick_xml::Writer;
use crate::error::XmlError;
use crate::toxml::ToXml;
pub(crate) mod util;
/// A map of extension namespace prefixes to local names to elements.
pub type ExtensionMap = BTreeMap<String, BTreeMap<String, Vec<Extension>>>;
/// A namespaced extension.
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Debug, Default, Clone, PartialEq)]
#[cfg_attr(feature = "builders", derive(Builder))]
#[cfg_attr(
feature = "builders",
builder(
setter(into),
default,
build_fn(name = "build_impl", private, error = "never::Never")
)
)]
pub struct Extension {
/// The qualified name of the extension element.
pub name: String,
/// The content of the extension element.
pub value: Option<String>,
/// The attributes for the extension element.
#[cfg_attr(feature = "builders", builder(setter(each = "attr")))]
pub attrs: BTreeMap<String, String>,
/// The children of the extension element. A map of local names to child elements.
#[cfg_attr(feature = "builders", builder(setter(each = "child")))]
pub children: BTreeMap<String, Vec<Extension>>,
}
impl Extension {
/// Return the qualified name of this extension.
///
/// # Examples
///
/// ```
/// use atom_syndication::extension::Extension;
///
/// let mut extension = Extension::default();
/// extension.set_name("ext:name");
/// assert_eq!(extension.name(), "ext:name");
/// ```
pub fn name(&self) -> &str {
self.name.as_str()
}
/// Set the qualified name of this extension.
///
/// # Examples
///
/// ```
/// use atom_syndication::extension::Extension;
///
/// let mut extension = Extension::default();
/// extension.set_name("ext:name");
/// ```
pub fn set_name<V>(&mut self, name: V)
where
V: Into<String>,
{
self.name = name.into();
}
/// Return the text content of this extension.
///
/// # Examples
///
/// ```
/// use atom_syndication::extension::Extension;
///
/// let mut extension = Extension::default();
/// extension.set_value("John Doe".to_string());
/// assert_eq!(extension.value(), Some("John Doe"));
/// ```
pub fn value(&self) -> Option<&str> {
self.value.as_deref()
}
/// Set the text content of this extension.
///
/// # Examples
///
/// ```
/// use atom_syndication::extension::Extension;
///
/// let mut extension = Extension::default();
/// extension.set_value("John Doe".to_string());
/// ```
pub fn set_value<V>(&mut self, value: V)
where
V: Into<Option<String>>,
{
self.value = value.into();
}
/// Return the attributes for the extension element.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeMap;
/// use atom_syndication::extension::Extension;
///
/// let mut extension = Extension::default();
/// let mut attrs = BTreeMap::<String, String>::new();
/// attrs.insert("email".to_string(), "johndoe@example.com".to_string());
/// extension.set_attrs(attrs.clone());
/// assert_eq!(*extension.attrs(), attrs);
/// ```
pub fn attrs(&self) -> &BTreeMap<String, String> {
&self.attrs
}
/// Set the attributes for the extension element.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeMap;
/// use atom_syndication::extension::Extension;
///
/// let mut extension = Extension::default();
/// extension.set_attrs(BTreeMap::new());
/// ```
pub fn set_attrs<V>(&mut self, attrs: V)
where
V: Into<BTreeMap<String, String>>,
{
self.attrs = attrs.into();
}
/// Return the children of the extension element.
///
/// A map of local names to child elements.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeMap;
/// use atom_syndication::extension::Extension;
///
/// let mut extension = Extension::default();
/// let mut children = BTreeMap::<String, Vec<Extension>>::new();
/// children.insert("ext:child".to_string(), Vec::new());
/// extension.set_children(children);
/// assert!(extension.children().contains_key("ext:child"));
/// ```
pub fn children(&self) -> &BTreeMap<String, Vec<Extension>> {
&self.children
}
/// Set the children of the extension element.
///
/// A map of local names to child elements.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeMap;
/// use atom_syndication::extension::Extension;
///
/// let mut extension = Extension::default();
/// extension.set_children(BTreeMap::new());
/// ```
pub fn set_children<V>(&mut self, children: V)
where
V: Into<BTreeMap<String, Vec<Extension>>>,
{
self.children = children.into();
}
}
impl ToXml for Extension {
fn to_xml<W: Write>(&self, writer: &mut Writer<W>) -> Result<(), XmlError> {
let mut element = BytesStart::new(&self.name);
element.extend_attributes(self.attrs.iter().map(|a| (a.0.as_bytes(), a.1.as_bytes())));
writer
.write_event(Event::Start(element))
.map_err(XmlError::new)?;
if let Some(value) = self.value.as_ref() {
writer
.write_event(Event::Text(BytesText::new(value)))
.map_err(XmlError::new)?;
}
for extension in self.children.values().flatten() {
extension.to_xml(writer)?;
}
writer
.write_event(Event::End(BytesEnd::new(&self.name)))
.map_err(XmlError::new)?;
Ok(())
}
}
#[cfg(feature = "builders")]
impl ExtensionBuilder {
/// Builds a new `Extension`.
pub fn build(&self) -> Extension {
self.build_impl().unwrap()
}
}