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
use std::collections::HashMap;
use std::fmt;
use std::io::Write;
use std::str::FromStr;
use quick_xml::Error as XmlError;
use quick_xml::Writer;
use extension::Extension;
use toxml::WriterExt;
pub const NAMESPACE: &str = "http://purl.org/rss/1.0/modules/syndication/";
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub enum UpdatePeriod {
HOURLY,
DAILY,
WEEKLY,
MONTHLY,
YEARLY,
}
impl FromStr for UpdatePeriod {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"hourly" => Ok(UpdatePeriod::HOURLY),
"daily" => Ok(UpdatePeriod::DAILY),
"weekly" => Ok(UpdatePeriod::WEEKLY),
"monthly" => Ok(UpdatePeriod::MONTHLY),
"yearly" => Ok(UpdatePeriod::YEARLY),
_ => Err(())
}
}
}
impl fmt::Display for UpdatePeriod {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
UpdatePeriod::HOURLY => write!(f, "hourly"),
UpdatePeriod::DAILY => write!(f, "daily"),
UpdatePeriod::WEEKLY => write!(f, "weekly"),
UpdatePeriod::MONTHLY => write!(f, "monthly"),
UpdatePeriod::YEARLY => write!(f, "yearly"),
}
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Builder)]
#[builder(setter(into), default)]
pub struct SyndicationExtension {
period: UpdatePeriod,
frequency: u32,
base: String,
}
impl SyndicationExtension {
pub fn base(&self) -> &str {
&self.base
}
pub fn set_base(&mut self, base: &str) {
self.base = base.to_owned();
}
pub fn frequency(&self) -> u32 {
self.frequency
}
pub fn set_frequency(&mut self, frequency: u32) {
self.frequency = frequency;
}
pub fn period(&self) -> &UpdatePeriod {
&self.period
}
pub fn set_period(&mut self, period: UpdatePeriod) {
self.period = period;
}
pub fn to_xml<W: Write>(&self, namespaces: &HashMap<String, String>, writer: &mut Writer<W>) -> Result<(), XmlError> {
for (prefix, namespace) in namespaces {
if NAMESPACE == namespace {
writer.write_text_element(format!("{}:updatePeriod", prefix), &self.period.to_string())?;
writer.write_text_element(format!("{}:updateFrequency", prefix), &format!("{}", self.frequency))?;
writer.write_text_element(format!("{}:updateBase", prefix), &self.base)?;
}
}
Ok(())
}
}
impl Default for SyndicationExtension {
fn default() -> Self {
SyndicationExtension { period: UpdatePeriod::DAILY, frequency: 1, base: String::from("1970-01-01T00:00+00:00") }
}
}
fn with_first_ext_value<'a, F>(map: &'a HashMap<String, Vec<Extension>>, field: &str, f: F)
where F: FnOnce(&'a str) {
if let Some(extensions) = map.get(field) {
if !extensions.is_empty() {
if let Some(v) = extensions[0].value.as_ref() {
f(v);
}
}
}
}
impl SyndicationExtension {
pub fn from_map(map: HashMap<String, Vec<Extension>>) -> Self {
let mut syn = SyndicationExtension::default();
with_first_ext_value(&map, "updatePeriod", |value| syn.period = value.parse().unwrap());
with_first_ext_value(&map, "updateFrequency", |value| syn.frequency = value.parse().unwrap());
with_first_ext_value(&map, "updateBase", |value| syn.base = value.to_owned());
syn
}
}