use std::path::PathBuf;
use clap::builder::PossibleValue;
use clap::{Parser, ValueEnum};
use crate::constants::QUALITY;
#[derive(Parser)]
#[command(name = "Reduce Image Size")]
#[command(author, version, about, long_about = None)]
pub struct Args {
pub src_dir: PathBuf,
pub dst_dir: PathBuf,
#[arg(short, long)]
pub recursive: bool,
#[arg(long)]
pub resize: bool,
#[arg(short, long, default_value_t = QUALITY,
value_parser = clap::value_parser!(i32).range(1..=100))]
pub quality: i32,
#[arg(short, long, default_value_t = SizeCLI::DEFAULT)]
pub size: SizeCLI,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum SizeCLI {
DEFAULT,
S,
M,
L,
}
impl SizeCLI {
pub fn possible_values() -> impl Iterator<Item = PossibleValue> {
Self::value_variants()
.iter()
.filter_map(ValueEnum::to_possible_value)
}
}
impl Default for SizeCLI {
fn default() -> Self {
Self::DEFAULT
}
}
impl std::fmt::Display for SizeCLI {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.to_possible_value()
.expect("No values are skipped.")
.get_name()
.fmt(f)
}
}
impl std::str::FromStr for SizeCLI {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
for variant in Self::value_variants() {
if variant.to_possible_value().unwrap().matches(s, false) {
return Ok(*variant);
}
}
Err(format!("Invalid variant: {s}"))
}
}
impl ValueEnum for SizeCLI {
fn value_variants<'a>() -> &'a [Self] {
&[Self::DEFAULT, Self::S, Self::M, Self::L]
}
fn to_possible_value(&self) -> Option<PossibleValue> {
Some(match self {
Self::DEFAULT => PossibleValue::new("DEFAULT"),
Self::S => PossibleValue::new("S").alias("s"),
Self::M => PossibleValue::new("M").alias("m"),
Self::L => PossibleValue::new("L").alias("l"),
})
}
}