Skip to main content

feedparser_rs/namespace/
slash.rs

1/// Slash namespace — `http://purl.org/rss/1.0/modules/slash/`
2/// WFW (Well-Formed Web) namespace — `http://wellformedweb.org/CommentAPI/`
3///
4/// These namespaces are commonly used together in `WordPress`, `Blogger`, and
5/// `Movable Type` feeds to expose comment metadata.
6///
7/// - `slash:comments` — integer count of comments on an entry
8/// - `wfw:commentRss` — URL of the entry's comment RSS feed
9use crate::types::Entry;
10
11/// Slash namespace URI
12pub const SLASH_NAMESPACE: &str = "http://purl.org/rss/1.0/modules/slash/";
13
14/// WFW namespace URI
15pub const WFW_NAMESPACE: &str = "http://wellformedweb.org/CommentAPI/";
16
17/// Handle a Slash namespace element at entry level
18///
19/// # Arguments
20///
21/// * `element` - Local name of the element (without namespace prefix)
22/// * `text` - Text content of the element
23/// * `entry` - Entry to update
24pub fn handle_slash_entry_element(element: &str, text: &str, entry: &mut Entry) {
25    if element == "comments" {
26        // Ignore non-integer values silently (bozo pattern: keep parsing)
27        entry.slash_comments = text.trim().parse::<u32>().ok();
28    } else if element == "hit_parade" {
29        entry.slash_hit_parade = Some(text.trim().to_string());
30    }
31}
32
33/// Handle a WFW namespace element at entry level
34///
35/// # Arguments
36///
37/// * `element` - Local name of the element (without namespace prefix)
38/// * `text` - Text content of the element
39/// * `entry` - Entry to update
40pub fn handle_wfw_entry_element(element: &str, text: &str, entry: &mut Entry) {
41    if element == "commentRss" || element == "commentrss" {
42        entry.wfw_comment_rss = Some(text.trim().to_string());
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn test_slash_comments_integer() {
52        let mut entry = Entry::default();
53        handle_slash_entry_element("comments", "42", &mut entry);
54        assert_eq!(entry.slash_comments, Some(42));
55    }
56
57    #[test]
58    fn test_slash_comments_with_whitespace() {
59        let mut entry = Entry::default();
60        handle_slash_entry_element("comments", "  7  ", &mut entry);
61        assert_eq!(entry.slash_comments, Some(7));
62    }
63
64    #[test]
65    fn test_slash_comments_zero() {
66        let mut entry = Entry::default();
67        handle_slash_entry_element("comments", "0", &mut entry);
68        assert_eq!(entry.slash_comments, Some(0));
69    }
70
71    #[test]
72    fn test_slash_comments_invalid_is_ignored() {
73        let mut entry = Entry::default();
74        handle_slash_entry_element("comments", "not-a-number", &mut entry);
75        assert_eq!(entry.slash_comments, None);
76    }
77
78    #[test]
79    fn test_slash_unknown_element_ignored() {
80        let mut entry = Entry::default();
81        handle_slash_entry_element("section", "tech", &mut entry);
82        assert_eq!(entry.slash_comments, None);
83    }
84
85    #[test]
86    fn test_wfw_comment_rss() {
87        let mut entry = Entry::default();
88        handle_wfw_entry_element(
89            "commentRss",
90            "https://example.com/post/comments/feed/",
91            &mut entry,
92        );
93        assert_eq!(
94            entry.wfw_comment_rss.as_deref(),
95            Some("https://example.com/post/comments/feed/")
96        );
97    }
98
99    #[test]
100    fn test_wfw_comment_rss_lowercase_variant() {
101        let mut entry = Entry::default();
102        handle_wfw_entry_element(
103            "commentrss",
104            "https://example.com/post/comments/feed/",
105            &mut entry,
106        );
107        assert_eq!(
108            entry.wfw_comment_rss.as_deref(),
109            Some("https://example.com/post/comments/feed/")
110        );
111    }
112
113    #[test]
114    fn test_wfw_unknown_element_ignored() {
115        let mut entry = Entry::default();
116        handle_wfw_entry_element("other", "value", &mut entry);
117        assert_eq!(entry.wfw_comment_rss, None);
118    }
119}