foundry_common/
clap.rs

1use clap_complete::{Shell as ClapCompleteShell, aot::Generator};
2use clap_complete_fig::Fig as ClapFig;
3use clap_complete_nushell::Nushell;
4
5#[derive(Clone, Copy)]
6pub enum Shell {
7    ClapCompleteShell(ClapCompleteShell),
8    Nushell,
9    Fig,
10}
11
12impl clap::ValueEnum for Shell {
13    fn value_variants<'a>() -> &'a [Self] {
14        &[
15            Self::ClapCompleteShell(ClapCompleteShell::Bash),
16            Self::ClapCompleteShell(ClapCompleteShell::Zsh),
17            Self::ClapCompleteShell(ClapCompleteShell::Fish),
18            Self::ClapCompleteShell(ClapCompleteShell::PowerShell),
19            Self::ClapCompleteShell(ClapCompleteShell::Elvish),
20            Self::Nushell,
21            Self::Fig,
22        ]
23    }
24
25    fn to_possible_value(&self) -> Option<clap::builder::PossibleValue> {
26        match self {
27            Self::ClapCompleteShell(shell) => shell.to_possible_value(),
28            Self::Nushell => Some(clap::builder::PossibleValue::new("nushell")),
29            Self::Fig => Some(clap::builder::PossibleValue::new("fig")),
30        }
31    }
32}
33
34impl Generator for Shell {
35    fn file_name(&self, name: &str) -> String {
36        match self {
37            Self::ClapCompleteShell(shell) => shell.file_name(name),
38            Self::Nushell => Nushell.file_name(name),
39            Self::Fig => ClapFig.file_name(name),
40        }
41    }
42
43    fn generate(&self, cmd: &clap::Command, buf: &mut dyn std::io::Write) {
44        match self {
45            Self::ClapCompleteShell(shell) => shell.generate(cmd, buf),
46            Self::Nushell => Nushell.generate(cmd, buf),
47            Self::Fig => ClapFig.generate(cmd, buf),
48        }
49    }
50}