1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use std::fmt::Formatter;

use anyhow::anyhow;

#[derive(Clone, Default, Debug)]
pub enum Status {
    #[default]
    Submit,
    Abort,
    Edit,
}

impl std::fmt::Display for Status {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            Self::Submit => "Submit",
            Self::Abort => "Abort",
            Self::Edit => "Edit",
        };
        write!(f, "{s}")
    }
}

impl std::str::FromStr for Status {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().trim() {
            "y" | "yes" | "" => Ok(Self::Submit),
            "n" | "no" => Ok(Self::Abort),
            "e" | "edit" => Ok(Self::Edit),
            &_ => Err(anyhow!("input error")),
        }
    }
}