Skip to main content

feedparser_rs/compat/
mod.rs

1//! Compatibility utilities for feedparser API
2//!
3//! This module provides utilities to ensure API compatibility with
4//! Python's feedparser library.
5
6use crate::types::FeedVersion;
7
8/// Normalize feed type string to Python feedparser format
9///
10/// Converts version enum to Python feedparser-compatible string format:
11/// - "RSS 2.0" -> "rss20"
12/// - "Atom 1.0" -> "atom10"
13/// - etc.
14///
15/// # Arguments
16///
17/// * `version` - Feed version to normalize
18///
19/// # Returns
20///
21/// Normalized version string compatible with Python feedparser
22///
23/// # Examples
24///
25/// ```
26/// use feedparser_rs::{compat::normalize_version, FeedVersion};
27///
28/// assert_eq!(normalize_version(FeedVersion::Rss20), "rss20");
29/// assert_eq!(normalize_version(FeedVersion::Atom10), "atom10");
30/// assert_eq!(normalize_version(FeedVersion::Unknown), "");
31/// ```
32#[must_use]
33pub fn normalize_version(version: FeedVersion) -> String {
34    version.as_str().to_string()
35}
36
37/// Convert duration in seconds to HH:MM:SS format
38///
39/// Formats duration for display in podcast feeds and other contexts
40/// where human-readable time format is needed.
41///
42/// # Arguments
43///
44/// * `seconds` - Duration in seconds
45///
46/// # Returns
47///
48/// Duration string in HH:MM:SS format
49///
50/// # Examples
51///
52/// ```
53/// use feedparser_rs::compat::format_duration;
54///
55/// assert_eq!(format_duration(0), "0:00:00");
56/// assert_eq!(format_duration(90), "0:01:30");
57/// assert_eq!(format_duration(3661), "1:01:01");
58/// assert_eq!(format_duration(36000), "10:00:00");
59/// ```
60#[must_use]
61pub fn format_duration(seconds: u32) -> String {
62    let hours = seconds / 3600;
63    let minutes = (seconds % 3600) / 60;
64    let secs = seconds % 60;
65    format!("{hours}:{minutes:02}:{secs:02}")
66}
67
68/// Check if a string is a valid feed version identifier
69///
70/// Validates whether a version string is one that
71/// [`FeedVersion::as_str`] can report for a recognized format.
72///
73/// # Arguments
74///
75/// * `version` - Version string to validate
76///
77/// # Returns
78///
79/// `true` if version is valid, `false` otherwise
80///
81/// # Examples
82///
83/// ```
84/// use feedparser_rs::compat::is_valid_version;
85///
86/// assert!(is_valid_version("rss20"));
87/// assert!(is_valid_version("atom10"));
88/// assert!(is_valid_version("json11"));
89/// assert!(is_valid_version("rss"));
90/// assert!(!is_valid_version("invalid"));
91/// assert!(!is_valid_version(""));
92/// ```
93#[must_use]
94pub fn is_valid_version(version: &str) -> bool {
95    matches!(
96        version,
97        "rss090"
98            | "rss091n"
99            | "rss091u"
100            | "rss092"
101            | "rss093"
102            | "rss094"
103            | "rss"
104            | "rss10"
105            | "rss20"
106            | "atom03"
107            | "atom10"
108            | "json10"
109            | "json11"
110    )
111}
112
113#[cfg(test)]
114mod tests {
115    use super::*;
116
117    #[test]
118    fn test_normalize_version() {
119        assert_eq!(normalize_version(FeedVersion::Rss20), "rss20");
120        assert_eq!(normalize_version(FeedVersion::Rss10), "rss10");
121        assert_eq!(normalize_version(FeedVersion::Atom10), "atom10");
122        assert_eq!(normalize_version(FeedVersion::Atom03), "atom03");
123        assert_eq!(normalize_version(FeedVersion::JsonFeed10), "json10");
124        assert_eq!(normalize_version(FeedVersion::JsonFeed11), "json11");
125        assert_eq!(normalize_version(FeedVersion::Unknown), "");
126    }
127
128    #[test]
129    fn test_format_duration_zero() {
130        assert_eq!(format_duration(0), "0:00:00");
131    }
132
133    #[test]
134    fn test_format_duration_seconds_only() {
135        assert_eq!(format_duration(30), "0:00:30");
136        assert_eq!(format_duration(59), "0:00:59");
137    }
138
139    #[test]
140    fn test_format_duration_minutes() {
141        assert_eq!(format_duration(60), "0:01:00");
142        assert_eq!(format_duration(90), "0:01:30");
143        assert_eq!(format_duration(150), "0:02:30");
144        assert_eq!(format_duration(3599), "0:59:59");
145    }
146
147    #[test]
148    fn test_format_duration_hours() {
149        assert_eq!(format_duration(3600), "1:00:00");
150        assert_eq!(format_duration(3661), "1:01:01");
151        assert_eq!(format_duration(7200), "2:00:00");
152        assert_eq!(format_duration(36000), "10:00:00");
153    }
154
155    #[test]
156    fn test_format_duration_large() {
157        assert_eq!(format_duration(86399), "23:59:59");
158        assert_eq!(format_duration(86400), "24:00:00");
159        assert_eq!(format_duration(90061), "25:01:01");
160    }
161
162    #[test]
163    fn test_is_valid_version_valid() {
164        assert!(is_valid_version("rss090"));
165        assert!(is_valid_version("rss091n"));
166        assert!(is_valid_version("rss"));
167        assert!(!is_valid_version("rss091"));
168    }
169
170    /// Exhaustive, so a new variant cannot compile until the walk below visits it.
171    const fn successor(version: FeedVersion) -> Option<FeedVersion> {
172        use FeedVersion as V;
173        match version {
174            V::Rss090 => Some(V::Rss091Netscape),
175            V::Rss091Netscape => Some(V::Rss091Userland),
176            V::Rss091Userland => Some(V::Rss092),
177            V::Rss092 => Some(V::Rss093),
178            V::Rss093 => Some(V::Rss094),
179            V::Rss094 => Some(V::RssUnknown),
180            V::RssUnknown => Some(V::Rss10),
181            V::Rss10 => Some(V::Rss20),
182            V::Rss20 => Some(V::Atom03),
183            V::Atom03 => Some(V::Atom10),
184            V::Atom10 => Some(V::JsonFeed10),
185            V::JsonFeed10 => Some(V::JsonFeed11),
186            V::JsonFeed11 => Some(V::Unknown),
187            V::Unknown => None,
188        }
189    }
190
191    #[test]
192    fn test_is_valid_version_matches_every_variant() {
193        for version in std::iter::successors(Some(FeedVersion::Rss090), |&v| successor(v)) {
194            assert_eq!(
195                is_valid_version(version.as_str()),
196                version != FeedVersion::Unknown,
197                "{version:?}"
198            );
199        }
200    }
201
202    #[test]
203    fn test_is_valid_version_invalid() {
204        assert!(!is_valid_version(""));
205        assert!(!is_valid_version("invalid"));
206        assert!(!is_valid_version("rss30"));
207        assert!(!is_valid_version("atom20"));
208        assert!(!is_valid_version("RSS20")); // Case sensitive
209        assert!(!is_valid_version("json12"));
210        assert!(!is_valid_version("rdf"));
211    }
212
213    #[test]
214    fn test_is_valid_version_edge_cases() {
215        assert!(!is_valid_version(" rss20"));
216        assert!(!is_valid_version("rss20 "));
217        assert!(!is_valid_version("rss 20"));
218    }
219}