use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
pub struct Size {
pub width: u32,
pub height: u32
}
#[derive(Debug, Clone, Copy, PartialEq, Deserialize, Serialize)]
#[non_exhaustive]
pub enum ImageFormat {
Png,
Jpeg,
Webp,
JpegWith {
quality: f32
},
WebpWith {
quality: f32
}
}
#[allow(unused)]
impl ImageFormat {
pub(crate) fn mime_type(&self) -> &'static str {
match self {
ImageFormat::Jpeg | ImageFormat::JpegWith { .. } => "image/jpeg",
ImageFormat::Webp | ImageFormat::WebpWith { .. } => "image/webp",
ImageFormat::Png => "image/png",
}
}
pub(crate) fn from_mime_type(mime_type: &str) -> Option<Self> {
match mime_type {
"image/jpeg" | "image/jpg" => Some(Self::Jpeg),
"image/webp" => Some(Self::Webp),
"image/png" => Some(Self::Png),
_ => None,
}
}
pub(crate) fn from_name(name: &str) -> Option<Self> {
if name.eq_ignore_ascii_case("jpeg") || name.eq_ignore_ascii_case("jpg") {
Some(Self::Jpeg)
}
else if name.eq_ignore_ascii_case("webp") {
Some(Self::Webp)
}
else if name.eq_ignore_ascii_case("png") {
Some(Self::Png)
}
else {
None
}
}
pub(crate) fn to_quality_and_format_str(&self) -> (f32, &'static str) {
match self {
ImageFormat::Png => (1.0, "Png"),
ImageFormat::Jpeg => (0.75, "Jpeg"),
ImageFormat::Webp => (0.7, "Webp"),
ImageFormat::JpegWith { quality } => (*quality, "Jpeg"),
ImageFormat::WebpWith { quality } => (*quality, "Webp"),
}
}
}