1use std::ffi::OsStr;
2use tracing::debug;
3
4pub struct SupportedFormats;
6
7impl SupportedFormats {
8 pub const EXTENSIONS: &'static [&'static str] =
10 &["jpg", "jpeg", "png", "gif", "bmp", "ico", "tiff", "tga", "webp"];
11
12 pub fn is_supported(extension: Option<&OsStr>) -> bool {
14 let result = extension
15 .and_then(|e| e.to_str())
16 .map(|e| e.to_lowercase())
17 .map(|e| Self::EXTENSIONS.contains(&e.as_str()))
18 .unwrap_or(false);
19
20 debug!(extension = ?extension, supported = result, "Checking file extension support");
21 result
22 }
23
24 pub fn supported_formats_string() -> String {
26 Self::EXTENSIONS.join(", ")
27 }
28}
29
30#[cfg(test)]
31mod tests {
32 use super::*;
33
34 #[test]
35 fn test_supported_extensions() {
36 assert!(SupportedFormats::is_supported(Some(OsStr::new("jpg"))));
38 assert!(SupportedFormats::is_supported(Some(OsStr::new("JPG"))));
39 assert!(SupportedFormats::is_supported(Some(OsStr::new("PNG"))));
40
41 assert!(!SupportedFormats::is_supported(Some(OsStr::new("txt"))));
43 assert!(!SupportedFormats::is_supported(Some(OsStr::new("doc"))));
44
45 assert!(!SupportedFormats::is_supported(None));
47 }
48
49 #[test]
50 fn test_formats_string() {
51 let formats = SupportedFormats::supported_formats_string();
52 for ext in SupportedFormats::EXTENSIONS {
54 assert!(formats.contains(ext));
55 }
56 }
57}