1#[allow(
32 unused_imports,
33 clippy::wildcard_imports,
34 reason = "Standard workspace crate prelude"
35)]
36pub(crate) use ctb_utilities::*;
37
38use dateparser::parse;
39use jiff::Timestamp;
40
41fn parse_timestamp(input: &str) -> Result<Timestamp> {
42 let dt = parse(input)?;
43 let nanos = i32::try_from(dt.timestamp_subsec_nanos())
44 .context("Parsed timestamp nanoseconds out of range")?;
45 Timestamp::new(dt.timestamp(), nanos)
46 .context("Parsed timestamp out of range")
47}
48
49pub mod atom;
50pub mod hatom;
51pub mod json_feed;
52pub mod rss;
53
54#[derive(Clone, Copy, Debug, Eq, PartialEq)]
56pub enum FeedFormat {
57 JsonFeed,
59 Atom,
61 HAtom,
62 Rss,
64 Rss1,
65 Rss092,
66 Rss091Netscape,
67 Rss091UserLand,
68 Rss09,
69 ScriptingNews10,
70 ScriptingNews20,
71}
72
73#[derive(Clone, Debug, Eq, PartialEq)]
75pub struct Entry {
76 id: String,
77 title: String,
78 body_html: String,
79 author: Option<String>,
80 date_published: String,
81 date_updated: Option<String>,
82 url: Option<String>,
83}
84
85#[derive(Clone, Debug, Default, Eq, PartialEq)]
91pub struct Image {
92 url: String,
93 alt: Option<String>,
94 width: Option<u32>,
95 height: Option<u32>,
96 description: Option<String>,
97}
98
99impl Image {
100 #[must_use]
102 pub fn new() -> Self {
103 Self::default()
104 }
105
106 #[must_use]
108 pub fn with_url(mut self, url: String) -> Self {
109 self.url = url;
110 self
111 }
112
113 #[must_use]
115 pub fn with_alt_opt(mut self, alt: Option<String>) -> Self {
116 self.alt = alt;
117 self
118 }
119
120 #[must_use]
122 pub fn with_width_opt(mut self, width: Option<u32>) -> Self {
123 self.width = width;
124 self
125 }
126
127 #[must_use]
129 pub fn with_height_opt(mut self, height: Option<u32>) -> Self {
130 self.height = height;
131 self
132 }
133
134 #[must_use]
136 pub fn with_description_opt(mut self, description: Option<String>) -> Self {
137 self.description = description;
138 self
139 }
140
141 pub fn url(&self) -> &str {
143 &self.url
144 }
145
146 pub fn alt(&self) -> Option<&str> {
148 self.alt.as_deref()
149 }
150
151 pub fn width(&self) -> Option<u32> {
153 self.width
154 }
155
156 pub fn height(&self) -> Option<u32> {
158 self.height
159 }
160
161 pub fn description(&self) -> Option<&str> {
163 self.description.as_deref()
164 }
165}
166
167impl Entry {
168 pub fn new(
173 id: impl Into<String>,
174 title: impl Into<String>,
175 body_html: impl Into<String>,
176 date_published: impl Into<String>,
177 ) -> Self {
178 Self {
179 id: id.into(),
180 title: title.into(),
181 body_html: body_html.into(),
182 author: None,
183 date_published: date_published.into(),
184 date_updated: None,
185 url: None,
186 }
187 }
188
189 #[must_use]
191 pub fn with_author(mut self, author: impl Into<String>) -> Self {
192 self.author = Some(author.into());
193 self
194 }
195
196 #[must_use]
198 pub fn with_author_opt(mut self, author: Option<String>) -> Self {
199 self.author = author;
200 self
201 }
202
203 #[must_use]
205 pub fn with_url_opt(mut self, url: Option<String>) -> Self {
206 self.url = url;
207 self
208 }
209
210 #[must_use]
212 pub fn with_updated_opt(mut self, updated: Option<String>) -> Self {
213 self.date_updated = updated;
214 self
215 }
216
217 pub fn id(&self) -> &str {
219 &self.id
220 }
221
222 pub fn title(&self) -> &str {
224 &self.title
225 }
226
227 pub fn body(&self) -> &str {
229 &self.body_html
230 }
231
232 pub fn author(&self) -> Option<&str> {
234 self.author.as_deref()
235 }
236
237 pub fn date(&self) -> &str {
239 &self.date_published
240 }
241
242 pub fn date_parsed(&self) -> Result<Timestamp> {
243 parse_timestamp(&self.date_published)
248 }
249
250 pub fn date_try_format(&self, fmt: &str) -> String {
251 if let Ok(dt) = self.date_parsed()
252 && let Ok(formatted) = jiff::fmt::strtime::format(fmt, dt)
253 {
254 formatted
255 } else {
256 self.date_published.clone()
257 }
258 }
259
260 pub fn updated(&self) -> Option<&str> {
262 self.date_updated.as_deref()
263 }
264
265 pub fn updated_parsed(&self) -> Result<Timestamp> {
266 parse_timestamp(bail_if_none!(self.date_updated.as_ref()))
267 }
268
269 pub fn updated_try_format(&self, fmt: &str) -> Option<String> {
270 if let Ok(dt) = self.updated_parsed()
271 && let Ok(formatted) = jiff::fmt::strtime::format(fmt, dt)
272 {
273 Some(formatted)
274 } else {
275 Some(self.date().to_string())
276 }
277 }
278
279 pub fn try_updated(&self) -> String {
280 match &self.updated() {
281 Some(dt) => (*dt).to_string(),
282 None => self.date().to_string(),
283 }
284 }
285
286 pub fn try_updated_parsed(&self) -> Result<Timestamp> {
287 parse_timestamp(&self.try_updated())
288 }
289
290 pub fn try_updated_try_format(&self, fmt: &str) -> String {
291 if let Ok(dt) = self.try_updated_parsed()
292 && let Ok(formatted) = jiff::fmt::strtime::format(fmt, dt)
293 {
294 formatted
295 } else {
296 self.try_updated()
297 }
298 }
299
300 pub fn url(&self) -> Option<&str> {
302 self.url.as_deref()
303 }
304}
305
306#[derive(Clone, Debug, Default, Eq, PartialEq)]
308pub struct Feed {
309 title: String,
310 home_page_url: Option<String>,
311 #[expect(
312 clippy::struct_field_names,
313 reason = "feed_url is standard RSS/Atom nomenclature"
314 )]
315 feed_url: Option<String>,
316 image: Option<Image>,
317 entries: Vec<Entry>,
318}
319
320impl Feed {
321 pub fn new(
323 title: impl Into<String>,
324 entries: impl IntoIterator<Item = Entry>,
325 ) -> Self {
326 Self {
327 title: title.into(),
328 home_page_url: None,
329 feed_url: None,
330 image: None,
331 entries: entries.into_iter().collect(),
332 }
333 }
334
335 pub fn empty(title: impl Into<String>) -> Self {
337 Self {
338 title: title.into(),
339 home_page_url: None,
340 feed_url: None,
341 image: None,
342 entries: Vec::new(),
343 }
344 }
345
346 #[must_use]
348 pub fn with_home_page_url_opt(
349 mut self,
350 home_page_url: Option<String>,
351 ) -> Self {
352 self.home_page_url = home_page_url;
353 self
354 }
355
356 #[must_use]
358 pub fn with_feed_url_opt(mut self, feed_url: Option<String>) -> Self {
359 self.feed_url = feed_url;
360 self
361 }
362
363 #[must_use]
365 pub fn with_image(mut self, image: Image) -> Self {
366 self.image = Some(image);
367 self
368 }
369
370 pub fn title(&self) -> &str {
372 &self.title
373 }
374
375 pub fn home_page_url(&self) -> Option<&str> {
377 self.home_page_url.as_deref()
378 }
379
380 pub fn feed_url(&self) -> Option<&str> {
382 self.feed_url.as_deref()
383 }
384
385 pub fn image(&self) -> Option<&Image> {
387 self.image.as_ref()
388 }
389
390 pub fn add_entry(&mut self, entry: Entry) {
392 self.entries.push(entry);
393 }
394
395 #[must_use]
397 pub fn with_entry(mut self, entry: Entry) -> Self {
398 self.entries.push(entry);
399 self
400 }
401
402 pub fn entries(&self) -> &[Entry] {
404 &self.entries
405 }
406
407 pub fn into_entries(self) -> Vec<Entry> {
409 self.entries
410 }
411
412 pub fn to(&self, format: FeedFormat) -> Result<String> {
414 match format {
415 FeedFormat::JsonFeed => json_feed::render(self),
416 FeedFormat::Atom => atom::render(self),
417 FeedFormat::Rss => rss::render(self),
418 FeedFormat::HAtom => hatom::render(self),
419 FeedFormat::Rss1 => rss::rss_10::render_rss_10(self),
420 FeedFormat::Rss092 => rss::rss_092::render_rss_092(self),
421 FeedFormat::Rss091Netscape => {
422 rss::rss_091::render_rss_091_netscape(self)
423 }
424 FeedFormat::Rss091UserLand => {
425 rss::rss_091::render_rss_091_userland(self)
426 }
427 FeedFormat::Rss09 => rss::rss_09::render_rss_09(self),
428 FeedFormat::ScriptingNews10 => {
429 rss::scripting_news::render_scripting_news_10(self)
430 }
431 FeedFormat::ScriptingNews20 => {
432 rss::scripting_news::render_scripting_news_20(self)
433 }
434 }
435 }
436
437 pub fn to_json_feed(&self) -> Result<String> {
439 self.to(FeedFormat::JsonFeed)
440 }
441
442 pub fn to_atom(&self) -> Result<String> {
444 self.to(FeedFormat::Atom)
445 }
446
447 pub fn to_rss(&self) -> Result<String> {
449 self.to(FeedFormat::Rss)
450 }
451
452 pub fn to_hatom(&self) -> Result<String> {
454 self.to(FeedFormat::HAtom)
455 }
456}