nameless_clap_generate/
shell.rs

1use std::fmt::Display;
2use std::str::FromStr;
3
4/// Shell with auto-generated completion script available.
5#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
6#[non_exhaustive]
7pub enum Shell {
8    /// Bourne Again SHell (bash)
9    Bash,
10    /// Elvish shell
11    Elvish,
12    /// Friendly Interactive SHell (fish)
13    Fish,
14    /// PowerShell
15    PowerShell,
16    /// Z SHell (zsh)
17    Zsh,
18}
19
20impl Shell {
21    /// A list of supported shells in `[&'static str]` form.
22    pub fn variants() -> [&'static str; 5] {
23        ["bash", "elvish", "fish", "powershell", "zsh"]
24    }
25}
26
27impl Display for Shell {
28    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29        match *self {
30            Shell::Bash => write!(f, "bash"),
31            Shell::Elvish => write!(f, "elvish"),
32            Shell::Fish => write!(f, "fish"),
33            Shell::PowerShell => write!(f, "powershell"),
34            Shell::Zsh => write!(f, "zsh"),
35        }
36    }
37}
38
39impl FromStr for Shell {
40    type Err = String;
41
42    fn from_str(s: &str) -> Result<Self, Self::Err> {
43        match s.to_ascii_lowercase().as_str() {
44            "bash" => Ok(Shell::Bash),
45            "elvish" => Ok(Shell::Elvish),
46            "fish" => Ok(Shell::Fish),
47            "powershell" => Ok(Shell::PowerShell),
48            "zsh" => Ok(Shell::Zsh),
49            _ => Err(String::from(
50                "[valid values: bash, elvish, fish, powershell, zsh]",
51            )),
52        }
53    }
54}