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///
12/// # Usage
13///
14/// These handlers are called by the main parsers when encountering
15/// namespaced elements. They update the feed metadata or entries
16/// with information from the namespace elements.
17///
18/// # Example
19///
20/// ```
21/// use feedparser_rs::namespace::dublin_core;
22/// use feedparser_rs::FeedMeta;
23///
24/// let mut feed = FeedMeta::default();
25/// dublin_core::handle_feed_element("creator", "John Doe", &mut feed);
26/// assert_eq!(feed.author.as_deref(), Some("John Doe"));
27/// ```
28/// Creative Commons license information
29pub mod cc;
30/// Content Module for RSS 1.0
31pub mod content;
32/// Dublin Core Metadata Element Set
33pub mod dublin_core;
34/// GeoRSS geographic location data
35pub mod georss;
36/// Media RSS specification
37pub mod media_rss;
38
39/// Common namespace URIs used in feeds
40pub mod namespaces {
41    /// Dublin Core Metadata Element Set
42    pub const DUBLIN_CORE: &str = "http://purl.org/dc/elements/1.1/";
43
44    /// Content Module for RSS 1.0
45    pub const CONTENT: &str = "http://purl.org/rss/1.0/modules/content/";
46
47    /// Media RSS
48    pub const MEDIA: &str = "http://search.yahoo.com/mrss/";
49
50    /// Atom 1.0
51    pub const ATOM: &str = "http://www.w3.org/2005/Atom";
52
53    /// RSS 1.0 (RDF)
54    pub const RDF: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
55
56    /// RSS 1.0
57    pub const RSS_10: &str = "http://purl.org/rss/1.0/";
58
59    /// iTunes Podcast
60    pub const ITUNES: &str = "http://www.itunes.com/dtds/podcast-1.0.dtd";
61
62    /// Podcast 2.0
63    pub const PODCAST: &str = "https://podcastindex.org/namespace/1.0";
64
65    /// `GeoRSS`
66    pub const GEORSS: &str = "http://www.georss.org/georss";
67
68    /// Creative Commons (modern)
69    pub const CC: &str = "http://creativecommons.org/ns#";
70
71    /// Creative Commons (legacy Userland)
72    pub const CREATIVE_COMMONS: &str = "http://backend.userland.com/creativeCommonsRssModule";
73}
74
75/// Get namespace URI for a common prefix
76///
77/// # Arguments
78///
79/// * `prefix` - Namespace prefix (e.g., "dc", "content", "media")
80///
81/// # Returns
82///
83/// Returns the namespace URI if the prefix is recognized
84pub fn get_namespace_uri(prefix: &str) -> Option<&'static str> {
85    match prefix {
86        "dc" => Some(namespaces::DUBLIN_CORE),
87        "content" => Some(namespaces::CONTENT),
88        "media" => Some(namespaces::MEDIA),
89        "atom" => Some(namespaces::ATOM),
90        "rdf" => Some(namespaces::RDF),
91        "itunes" => Some(namespaces::ITUNES),
92        "podcast" => Some(namespaces::PODCAST),
93        "georss" => Some(namespaces::GEORSS),
94        "cc" => Some(namespaces::CC),
95        "creativeCommons" => Some(namespaces::CREATIVE_COMMONS),
96        _ => None,
97    }
98}
99
100/// Get common prefix for a namespace URI
101///
102/// # Arguments
103///
104/// * `uri` - Namespace URI
105///
106/// # Returns
107///
108/// Returns the common prefix if the URI is recognized
109pub fn get_namespace_prefix(uri: &str) -> Option<&'static str> {
110    match uri {
111        namespaces::DUBLIN_CORE => Some("dc"),
112        namespaces::CONTENT => Some("content"),
113        namespaces::MEDIA => Some("media"),
114        namespaces::ATOM => Some("atom"),
115        namespaces::RDF => Some("rdf"),
116        namespaces::ITUNES => Some("itunes"),
117        namespaces::PODCAST => Some("podcast"),
118        namespaces::GEORSS => Some("georss"),
119        namespaces::CC => Some("cc"),
120        namespaces::CREATIVE_COMMONS => Some("creativeCommons"),
121        _ => None,
122    }
123}
124
125#[cfg(test)]
126mod tests {
127    use super::*;
128
129    #[test]
130    fn test_get_namespace_uri() {
131        assert_eq!(
132            get_namespace_uri("dc"),
133            Some("http://purl.org/dc/elements/1.1/")
134        );
135        assert_eq!(
136            get_namespace_uri("content"),
137            Some("http://purl.org/rss/1.0/modules/content/")
138        );
139        assert_eq!(
140            get_namespace_uri("media"),
141            Some("http://search.yahoo.com/mrss/")
142        );
143        assert_eq!(get_namespace_uri("unknown"), None);
144    }
145
146    #[test]
147    fn test_get_namespace_prefix() {
148        assert_eq!(
149            get_namespace_prefix("http://purl.org/dc/elements/1.1/"),
150            Some("dc")
151        );
152        assert_eq!(
153            get_namespace_prefix("http://purl.org/rss/1.0/modules/content/"),
154            Some("content")
155        );
156        assert_eq!(
157            get_namespace_prefix("http://search.yahoo.com/mrss/"),
158            Some("media")
159        );
160        assert_eq!(get_namespace_prefix("http://unknown.example.com/"), None);
161    }
162}