ferrite_image/
formats.rs

1use std::ffi::OsStr;
2use tracing::debug;
3
4/// Handles supported image format detection and validation
5pub struct SupportedFormats;
6
7impl SupportedFormats {
8    /// List of supported file extensions
9    pub const EXTENSIONS: &'static [&'static str] =
10        &["jpg", "jpeg", "png", "gif", "bmp", "ico", "tiff", "tga", "webp"];
11
12    /// Checks if a given file extension is supported
13    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    /// Returns a comma-separated string of supported formats
25    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        // Test valid extensions with different cases
37        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        // Test invalid extensions
42        assert!(!SupportedFormats::is_supported(Some(OsStr::new("txt"))));
43        assert!(!SupportedFormats::is_supported(Some(OsStr::new("doc"))));
44
45        // Test None case
46        assert!(!SupportedFormats::is_supported(None));
47    }
48
49    #[test]
50    fn test_formats_string() {
51        let formats = SupportedFormats::supported_formats_string();
52        // Verify all supported formats are in the string
53        for ext in SupportedFormats::EXTENSIONS {
54            assert!(formats.contains(ext));
55        }
56    }
57}