feedparser_rs/compat/
mod.rs1use crate::types::FeedVersion;
7
8#[must_use]
33pub fn normalize_version(version: FeedVersion) -> String {
34 version.as_str().to_string()
35}
36
37#[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#[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 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")); 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}