ctb_formats_syndication/rss/
rss_092.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! RSS 0.92 syndication feed serializer and validator.
21
22#[allow(
23    unused_imports,
24    clippy::wildcard_imports,
25    reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29use super::Feed;
30use crate::rss::{ensure_http_or_ftp_uri, write_text_element};
31use xml::writer::{EmitterConfig, XmlEvent};
32
33// ============================================================================
34// RSS 0.92
35// ============================================================================
36
37/// Render a [`Feed`] into RSS 0.92 format.
38///
39/// RSS 0.92 is similar to 0.91 but allows more optional elements. No explicit
40/// item limit is defined in the spec.
41///
42/// Possibly TODO: may require an `<image>` element for the channel. However,
43/// the sample file doesn't include one, so it may be intended to be newly
44/// optional in 0.92 and just not documented as such.
45///
46/// Specification: <https://www.rssboard.org/rss-0-9-2>
47pub fn render_rss_092(feed: &Feed) -> Result<String> {
48    let mut output = Vec::new();
49
50    let config = EmitterConfig::new()
51        .perform_indent(true)
52        .indent_string("\t");
53    let mut writer = config.create_writer(&mut output);
54
55    writer
56        .write(XmlEvent::StartDocument {
57            version: xml::common::XmlVersion::Version10,
58            encoding: Some("utf-8"),
59            standalone: None,
60        })
61        .context("writing XML declaration")?;
62
63    writer
64        .write(XmlEvent::start_element("rss").attr("version", "0.92"))
65        .context("writing rss start element")?;
66
67    writer
68        .write(XmlEvent::start_element("channel"))
69        .context("writing channel start")?;
70
71    write_text_element(&mut writer, "title", feed.title())?;
72
73    if let Some(home_page_url) = feed.home_page_url() {
74        ensure_http_or_ftp_uri(home_page_url)?;
75
76        write_text_element(&mut writer, "link", home_page_url)?;
77    }
78
79    write_text_element(&mut writer, "description", feed.title())?;
80
81    // Items (no explicit limit in 0.92).
82    for entry in feed.entries() {
83        writer
84            .write(XmlEvent::start_element("item"))
85            .context("writing item start")?;
86
87        if !entry.title().is_empty() {
88            write_text_element(&mut writer, "title", entry.title())?;
89        }
90
91        if let Some(url) = entry.url() {
92            ensure_http_or_ftp_uri(url)?;
93
94            write_text_element(&mut writer, "link", url)?;
95        }
96
97        if !entry.body().is_empty() {
98            write_text_element(&mut writer, "description", entry.body())?;
99        }
100
101        writer
102            .write(XmlEvent::end_element())
103            .context("closing item element")?;
104    }
105
106    writer
107        .write(XmlEvent::end_element())
108        .context("closing channel element")?;
109
110    writer
111        .write(XmlEvent::end_element())
112        .context("closing rss element")?;
113
114    String::from_utf8(output).context("RSS 0.92 output is not valid UTF-8")
115}