-
Notifications
You must be signed in to change notification settings - Fork 22
/
category.rs
184 lines (166 loc) · 4.7 KB
/
category.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
use std::borrow::Cow;
use std::io::{BufRead, Write};
use quick_xml::events::{BytesStart, Event};
use quick_xml::Reader;
use quick_xml::Writer;
use crate::error::{Error, XmlError};
use crate::toxml::ToXml;
use crate::util::{attr_value, decode};
/// Represents a category in an Atom feed
#[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 Category {
/// Identifies the category.
pub term: String,
/// Identifies the categorization scheme via a URI.
pub scheme: Option<String>,
/// A human-readable label for display.
pub label: Option<String>,
}
impl Category {
/// Return the term that identifies this category.
///
/// # Examples
///
/// ```
/// use atom_syndication::Category;
///
/// let mut category = Category::default();
/// category.set_term("technology");
/// assert_eq!(category.term(), "technology");
/// ```
pub fn term(&self) -> &str {
self.term.as_str()
}
/// Set the term that identifies this category.
///
/// # Examples
///
/// ```
/// use atom_syndication::Category;
///
/// let mut category = Category::default();
/// category.set_term("technology");
/// ```
pub fn set_term<V>(&mut self, term: V)
where
V: Into<String>,
{
self.term = term.into();
}
/// Return the categorization scheme URI.
///
/// # Examples
///
/// ```
/// use atom_syndication::Category;
///
/// let mut category = Category::default();
/// category.set_scheme("http://example.com/scheme".to_string());
/// assert_eq!(category.scheme(), Some("http://example.com/scheme"));
/// ```
pub fn scheme(&self) -> Option<&str> {
self.scheme.as_deref()
}
/// Set the categorization scheme URI.
///
/// # Examples
///
/// ```
/// use atom_syndication::Category;
///
/// let mut category = Category::default();
/// category.set_scheme("http://example.com/scheme".to_string());
/// ```
pub fn set_scheme<V>(&mut self, scheme: V)
where
V: Into<Option<String>>,
{
self.scheme = scheme.into();
}
/// Return the label for this category.
///
/// # Examples
///
/// ```
/// use atom_syndication::Category;
///
/// let mut category = Category::default();
/// category.set_scheme("Technology".to_string());
/// assert_eq!(category.scheme(), Some("Technology"));
/// ```
pub fn label(&self) -> Option<&str> {
self.label.as_deref()
}
/// Set the label for this category.
///
/// # Examples
///
/// ```
/// use atom_syndication::Category;
///
/// let mut category = Category::default();
/// category.set_scheme("Technology".to_string());
/// ```
pub fn set_label<V>(&mut self, label: V)
where
V: Into<Option<String>>,
{
self.label = label.into();
}
}
impl Category {
pub(crate) fn from_xml<'s, B: BufRead>(
reader: &mut Reader<B>,
element: &'s BytesStart<'s>,
) -> Result<Self, Error> {
let mut category = Category::default();
for att in element.attributes().with_checks(false).flatten() {
match decode(att.key.as_ref(), reader)? {
Cow::Borrowed("term") => {
category.term = attr_value(&att, reader)?.to_string();
}
Cow::Borrowed("scheme") => {
category.scheme = Some(attr_value(&att, reader)?.to_string());
}
Cow::Borrowed("label") => {
category.label = Some(attr_value(&att, reader)?.to_string());
}
_ => {}
}
}
Ok(category)
}
}
impl ToXml for Category {
fn to_xml<W: Write>(&self, writer: &mut Writer<W>) -> Result<(), XmlError> {
let mut element = BytesStart::new("category");
element.push_attribute(("term", &*self.term));
if let Some(ref scheme) = self.scheme {
element.push_attribute(("scheme", &**scheme));
}
if let Some(ref label) = self.label {
element.push_attribute(("label", &**label));
}
writer
.write_event(Event::Empty(element))
.map_err(XmlError::new)?;
Ok(())
}
}
#[cfg(feature = "builders")]
impl CategoryBuilder {
/// Builds a new `Category`.
pub fn build(&self) -> Category {
self.build_impl().unwrap()
}
}