use std::ffi::OsStr;
use tracing::debug;
pub struct SupportedFormats;
impl SupportedFormats {
pub const EXTENSIONS: &'static [&'static str] =
&["jpg", "jpeg", "png", "gif", "bmp", "ico", "tiff", "tga", "webp"];
pub fn is_supported(extension: Option<&OsStr>) -> bool {
let result = extension
.and_then(|e| e.to_str())
.map(|e| e.to_lowercase())
.map(|e| Self::EXTENSIONS.contains(&e.as_str()))
.unwrap_or(false);
debug!(extension = ?extension, supported = result, "Checking file extension support");
result
}
pub fn supported_formats_string() -> String {
Self::EXTENSIONS.join(", ")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_supported_extensions() {
assert!(SupportedFormats::is_supported(Some(OsStr::new("jpg"))));
assert!(SupportedFormats::is_supported(Some(OsStr::new("JPG"))));
assert!(SupportedFormats::is_supported(Some(OsStr::new("PNG"))));
assert!(!SupportedFormats::is_supported(Some(OsStr::new("txt"))));
assert!(!SupportedFormats::is_supported(Some(OsStr::new("doc"))));
assert!(!SupportedFormats::is_supported(None));
}
#[test]
fn test_formats_string() {
let formats = SupportedFormats::supported_formats_string();
for ext in SupportedFormats::EXTENSIONS {
assert!(formats.contains(ext));
}
}
}