axum-image-endpoint 0.1.1

Image Endpoint to upload big images and receive rescaled ones
Documentation
use serde::Deserialize;

#[derive(Debug, Clone, Deserialize)]
pub struct GetImageOptions {
    pub(crate) width: Option<u32>,
    pub(crate) height: Option<u32>,
    pub(crate) quality: Option<u8>,
    pub(crate) rotate: Option<u64>,
    pub(crate) grayscale: Option<bool>,
    pub(crate) imgtype: Option<ImageType>,
}

impl GetImageOptions {
    pub(crate) fn no_options_set(&self) -> bool {
        self.width.is_none()
            && self.height.is_none()
            && self.quality.is_none()
            && self.rotate.is_none()
            && self.grayscale.is_none()
            && self.imgtype.is_none()
    }
}
impl FolderName for GetImageOptions {
    fn folder_name(&self) -> String {
        let mut s = String::new();

        // version if we would like to add more options later, so we can differentiate
        s.push('v');
        s.push('1');
        s.push('_');

        s.push('w');
        s.push_str(&self.width.folder_name());
        s.push('_');

        s.push('h');
        s.push_str(&self.height.folder_name());
        s.push('_');

        s.push('q');
        s.push_str(&self.quality.folder_name());
        s.push('_');

        s.push('r');
        s.push_str(&self.rotate.folder_name());
        s.push('_');

        s.push('g');
        s.push_str(&self.grayscale.folder_name());
        s.push('_');

        s.push('t');
        s.push_str(&self.imgtype.folder_name());

        s
    }
}

pub(crate) trait FolderName {
    fn folder_name(&self) -> String;
}

impl<T: ToString> FolderName for Option<T> {
    fn folder_name(&self) -> String {
        match self {
            Some(value) => value.to_string(),
            None => "None".to_owned(),
        }
    }
}

#[derive(Debug, Clone, Copy, Deserialize)]
pub enum ImageType {
    Jpeg,
    Jpg,
    Png,
    Webp,
}

impl FolderName for Option<ImageType> {
    fn folder_name(&self) -> String {
        match self {
            Some(image_type) => match image_type {
                ImageType::Jpeg | ImageType::Jpg => "Jpeg".to_owned(),
                ImageType::Png => "Png".to_owned(),
                ImageType::Webp => "Webp".to_owned(),
            },
            None => "None".to_owned(),
        }
    }
}