use anyhow::Result;
use image::ImageFormat;
use std::path::Path;
pub fn get_format_from_extension(path: &str) -> Option<ImageFormat> {
let ext = Path::new(path)
.extension()
.and_then(|e| e.to_str())
.map(|s| s.to_lowercase())?;
match ext.as_str() {
"jpg" | "jpeg" => Some(ImageFormat::Jpeg),
"png" => Some(ImageFormat::Png),
"webp" => Some(ImageFormat::WebP),
"bmp" => Some(ImageFormat::Bmp),
"gif" => Some(ImageFormat::Gif),
"ico" => Some(ImageFormat::Ico),
"tif" | "tiff" => Some(ImageFormat::Tiff),
"avif" => Some(ImageFormat::Avif),
_ => None,
}
}
pub fn get_file_size(path: &str) -> Result<u64> {
Ok(std::fs::metadata(path)?.len())
}
pub fn format_file_size(bytes: u64) -> String {
if bytes < 1024 {
format!("{} B", bytes)
} else if bytes < 1024 * 1024 {
format!("{:.1} KB", bytes as f64 / 1024.0)
} else if bytes < 1024 * 1024 * 1024 {
format!("{:.2} MB", bytes as f64 / (1024.0 * 1024.0))
} else {
format!("{:.2} GB", bytes as f64 / (1024.0 * 1024.0 * 1024.0))
}
}
pub fn clamp<T: PartialOrd>(value: T, min: T, max: T) -> T {
if value < min {
min
} else if value > max {
max
} else {
value
}
}