use super::ZshOptionState;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum ShellDialect {
Posix,
Mksh,
#[default]
Bash,
Zsh,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct DialectFeatures {
pub(super) double_bracket: bool,
pub(super) arithmetic_command: bool,
pub(super) arithmetic_for: bool,
pub(super) function_keyword: bool,
pub(super) select_loop: bool,
pub(super) coproc_keyword: bool,
pub(super) zsh_repeat_loop: bool,
pub(super) zsh_foreach_loop: bool,
pub(super) zsh_parameter_modifiers: bool,
pub(super) zsh_brace_if: bool,
pub(super) zsh_always: bool,
pub(super) zsh_background_operators: bool,
pub(super) zsh_glob_qualifiers: bool,
}
impl ShellDialect {
pub fn from_name(name: &str) -> Self {
match name.trim().to_ascii_lowercase().as_str() {
"sh" | "dash" | "ksh" | "posix" => Self::Posix,
"mksh" => Self::Mksh,
"zsh" => Self::Zsh,
_ => Self::Bash,
}
}
pub(super) const fn features(self) -> DialectFeatures {
match self {
Self::Posix => DialectFeatures {
double_bracket: false,
arithmetic_command: false,
arithmetic_for: false,
function_keyword: true,
select_loop: false,
coproc_keyword: false,
zsh_repeat_loop: false,
zsh_foreach_loop: false,
zsh_parameter_modifiers: false,
zsh_brace_if: false,
zsh_always: false,
zsh_background_operators: false,
zsh_glob_qualifiers: false,
},
Self::Mksh => DialectFeatures {
double_bracket: true,
arithmetic_command: true,
arithmetic_for: false,
function_keyword: true,
select_loop: true,
coproc_keyword: false,
zsh_repeat_loop: false,
zsh_foreach_loop: false,
zsh_parameter_modifiers: false,
zsh_brace_if: false,
zsh_always: false,
zsh_background_operators: false,
zsh_glob_qualifiers: false,
},
Self::Bash => DialectFeatures {
double_bracket: true,
arithmetic_command: true,
arithmetic_for: true,
function_keyword: true,
select_loop: true,
coproc_keyword: true,
zsh_repeat_loop: false,
zsh_foreach_loop: false,
zsh_parameter_modifiers: false,
zsh_brace_if: false,
zsh_always: false,
zsh_background_operators: false,
zsh_glob_qualifiers: false,
},
Self::Zsh => DialectFeatures {
double_bracket: true,
arithmetic_command: true,
arithmetic_for: true,
function_keyword: true,
select_loop: true,
coproc_keyword: true,
zsh_repeat_loop: true,
zsh_foreach_loop: true,
zsh_parameter_modifiers: true,
zsh_brace_if: true,
zsh_always: true,
zsh_background_operators: true,
zsh_glob_qualifiers: true,
},
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ShellProfile {
pub dialect: ShellDialect,
pub options: Option<ZshOptionState>,
}
impl ShellProfile {
pub fn native(dialect: ShellDialect) -> Self {
Self {
dialect,
options: (dialect == ShellDialect::Zsh).then(ZshOptionState::zsh_default),
}
}
pub fn with_zsh_options(dialect: ShellDialect, options: ZshOptionState) -> Self {
Self {
dialect,
options: (dialect == ShellDialect::Zsh).then_some(options),
}
}
pub fn zsh_options(&self) -> Option<&ZshOptionState> {
self.options.as_ref()
}
}