Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add from_str to the shellenum #258

Merged
merged 2 commits into from
Jul 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions crates/rattler_shell/src/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ use itertools::Itertools;
use rattler_conda_types::Platform;
use std::collections::HashMap;
use std::process::Command;
use std::str::FromStr;
use std::{
fmt::Write,
path::{Path, PathBuf},
};
use thiserror::Error;

/// A trait for generating shell scripts.
/// The trait is implemented for each shell individually.
Expand Down Expand Up @@ -510,18 +512,34 @@ impl ShellEnum {
}
}

/// Parsing of a shell was not possible. The shell mostlikely is not supported.
#[derive(Debug, Error)]
#[error("{0}")]
pub struct ParseShellEnumError(String);

impl FromStr for ShellEnum {
type Err = ParseShellEnumError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"bash" => Ok(Bash.into()),
"zsh" => Ok(Zsh.into()),
"xonsh" => Ok(Xonsh.into()),
"fish" => Ok(Fish.into()),
"cmd" => Ok(CmdExe.into()),
"powershell" | "powershell_ise" => Ok(PowerShell::default().into()),
_ => Err(ParseShellEnumError(format!(
"'{}' is an unknown shell variant",
s
))),
}
}
}

/// Determine the shell from a path to a shell.
fn parse_shell_from_path(path: &Path) -> Option<ShellEnum> {
let name = path.file_stem()?.to_str()?;
match name {
"bash" => Some(Bash.into()),
"zsh" => Some(Zsh.into()),
"xonsh" => Some(Xonsh.into()),
"fish" => Some(Fish.into()),
"cmd" => Some(CmdExe.into()),
"powershell" | "powershell_ise" => Some(PowerShell::default().into()),
_ => None,
}
ShellEnum::from_str(name).ok()
}

/// A helper struct for generating shell scripts.
Expand Down
Loading