From 8048daa265a1f3ab228d1c971370478c7d81687d Mon Sep 17 00:00:00 2001 From: Ruben Arts Date: Wed, 12 Jul 2023 09:23:14 +0200 Subject: [PATCH 1/2] feat: add from_str to the shellenum --- crates/rattler_shell/src/shell/mod.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/crates/rattler_shell/src/shell/mod.rs b/crates/rattler_shell/src/shell/mod.rs index 6d9a299c7..18bf72b3a 100644 --- a/crates/rattler_shell/src/shell/mod.rs +++ b/crates/rattler_shell/src/shell/mod.rs @@ -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. @@ -510,6 +512,30 @@ 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 { + 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 { let name = path.file_stem()?.to_str()?; From e846455772ccfc94f37e02da26029f3e23787307 Mon Sep 17 00:00:00 2001 From: Ruben Arts Date: Wed, 12 Jul 2023 09:28:46 +0200 Subject: [PATCH 2/2] misc: use from_str to reduce duplicate code --- crates/rattler_shell/src/shell/mod.rs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/crates/rattler_shell/src/shell/mod.rs b/crates/rattler_shell/src/shell/mod.rs index 18bf72b3a..6ff7a7a1a 100644 --- a/crates/rattler_shell/src/shell/mod.rs +++ b/crates/rattler_shell/src/shell/mod.rs @@ -539,15 +539,7 @@ impl FromStr for ShellEnum { /// Determine the shell from a path to a shell. fn parse_shell_from_path(path: &Path) -> Option { 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.