Skip to main content

feedparser_rs/namespace/
mod.rs

1/// Namespace handlers for extended feed formats
2///
3/// This module provides support for parsing various XML namespaces
4/// commonly found in RSS and Atom feeds:
5///
6/// - **Dublin Core** (`dc:`) - Metadata elements
7/// - **Content** (`content:`) - Full HTML content
8/// - **Media RSS** (`media:`) - Multimedia content
9/// - **GeoRSS** (`georss:`) - Geographic location data
10/// - **Creative Commons** (`cc:`) - License information
11/// - **Slash** (`slash:`) - Comment count
12/// - **WFW** (`wfw:`) - Comment RSS URL
13///
14/// # Usage
15///
16/// These handlers are called by the main parsers when encountering
17/// namespaced elements. They update the feed metadata or entries
18/// with information from the namespace elements.
19///
20/// # Example
21///
22/// ```
23/// use feedparser_rs::namespace::dublin_core;
24/// use feedparser_rs::{FeedMeta, ParserLimits};
25///
26/// let mut feed = FeedMeta::default();
27/// let limits = ParserLimits::default();
28/// dublin_core::handle_feed_element("creator", "John Doe", &mut feed, &limits);
29/// assert_eq!(feed.author.as_deref(), Some("John Doe"));
30/// ```
31/// Creative Commons license information
32pub mod cc;
33/// Content Module for RSS 1.0
34pub mod content;
35/// Dublin Core Metadata Element Set
36pub mod dublin_core;
37/// GeoRSS geographic location data
38pub mod georss;
39/// Media RSS specification
40pub mod media_rss;
41/// Slash and WFW namespace (comment count + comment RSS URL)
42pub mod slash;
43/// Syndication Module for RSS 1.0
44pub mod syndication;
45/// Atom Threading Extensions (RFC 4685)
46pub mod threading;
47
48/// Common namespace URIs used in feeds
49pub mod namespaces {
50    /// Dublin Core Metadata Element Set
51    pub const DUBLIN_CORE: &str = "http://purl.org/dc/elements/1.1/";
52
53    /// Content Module for RSS 1.0
54    pub const CONTENT: &str = "http://purl.org/rss/1.0/modules/content/";
55
56    /// Media RSS
57    pub const MEDIA: &str = "http://search.yahoo.com/mrss/";
58
59    /// Atom 1.0
60    pub const ATOM: &str = "http://www.w3.org/2005/Atom";
61
62    /// RSS 1.0 (RDF)
63    pub const RDF: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
64
65    /// RSS 1.0
66    pub const RSS_10: &str = "http://purl.org/rss/1.0/";
67
68    /// Syndication Module for RSS 1.0
69    pub const SYNDICATION: &str = "http://purl.org/rss/1.0/modules/syndication/";
70
71    /// iTunes Podcast
72    pub const ITUNES: &str = "http://www.itunes.com/dtds/podcast-1.0.dtd";
73
74    /// Podcast 2.0
75    pub const PODCAST: &str = "https://podcastindex.org/namespace/1.0";
76
77    /// `GeoRSS`
78    pub const GEORSS: &str = "http://www.georss.org/georss";
79
80    /// W3C Basic Geo (WGS84) namespace
81    pub const GEO: &str = "http://www.w3.org/2003/01/geo/wgs84_pos#";
82
83    /// Creative Commons (modern)
84    pub const CC: &str = "http://creativecommons.org/ns#";
85
86    /// Creative Commons (legacy Userland)
87    pub const CREATIVE_COMMONS: &str = "http://backend.userland.com/creativeCommonsRssModule";
88
89    /// Atom Threading Extensions (RFC 4685)
90    pub const THREADING: &str = "http://purl.org/syndication/thread/1.0";
91
92    /// Slash namespace
93    pub const SLASH: &str = "http://purl.org/rss/1.0/modules/slash/";
94
95    /// WFW (Well-Formed Web) namespace
96    pub const WFW: &str = "http://wellformedweb.org/CommentAPI/";
97}
98
99/// Get namespace URI for a common prefix
100///
101/// # Arguments
102///
103/// * `prefix` - Namespace prefix (e.g., "dc", "content", "media")
104///
105/// # Returns
106///
107/// Returns the namespace URI if the prefix is recognized
108pub fn get_namespace_uri(prefix: &str) -> Option<&'static str> {
109    match prefix {
110        "dc" => Some(namespaces::DUBLIN_CORE),
111        "content" => Some(namespaces::CONTENT),
112        "media" => Some(namespaces::MEDIA),
113        "atom" => Some(namespaces::ATOM),
114        "rdf" => Some(namespaces::RDF),
115        "syn" | "syndication" => Some(namespaces::SYNDICATION),
116        "itunes" => Some(namespaces::ITUNES),
117        "podcast" => Some(namespaces::PODCAST),
118        "georss" => Some(namespaces::GEORSS),
119        "geo" => Some(namespaces::GEO),
120        "cc" => Some(namespaces::CC),
121        "creativeCommons" => Some(namespaces::CREATIVE_COMMONS),
122        "thr" => Some(namespaces::THREADING),
123        "slash" => Some(namespaces::SLASH),
124        "wfw" => Some(namespaces::WFW),
125        _ => None,
126    }
127}
128
129/// Get common prefix for a namespace URI
130///
131/// # Arguments
132///
133/// * `uri` - Namespace URI
134///
135/// # Returns
136///
137/// Returns the common prefix if the URI is recognized
138pub fn get_namespace_prefix(uri: &str) -> Option<&'static str> {
139    match uri {
140        namespaces::DUBLIN_CORE => Some("dc"),
141        namespaces::CONTENT => Some("content"),
142        namespaces::MEDIA => Some("media"),
143        namespaces::ATOM => Some("atom"),
144        namespaces::RDF => Some("rdf"),
145        namespaces::SYNDICATION => Some("syn"),
146        namespaces::ITUNES => Some("itunes"),
147        namespaces::PODCAST => Some("podcast"),
148        namespaces::GEORSS => Some("georss"),
149        namespaces::GEO => Some("geo"),
150        namespaces::CC => Some("cc"),
151        namespaces::CREATIVE_COMMONS => Some("creativeCommons"),
152        namespaces::THREADING => Some("thr"),
153        namespaces::SLASH => Some("slash"),
154        namespaces::WFW => Some("wfw"),
155        _ => None,
156    }
157}
158
159#[cfg(test)]
160mod tests {
161    use super::*;
162
163    #[test]
164    fn test_get_namespace_uri() {
165        assert_eq!(
166            get_namespace_uri("dc"),
167            Some("http://purl.org/dc/elements/1.1/")
168        );
169        assert_eq!(
170            get_namespace_uri("content"),
171            Some("http://purl.org/rss/1.0/modules/content/")
172        );
173        assert_eq!(
174            get_namespace_uri("media"),
175            Some("http://search.yahoo.com/mrss/")
176        );
177        assert_eq!(get_namespace_uri("unknown"), None);
178    }
179
180    #[test]
181    fn test_get_namespace_prefix() {
182        assert_eq!(
183            get_namespace_prefix("http://purl.org/dc/elements/1.1/"),
184            Some("dc")
185        );
186        assert_eq!(
187            get_namespace_prefix("http://purl.org/rss/1.0/modules/content/"),
188            Some("content")
189        );
190        assert_eq!(
191            get_namespace_prefix("http://search.yahoo.com/mrss/"),
192            Some("media")
193        );
194        assert_eq!(get_namespace_prefix("http://unknown.example.com/"), None);
195    }
196}