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