ctb_formats_syndication/
rss.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 renderer for the syndication data model.
21//!
22//! This module supports rendering feeds to multiple RSS versions:
23//!
24//! - **RSS 0.9**: Original Netscape RDF-based format. ASCII-only, max 8KB, max
25//!   15 items, lowercase tags.
26//! - **RSS 0.91**: `UserLand`'s simplified DTD-based format. Max 15 items,
27//!   field length limits per the schema.
28//! - **RSS 0.92**: Extended 0.91 with optional elements (cloud, enclosure,
29//!   category, source).
30//! - **RSS 1.0**: W3C RDF-based format with namespaces and `rdf:about`
31//!   attributes.
32//! - **RSS 2.0**: Most common modern format, with Atom namespace for self-link.
33
34use crate::rss::rss_20::render_rss_20;
35#[allow(
36    unused_imports,
37    clippy::wildcard_imports,
38    reason = "Standard workspace module prelude"
39)]
40use crate::utilities::*;
41
42use super::Feed;
43use ctb_formats_uri::{
44    ensure_scheme_in, list_permanent_or_historic_iana_schemes,
45};
46use xml::writer::XmlEvent;
47
48pub mod rss_09;
49pub mod rss_091;
50pub mod rss_092;
51pub mod rss_10;
52pub mod rss_20;
53pub mod scripting_news;
54
55// ============================================================================
56// Constants
57// ============================================================================
58
59/// Maximum file size for RSS 0.9 (8KB).
60const RSS_09_MAX_SIZE: usize = 8 * 1024;
61
62/// Maximum number of items per channel for RSS 0.9 and 0.91.
63const RSS_09_091_MAX_ITEMS: usize = 15;
64
65/// RSS 0.9 namespace.
66const RSS_09_NAMESPACE: &str = "http://channel.netscape.com/rdf/simple/0.9/";
67
68/// RDF namespace.
69const RDF_NAMESPACE: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
70
71/// RSS 1.0 namespace.
72const RSS_10_NAMESPACE: &str = "http://purl.org/rss/1.0/";
73
74/// Atom namespace for RSS 2.0 self-link.
75const ATOM_NAMESPACE: &str = "http://www.w3.org/2005/Atom";
76
77// ============================================================================
78// Public render function (dispatcher)
79// ============================================================================
80
81/// Render a [`Feed`] into RSS 2.0 format (default).
82///
83/// This is the main entry point that matches the signature expected by
84/// `FeedFormat::Rss`.
85pub(crate) fn render(feed: &Feed) -> Result<String> {
86    render_rss_20(feed)
87}
88
89// ============================================================================
90// Helper functions
91// ============================================================================
92
93pub(crate) fn ensure_http_or_ftp_uri(url: &str) -> Result<()> {
94    ensure_scheme_in(url, vec!["http", "ftp"])?;
95    Ok(())
96}
97
98pub(crate) fn ensure_http_https_or_ftp_uri(url: &str) -> Result<()> {
99    ensure_scheme_in(url, vec!["http", "https", "ftp"])?;
100    Ok(())
101}
102
103pub(crate) fn ensure_http_https_ftp_or_mailto_uri(url: &str) -> Result<()> {
104    ensure_scheme_in(url, vec!["http", "https", "ftp", "mailto"])?;
105    Ok(())
106}
107
108pub(crate) fn ensure_permanent_or_historic_iana_uri(url: &str) -> Result<()> {
109    ensure_scheme_in(
110        url,
111        list_permanent_or_historic_iana_schemes()?
112            .iter()
113            .map(std::string::String::as_str)
114            .collect(),
115    )?;
116    Ok(())
117}
118
119/// Write a simple text element like `<tag>text</tag>`.
120fn write_text_element<W: std::io::Write>(
121    writer: &mut xml::writer::EventWriter<W>,
122    tag: &str,
123    text: &str,
124) -> Result<()> {
125    writer
126        .write(XmlEvent::start_element(tag))
127        .context("writing element start")?;
128    writer
129        .write(XmlEvent::characters(text))
130        .context("writing element text")?;
131    writer
132        .write(XmlEvent::end_element())
133        .context("writing element end")?;
134    Ok(())
135}
136
137// ============================================================================
138// Tests
139// ============================================================================
140
141#[cfg(test)]
142#[allow(
143    clippy::panic,
144    clippy::expect_used,
145    clippy::unwrap_used,
146    clippy::unwrap_in_result,
147    clippy::panic_in_result_fn,
148    clippy::indexing_slicing,
149    clippy::arithmetic_side_effects,
150    reason = "Standard repository test boilerplate"
151)]
152mod tests {
153    use super::*;
154
155    #[crate::ctb_test]
156    fn test_render_dispatcher() {
157        // The default render() should produce RSS 2.0.
158        let feed = Feed::empty("Test")
159            .with_home_page_url_opt(Some("http://example.org/".into()));
160        let result = render(&feed).unwrap();
161
162        assert!(result.contains("<rss version=\"2.0\""), "Got: '{result}'");
163    }
164}