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/// Syndication Module for RSS 1.0
39pub mod syndication;
40
41/// Common namespace URIs used in feeds
42pub mod namespaces {
43    /// Dublin Core Metadata Element Set
44    pub const DUBLIN_CORE: &str = "http://purl.org/dc/elements/1.1/";
45
46    /// Content Module for RSS 1.0
47    pub const CONTENT: &str = "http://purl.org/rss/1.0/modules/content/";
48
49    /// Media RSS
50    pub const MEDIA: &str = "http://search.yahoo.com/mrss/";
51
52    /// Atom 1.0
53    pub const ATOM: &str = "http://www.w3.org/2005/Atom";
54
55    /// RSS 1.0 (RDF)
56    pub const RDF: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
57
58    /// RSS 1.0
59    pub const RSS_10: &str = "http://purl.org/rss/1.0/";
60
61    /// Syndication Module for RSS 1.0
62    pub const SYNDICATION: &str = "http://purl.org/rss/1.0/modules/syndication/";
63
64    /// iTunes Podcast
65    pub const ITUNES: &str = "http://www.itunes.com/dtds/podcast-1.0.dtd";
66
67    /// Podcast 2.0
68    pub const PODCAST: &str = "https://podcastindex.org/namespace/1.0";
69
70    /// `GeoRSS`
71    pub const GEORSS: &str = "http://www.georss.org/georss";
72
73    /// Creative Commons (modern)
74    pub const CC: &str = "http://creativecommons.org/ns#";
75
76    /// Creative Commons (legacy Userland)
77    pub const CREATIVE_COMMONS: &str = "http://backend.userland.com/creativeCommonsRssModule";
78}
79
80/// Get namespace URI for a common prefix
81///
82/// # Arguments
83///
84/// * `prefix` - Namespace prefix (e.g., "dc", "content", "media")
85///
86/// # Returns
87///
88/// Returns the namespace URI if the prefix is recognized
89pub fn get_namespace_uri(prefix: &str) -> Option<&'static str> {
90    match prefix {
91        "dc" => Some(namespaces::DUBLIN_CORE),
92        "content" => Some(namespaces::CONTENT),
93        "media" => Some(namespaces::MEDIA),
94        "atom" => Some(namespaces::ATOM),
95        "rdf" => Some(namespaces::RDF),
96        "syn" | "syndication" => Some(namespaces::SYNDICATION),
97        "itunes" => Some(namespaces::ITUNES),
98        "podcast" => Some(namespaces::PODCAST),
99        "georss" => Some(namespaces::GEORSS),
100        "cc" => Some(namespaces::CC),
101        "creativeCommons" => Some(namespaces::CREATIVE_COMMONS),
102        _ => None,
103    }
104}
105
106/// Get common prefix for a namespace URI
107///
108/// # Arguments
109///
110/// * `uri` - Namespace URI
111///
112/// # Returns
113///
114/// Returns the common prefix if the URI is recognized
115pub fn get_namespace_prefix(uri: &str) -> Option<&'static str> {
116    match uri {
117        namespaces::DUBLIN_CORE => Some("dc"),
118        namespaces::CONTENT => Some("content"),
119        namespaces::MEDIA => Some("media"),
120        namespaces::ATOM => Some("atom"),
121        namespaces::RDF => Some("rdf"),
122        namespaces::SYNDICATION => Some("syn"),
123        namespaces::ITUNES => Some("itunes"),
124        namespaces::PODCAST => Some("podcast"),
125        namespaces::GEORSS => Some("georss"),
126        namespaces::CC => Some("cc"),
127        namespaces::CREATIVE_COMMONS => Some("creativeCommons"),
128        _ => None,
129    }
130}
131
132#[cfg(test)]
133mod tests {
134    use super::*;
135
136    #[test]
137    fn test_get_namespace_uri() {
138        assert_eq!(
139            get_namespace_uri("dc"),
140            Some("http://purl.org/dc/elements/1.1/")
141        );
142        assert_eq!(
143            get_namespace_uri("content"),
144            Some("http://purl.org/rss/1.0/modules/content/")
145        );
146        assert_eq!(
147            get_namespace_uri("media"),
148            Some("http://search.yahoo.com/mrss/")
149        );
150        assert_eq!(get_namespace_uri("unknown"), None);
151    }
152
153    #[test]
154    fn test_get_namespace_prefix() {
155        assert_eq!(
156            get_namespace_prefix("http://purl.org/dc/elements/1.1/"),
157            Some("dc")
158        );
159        assert_eq!(
160            get_namespace_prefix("http://purl.org/rss/1.0/modules/content/"),
161            Some("content")
162        );
163        assert_eq!(
164            get_namespace_prefix("http://search.yahoo.com/mrss/"),
165            Some("media")
166        );
167        assert_eq!(get_namespace_prefix("http://unknown.example.com/"), None);
168    }
169}