aboutsummaryrefslogtreecommitdiff
path: root/mingling_core/src/comp/flags.rs
blob: 490c88b6f9053d3d6a6eb42d78f99eacd38604c7 (plain) (blame)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use just_fmt::snake_case;

/// Represents the shell environment for which the output format is intended.
///
/// This enum defines the supported shell types that can be used for
/// generating shell-specific command syntax, scripts, or completions.
///
/// # Behavior under `structural_renderer` feature
///
/// When the `structural_renderer` feature is enabled, this enum derives
/// [`serde::Serialize`](https://docs.rs/serde/latest/serde/trait.Serialize.html).
/// The serialization produces shell-specific string identifiers:
///
/// - `Bash` serializes to `"bash"`
/// - `Zsh` serializes to `"zsh"`
/// - `Fish` serializes to `"fish"`
/// - `Powershell` serializes to `"powershell"`
/// - `Other(name)` serializes to the inner string value
///
/// This allows the shell type to be transmitted as a plain string over
/// serialization boundaries (e.g., JSON, YAML) when using structural
/// rendering, while deserialization is handled by a separate process
/// (such as the `From<String>` implementation).
#[derive(Default, Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "structural_renderer", derive(serde::Serialize))]
pub enum ShellFlag {
    /// Represents the Bash shell.
    #[default]
    Bash,
    /// Represents the Zsh shell.
    Zsh,
    /// Represents the Fish shell.
    Fish,
    /// Represents `PowerShell`.
    Powershell,
    /// A custom or unsupported shell type, identified by the provided string.
    Other(String),
}

impl From<String> for ShellFlag {
    fn from(s: String) -> Self {
        match s.trim().to_lowercase().as_str() {
            "zsh" => Self::Zsh,
            "bash" => Self::Bash,
            "fish" => Self::Fish,
            "pwsh" | "ps1" | "powershell" => Self::Powershell,
            other => Self::Other(snake_case!(other)),
        }
    }
}

impl From<ShellFlag> for String {
    fn from(flag: ShellFlag) -> Self {
        match flag {
            ShellFlag::Zsh => "zsh".to_string(),
            ShellFlag::Bash => "bash".to_string(),
            ShellFlag::Fish => "fish".to_string(),
            ShellFlag::Powershell => "powershell".to_string(),
            ShellFlag::Other(s) => s,
        }
    }
}