mit_commit_message_lints/mit/lib/
rotation_option.rs1use std::{
3 fmt::{Display, Formatter},
4 str::FromStr,
5};
6
7use crate::mit::lib::errors::DeserializeRotationOptionError;
8
9#[derive(clap::ValueEnum, Ord, PartialOrd, Eq, PartialEq, Debug, Clone, Copy)]
11pub enum RotationOption {
12 Off,
14 RoundRobin,
16 Random,
18}
19
20const OFF_DISPLAY: &str = "off";
21const ROUND_ROBIN_DISPLAY: &str = "round-robin";
22const RANDOM_DISPLAY: &str = "random";
23
24impl FromStr for RotationOption {
25 type Err = DeserializeRotationOptionError;
26
27 fn from_str(s: &str) -> Result<Self, Self::Err> {
28 match s.to_ascii_lowercase().as_str() {
29 OFF_DISPLAY => Ok(Self::Off),
30 ROUND_ROBIN_DISPLAY => Ok(Self::RoundRobin),
31 RANDOM_DISPLAY => Ok(Self::Random),
32 _ => Err(DeserializeRotationOptionError { src: s.into() }),
33 }
34 }
35}
36
37impl Display for RotationOption {
38 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
39 match self {
40 Self::Off => write!(f, "{OFF_DISPLAY}"),
41 Self::RoundRobin => write!(f, "{ROUND_ROBIN_DISPLAY}"),
42 Self::Random => write!(f, "{RANDOM_DISPLAY}"),
43 }
44 }
45}