use std::{
fmt::{Display, Formatter},
str::FromStr,
};
use crate::mit::lib::errors::DeserializeRotationOptionError;
#[derive(clap::ValueEnum, Ord, PartialOrd, Eq, PartialEq, Debug, Clone, Copy)]
pub enum RotationOption {
Off,
RoundRobin,
Random,
}
const OFF_DISPLAY: &str = "off";
const ROUND_ROBIN_DISPLAY: &str = "round-robin";
const RANDOM_DISPLAY: &str = "random";
impl FromStr for RotationOption {
type Err = DeserializeRotationOptionError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_ascii_lowercase().as_str() {
OFF_DISPLAY => Ok(Self::Off),
ROUND_ROBIN_DISPLAY => Ok(Self::RoundRobin),
RANDOM_DISPLAY => Ok(Self::Random),
_ => Err(DeserializeRotationOptionError { src: s.into() }),
}
}
}
impl Display for RotationOption {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::Off => write!(f, "{OFF_DISPLAY}"),
Self::RoundRobin => write!(f, "{ROUND_ROBIN_DISPLAY}"),
Self::Random => write!(f, "{RANDOM_DISPLAY}"),
}
}
}