Skip to content

Commit

Permalink
Use Shells instead of Box<dyn Shell> everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
amitdahan committed Jul 3, 2023
1 parent 9f5695e commit 7781b5d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 24 deletions.
13 changes: 6 additions & 7 deletions src/commands/completions.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::command::Command;
use crate::config::FnmConfig;
use crate::shell::{infer_shell, Shell};
use crate::shell::infer_shell;
use crate::{cli::Cli, shell::Shells};
use clap::{CommandFactory, Parser, ValueEnum};
use clap_complete::{Generator, Shell as ClapShell};
use clap_complete::{Generator, Shell};
use thiserror::Error;

#[derive(Parser, Debug)]
Expand All @@ -18,12 +18,11 @@ impl Command for Completions {

fn apply(self, _config: &FnmConfig) -> Result<(), Self::Error> {
let mut stdio = std::io::stdout();
let shell: Box<dyn Shell> = self
let shell: Shell = self
.shell
.map(Into::into)
.or_else(|| infer_shell().map(Into::into))
.ok_or(Error::CantInferShell)?;
let shell: ClapShell = shell.into();
.or_else(infer_shell)
.ok_or(Error::CantInferShell)?
.into();
let app = Cli::command();
shell.generate(&app, &mut stdio);
Ok(())
Expand Down
3 changes: 1 addition & 2 deletions src/commands/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@ impl Command for Env {
return Ok(());
}

let shell: Box<dyn Shell> = self
let shell = self
.shell
.map(Into::into)
.or_else(infer_shell)
.ok_or(Error::CantInferShell)?;

Expand Down
16 changes: 9 additions & 7 deletions src/shell/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ pub use self::unix::infer_shell;
#[cfg(not(unix))]
pub use self::windows::infer_shell;

pub(self) fn shell_from_string(shell: &str) -> Option<Box<dyn super::Shell>> {
use super::{Bash, Fish, PowerShell, WindowsCmd, Zsh};
use super::Shells;

pub(self) fn shell_from_string(shell: &str) -> Option<Shells> {
match shell {
"sh" | "bash" => return Some(Box::from(Bash)),
"zsh" => return Some(Box::from(Zsh)),
"fish" => return Some(Box::from(Fish)),
"pwsh" | "powershell" => return Some(Box::from(PowerShell)),
"cmd" => return Some(Box::from(WindowsCmd)),
"sh" | "bash" => return Some(Shells::Bash),
"zsh" => return Some(Shells::Zsh),
"fish" => return Some(Shells::Fish),
"pwsh" | "powershell" => return Some(Shells::PowerShell),
#[cfg(windows)]
"cmd" => return Some(Shells::Cmd),
cmd_name => log::debug!("binary is not a supported shell: {:?}", cmd_name),
};
None
Expand Down
4 changes: 2 additions & 2 deletions src/shell/infer/unix.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![cfg(unix)]

use crate::shell::Shell;
use crate::shell::Shells;
use log::debug;
use std::io::{Error, ErrorKind};
use thiserror::Error;
Expand All @@ -13,7 +13,7 @@ struct ProcessInfo {

const MAX_ITERATIONS: u8 = 10;

pub fn infer_shell() -> Option<Box<dyn Shell>> {
pub fn infer_shell() -> Option<Shells> {
let mut pid = Some(std::process::id());
let mut visited = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/shell/infer/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::shell::Shell;
use std::ffi::OsStr;
use sysinfo::{ProcessExt, System, SystemExt};

pub fn infer_shell() -> Option<Box<dyn Shell>> {
pub fn infer_shell() -> Option<Shells> {
let mut system = System::new();
let mut current_pid = sysinfo::get_current_pid().ok();

Expand Down
28 changes: 23 additions & 5 deletions src/shell/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ impl Display for Shells {
}
}

impl From<Shells> for Box<dyn Shell> {
fn from(shell: Shells) -> Box<dyn Shell> {
match shell {
impl Shells {
fn actual_shell(&self) -> Box<dyn Shell> {
match self {
Shells::Zsh => Box::from(super::zsh::Zsh),
Shells::Bash => Box::from(super::bash::Bash),
Shells::Fish => Box::from(super::fish::Fish),
Expand All @@ -49,8 +49,26 @@ impl From<Shells> for Box<dyn Shell> {
}
}

impl From<Box<dyn Shell>> for clap_complete::Shell {
fn from(shell: Box<dyn Shell>) -> Self {
impl Shell for Shells {
fn path(&self, path: &Path) -> anyhow::Result<String> {
self.actual_shell().path(path)
}

fn set_env_var(&self, name: &str, value: &str) -> String {
self.actual_shell().set_env_var(name, value)
}

fn use_on_cd(&self, config: &crate::config::FnmConfig) -> anyhow::Result<String> {
self.actual_shell().use_on_cd(config)
}

fn to_clap_shell(&self) -> clap_complete::Shell {
self.actual_shell().to_clap_shell()
}
}

impl From<Shells> for clap_complete::Shell {
fn from(shell: Shells) -> Self {
shell.to_clap_shell()
}
}

0 comments on commit 7781b5d

Please sign in to comment.