Refactor file type checking for better consistency
Fix tests
This commit is contained in:
85
src/file_types.rs
Normal file
85
src/file_types.rs
Normal file
@@ -0,0 +1,85 @@
|
||||
use std::path::Path;
|
||||
use walkdir::DirEntry;
|
||||
|
||||
/// Supported image file extensions
|
||||
pub const IMAGE_EXTENSIONS: &[&str] = &[
|
||||
"jpg", "jpeg", "png", "webp", "tiff", "tif", "heif", "heic", "avif", "nef",
|
||||
];
|
||||
|
||||
/// Supported video file extensions
|
||||
pub const VIDEO_EXTENSIONS: &[&str] = &["mp4", "mov", "avi", "mkv"];
|
||||
|
||||
/// Check if a path has an image extension
|
||||
pub fn is_image_file(path: &Path) -> bool {
|
||||
if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
|
||||
let ext_lower = ext.to_lowercase();
|
||||
IMAGE_EXTENSIONS.contains(&ext_lower.as_str())
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if a path has a video extension
|
||||
pub fn is_video_file(path: &Path) -> bool {
|
||||
if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
|
||||
let ext_lower = ext.to_lowercase();
|
||||
VIDEO_EXTENSIONS.contains(&ext_lower.as_str())
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if a path has a supported media extension (image or video)
|
||||
pub fn is_media_file(path: &Path) -> bool {
|
||||
is_image_file(path) || is_video_file(path)
|
||||
}
|
||||
|
||||
/// Check if a DirEntry is an image file (for walkdir usage)
|
||||
pub fn direntry_is_image(entry: &DirEntry) -> bool {
|
||||
is_image_file(&entry.path())
|
||||
}
|
||||
|
||||
/// Check if a DirEntry is a video file (for walkdir usage)
|
||||
pub fn direntry_is_video(entry: &DirEntry) -> bool {
|
||||
is_video_file(&entry.path())
|
||||
}
|
||||
|
||||
/// Check if a DirEntry is a media file (for walkdir usage)
|
||||
pub fn direntry_is_media(entry: &DirEntry) -> bool {
|
||||
is_media_file(&entry.path())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::path::Path;
|
||||
|
||||
#[test]
|
||||
fn test_is_image_file() {
|
||||
assert!(is_image_file(Path::new("photo.jpg")));
|
||||
assert!(is_image_file(Path::new("photo.JPG")));
|
||||
assert!(is_image_file(Path::new("photo.png")));
|
||||
assert!(is_image_file(Path::new("photo.nef")));
|
||||
assert!(!is_image_file(Path::new("video.mp4")));
|
||||
assert!(!is_image_file(Path::new("document.txt")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_video_file() {
|
||||
assert!(is_video_file(Path::new("video.mp4")));
|
||||
assert!(is_video_file(Path::new("video.MP4")));
|
||||
assert!(is_video_file(Path::new("video.mov")));
|
||||
assert!(is_video_file(Path::new("video.avi")));
|
||||
assert!(!is_video_file(Path::new("photo.jpg")));
|
||||
assert!(!is_video_file(Path::new("document.txt")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_media_file() {
|
||||
assert!(is_media_file(Path::new("photo.jpg")));
|
||||
assert!(is_media_file(Path::new("video.mp4")));
|
||||
assert!(is_media_file(Path::new("photo.PNG")));
|
||||
assert!(!is_media_file(Path::new("document.txt")));
|
||||
assert!(!is_media_file(Path::new("no_extension")));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user