feedparser_rs/namespace/
syndication.rs1use crate::types::FeedMeta;
14
15pub const SYNDICATION_NAMESPACE: &str = "http://purl.org/rss/1.0/modules/syndication/";
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20#[expect(
21 clippy::exhaustive_enums,
22 reason = "the RSS syndication module defines exactly these five update periods"
23)]
24pub enum UpdatePeriod {
25 Hourly,
27 Daily,
29 Weekly,
31 Monthly,
33 Yearly,
35}
36
37impl UpdatePeriod {
38 #[must_use]
42 pub fn parse(s: &str) -> Option<Self> {
43 match s.to_lowercase().as_str() {
44 "hourly" => Some(Self::Hourly),
45 "daily" => Some(Self::Daily),
46 "weekly" => Some(Self::Weekly),
47 "monthly" => Some(Self::Monthly),
48 "yearly" => Some(Self::Yearly),
49 _ => None,
50 }
51 }
52
53 #[must_use]
55 pub const fn as_str(&self) -> &'static str {
56 match self {
57 Self::Hourly => "hourly",
58 Self::Daily => "daily",
59 Self::Weekly => "weekly",
60 Self::Monthly => "monthly",
61 Self::Yearly => "yearly",
62 }
63 }
64}
65
66#[derive(Debug, Clone, Default)]
68#[non_exhaustive]
69pub struct SyndicationMeta {
70 pub update_period: Option<UpdatePeriod>,
72 pub update_frequency: Option<String>,
74 pub update_base: Option<String>,
76}
77
78pub fn handle_feed_element(element: &str, text: &str, feed: &mut FeedMeta) {
86 match element {
87 "updatePeriod" => {
88 if let Some(period) = UpdatePeriod::parse(text) {
89 if feed.syndication.is_none() {
90 feed.syndication = Some(Box::new(SyndicationMeta::default()));
91 }
92 if let Some(syn) = &mut feed.syndication {
93 syn.update_period = Some(period);
94 }
95 }
96 }
97 "updateFrequency" if !text.is_empty() => {
98 if feed.syndication.is_none() {
99 feed.syndication = Some(Box::new(SyndicationMeta::default()));
100 }
101 if let Some(syn) = &mut feed.syndication {
102 syn.update_frequency = Some(text.to_string());
103 }
104 }
105 "updateBase" => {
106 if feed.syndication.is_none() {
107 feed.syndication = Some(Box::new(SyndicationMeta::default()));
108 }
109 if let Some(syn) = &mut feed.syndication {
110 syn.update_base = Some(text.to_string());
111 }
112 }
113 _ => {
114 }
116 }
117}
118
119#[cfg(test)]
120mod tests {
121 use super::*;
122
123 #[test]
124 fn test_update_period_parse() {
125 assert_eq!(UpdatePeriod::parse("hourly"), Some(UpdatePeriod::Hourly));
126 assert_eq!(UpdatePeriod::parse("daily"), Some(UpdatePeriod::Daily));
127 assert_eq!(UpdatePeriod::parse("weekly"), Some(UpdatePeriod::Weekly));
128 assert_eq!(UpdatePeriod::parse("monthly"), Some(UpdatePeriod::Monthly));
129 assert_eq!(UpdatePeriod::parse("yearly"), Some(UpdatePeriod::Yearly));
130 assert_eq!(UpdatePeriod::parse("invalid"), None);
131 }
132
133 #[test]
134 fn test_update_period_case_insensitive() {
135 assert_eq!(UpdatePeriod::parse("HOURLY"), Some(UpdatePeriod::Hourly));
136 assert_eq!(UpdatePeriod::parse("Daily"), Some(UpdatePeriod::Daily));
137 assert_eq!(UpdatePeriod::parse("WeeKLY"), Some(UpdatePeriod::Weekly));
138 }
139
140 #[test]
141 fn test_update_period_as_str() {
142 assert_eq!(UpdatePeriod::Hourly.as_str(), "hourly");
143 assert_eq!(UpdatePeriod::Daily.as_str(), "daily");
144 assert_eq!(UpdatePeriod::Weekly.as_str(), "weekly");
145 assert_eq!(UpdatePeriod::Monthly.as_str(), "monthly");
146 assert_eq!(UpdatePeriod::Yearly.as_str(), "yearly");
147 }
148
149 #[test]
150 fn test_handle_update_period() {
151 let mut feed = FeedMeta::default();
152
153 handle_feed_element("updatePeriod", "daily", &mut feed);
154
155 assert!(feed.syndication.is_some());
156 let syn = feed.syndication.as_ref().unwrap();
157 assert_eq!(syn.update_period, Some(UpdatePeriod::Daily));
158 }
159
160 #[test]
161 fn test_handle_update_frequency() {
162 let mut feed = FeedMeta::default();
163
164 handle_feed_element("updateFrequency", "2", &mut feed);
165
166 assert!(feed.syndication.is_some());
167 let syn = feed.syndication.as_ref().unwrap();
168 assert_eq!(syn.update_frequency, Some("2".to_string()));
169 }
170
171 #[test]
172 fn test_handle_update_base() {
173 let mut feed = FeedMeta::default();
174
175 handle_feed_element("updateBase", "2024-12-18T00:00:00Z", &mut feed);
176
177 assert!(feed.syndication.is_some());
178 let syn = feed.syndication.as_ref().unwrap();
179 assert_eq!(syn.update_base.as_deref(), Some("2024-12-18T00:00:00Z"));
180 }
181
182 #[test]
183 fn test_handle_multiple_elements() {
184 let mut feed = FeedMeta::default();
185
186 handle_feed_element("updatePeriod", "hourly", &mut feed);
187 handle_feed_element("updateFrequency", "1", &mut feed);
188 handle_feed_element("updateBase", "2024-01-01T00:00:00Z", &mut feed);
189
190 let syn = feed.syndication.as_ref().unwrap();
191 assert_eq!(syn.update_period, Some(UpdatePeriod::Hourly));
192 assert_eq!(syn.update_frequency, Some("1".to_string()));
193 assert_eq!(syn.update_base.as_deref(), Some("2024-01-01T00:00:00Z"));
194 }
195
196 #[test]
197 fn test_handle_invalid_frequency() {
198 let mut feed = FeedMeta::default();
199
200 handle_feed_element("updateFrequency", "not-a-number", &mut feed);
201
202 let syn = feed.syndication.as_ref().unwrap();
204 assert_eq!(syn.update_frequency, Some("not-a-number".to_string()));
205 }
206
207 #[test]
208 fn test_handle_unknown_element() {
209 let mut feed = FeedMeta::default();
210
211 handle_feed_element("unknown", "value", &mut feed);
212
213 assert!(feed.syndication.is_none());
214 }
215}