use std::collections::HashMap;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Mode {
#[default]
Fit,
Crop,
Exact,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Format {
#[default]
Auto,
Jpeg,
Png,
WebP,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Preset {
pub name: String,
pub width: Option<u32>,
pub height: Option<u32>,
pub mode: Mode,
pub format: Format,
pub quality: u8,
}
impl Preset {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
width: None,
height: None,
mode: Mode::Fit,
format: Format::Auto,
quality: 82,
}
}
pub fn width(mut self, width: u32) -> Self {
self.width = Some(width);
self
}
pub fn height(mut self, height: u32) -> Self {
self.height = Some(height);
self
}
pub fn fit(mut self) -> Self {
self.mode = Mode::Fit;
self
}
pub fn crop(mut self) -> Self {
self.mode = Mode::Crop;
self
}
pub fn exact(mut self) -> Self {
self.mode = Mode::Exact;
self
}
pub fn format(mut self, format: Format) -> Self {
self.format = format;
self
}
pub fn quality(mut self, quality: u8) -> Self {
self.quality = quality.clamp(1, 100);
self
}
}
pub const THUMB: &str = "thumb";
pub const PREVIEW: &str = "preview";
pub fn builtin() -> Vec<Preset> {
vec![
Preset::new(THUMB).width(320).height(320),
Preset::new(PREVIEW).width(1200).height(1200),
]
}
pub fn all(registered: impl IntoIterator<Item = Preset>) -> HashMap<String, Preset> {
builtin()
.into_iter()
.chain(registered)
.map(|p| (p.name.clone(), p))
.collect()
}