structopt_utilities/completions/
shell.rs

1use core::hint::unreachable_unchecked;
2use structopt::clap::{self, Shell::*};
3
4/// Shell wrapper type with additional traits.
5#[derive(Debug, Copy, Clone)]
6pub struct Shell(pub clap::Shell);
7
8impl Shell {
9    /// Convert a string to `Shell`
10    ///
11    /// ### Safety
12    ///
13    /// It is safe when `text` is always one of `["bash", "fish", "zsh", "powershell", "elvish"]`.
14    /// Otherwise, it returns `unreachable_unchecked()`.
15    pub unsafe fn parse_from_str_unchecked(text: &str) -> Self {
16        Shell(match text {
17            "bash" => Bash,
18            "fish" => Fish,
19            "zsh" => Zsh,
20            "powershell" => PowerShell,
21            "elvish" => Elvish,
22            _ => unreachable_unchecked(),
23        })
24    }
25
26    /// Extract `clap::Shell`.
27    pub fn to_clap(self) -> clap::Shell {
28        self.0
29    }
30
31    /// Wrap `clap::Shell` into `Shell`.
32    pub fn from_clap(clap: clap::Shell) -> Self {
33        Shell(clap)
34    }
35}
36
37impl PartialEq for Shell {
38    fn eq(&self, other: &Self) -> bool {
39        matches!(
40            (self.0, other.0),
41            (Bash, Bash) | (Fish, Fish) | (Zsh, Zsh) | (PowerShell, PowerShell) | (Elvish, Elvish)
42        )
43    }
44}
45
46impl Eq for Shell {}