use std::fmt;
use std::num::ParseIntError;
use std::str::FromStr;
use serde::{Deserialize, Serialize};
use strum_macros::EnumIter;
use crate::domain::external_images::ImageType;
#[derive(Debug, Serialize, Deserialize, Clone, EnumIter, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum ImageSize {
Thumb,
Small,
Large,
Custom(u32),
}
impl fmt::Display for ImageSize {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ImageSize::Thumb => write!(f, "thumb"),
ImageSize::Small => write!(f, "small"),
ImageSize::Large => write!(f, "large"),
ImageSize::Custom(width) => write!(f, "{}", width),
}
}
}
impl FromStr for ImageSize {
type Err = ();
fn from_str(input: &str) -> std::result::Result<ImageSize, ()> {
let int_size: core::result::Result<u32, ParseIntError> = input.parse();
match int_size {
Ok(size) => Ok(ImageSize::Custom(size)),
Err(_) => match input {
"thumb" => Ok(ImageSize::Thumb),
"small" => Ok(ImageSize::Small),
"large" => Ok(ImageSize::Small),
_ => Err(()),
},
}
}
}
impl ImageSize {
pub fn to_size(&self) -> u32 {
match self {
ImageSize::Thumb => 258,
ImageSize::Small => 512,
ImageSize::Large => 1024,
ImageSize::Custom(width) => *width,
}
}
pub fn to_filename_element(&self) -> String {
format!(".{}", self)
}
pub fn optional_to_filename_element(optinal: &Option<Self>) -> String {
match optinal {
Some(kind) => kind.to_filename_element(),
None => "".to_string(),
}
}
}
#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
pub struct ImageRequestOptions {
pub size: Option<ImageSize>,
#[serde(rename = "type")]
pub kind: Option<ImageType>,
#[serde(default)]
pub defaulting: bool,
}