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