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    /// Creative Commons (modern)
80    pub const CC: &str = "http://creativecommons.org/ns#";
81
82    /// Creative Commons (legacy Userland)
83    pub const CREATIVE_COMMONS: &str = "http://backend.userland.com/creativeCommonsRssModule";
84
85    /// Atom Threading Extensions (RFC 4685)
86    pub const THREADING: &str = "http://purl.org/syndication/thread/1.0";
87
88    /// Slash namespace
89    pub const SLASH: &str = "http://purl.org/rss/1.0/modules/slash/";
90
91    /// WFW (Well-Formed Web) namespace
92    pub const WFW: &str = "http://wellformedweb.org/CommentAPI/";
93}
94
95/// Get namespace URI for a common prefix
96///
97/// # Arguments
98///
99/// * `prefix` - Namespace prefix (e.g., "dc", "content", "media")
100///
101/// # Returns
102///
103/// Returns the namespace URI if the prefix is recognized
104pub fn get_namespace_uri(prefix: &str) -> Option<&'static str> {
105    match prefix {
106        "dc" => Some(namespaces::DUBLIN_CORE),
107        "content" => Some(namespaces::CONTENT),
108        "media" => Some(namespaces::MEDIA),
109        "atom" => Some(namespaces::ATOM),
110        "rdf" => Some(namespaces::RDF),
111        "syn" | "syndication" => Some(namespaces::SYNDICATION),
112        "itunes" => Some(namespaces::ITUNES),
113        "podcast" => Some(namespaces::PODCAST),
114        "georss" => Some(namespaces::GEORSS),
115        "cc" => Some(namespaces::CC),
116        "creativeCommons" => Some(namespaces::CREATIVE_COMMONS),
117        "thr" => Some(namespaces::THREADING),
118        "slash" => Some(namespaces::SLASH),
119        "wfw" => Some(namespaces::WFW),
120        _ => None,
121    }
122}
123
124/// Get common prefix for a namespace URI
125///
126/// # Arguments
127///
128/// * `uri` - Namespace URI
129///
130/// # Returns
131///
132/// Returns the common prefix if the URI is recognized
133pub fn get_namespace_prefix(uri: &str) -> Option<&'static str> {
134    match uri {
135        namespaces::DUBLIN_CORE => Some("dc"),
136        namespaces::CONTENT => Some("content"),
137        namespaces::MEDIA => Some("media"),
138        namespaces::ATOM => Some("atom"),
139        namespaces::RDF => Some("rdf"),
140        namespaces::SYNDICATION => Some("syn"),
141        namespaces::ITUNES => Some("itunes"),
142        namespaces::PODCAST => Some("podcast"),
143        namespaces::GEORSS => Some("georss"),
144        namespaces::CC => Some("cc"),
145        namespaces::CREATIVE_COMMONS => Some("creativeCommons"),
146        namespaces::THREADING => Some("thr"),
147        namespaces::SLASH => Some("slash"),
148        namespaces::WFW => Some("wfw"),
149        _ => None,
150    }
151}
152
153#[cfg(test)]
154mod tests {
155    use super::*;
156
157    #[test]
158    fn test_get_namespace_uri() {
159        assert_eq!(
160            get_namespace_uri("dc"),
161            Some("http://purl.org/dc/elements/1.1/")
162        );
163        assert_eq!(
164            get_namespace_uri("content"),
165            Some("http://purl.org/rss/1.0/modules/content/")
166        );
167        assert_eq!(
168            get_namespace_uri("media"),
169            Some("http://search.yahoo.com/mrss/")
170        );
171        assert_eq!(get_namespace_uri("unknown"), None);
172    }
173
174    #[test]
175    fn test_get_namespace_prefix() {
176        assert_eq!(
177            get_namespace_prefix("http://purl.org/dc/elements/1.1/"),
178            Some("dc")
179        );
180        assert_eq!(
181            get_namespace_prefix("http://purl.org/rss/1.0/modules/content/"),
182            Some("content")
183        );
184        assert_eq!(
185            get_namespace_prefix("http://search.yahoo.com/mrss/"),
186            Some("media")
187        );
188        assert_eq!(get_namespace_prefix("http://unknown.example.com/"), None);
189    }
190}