Skip to content

Commit

Permalink
Revert "Use Shells instead of Box<dyn Shell> everywhere"
Browse files Browse the repository at this point in the history
This reverts commit 7781b5d.
  • Loading branch information
amitdahan committed Jul 3, 2023
1 parent 7781b5d commit e37dd32
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 42 deletions.
13 changes: 7 additions & 6 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;
use crate::shell::{infer_shell, Shell};
use crate::{cli::Cli, shell::Shells};
use clap::{CommandFactory, Parser, ValueEnum};
use clap_complete::{Generator, Shell};
use clap_complete::{Generator, Shell as ClapShell};
use thiserror::Error;

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

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

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

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

use super::Shells;

pub(self) fn shell_from_string(shell: &str) -> Option<Shells> {
pub(self) fn shell_from_string(shell: &str) -> Option<Box<dyn super::Shell>> {
use super::{Bash, Fish, PowerShell, WindowsCmd, Zsh};
match shell {
"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),
"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)),
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::Shells;
use crate::shell::Shell;
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<Shells> {
pub fn infer_shell() -> Option<Box<dyn Shell>> {
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<Shells> {
pub fn infer_shell() -> Option<Box<dyn Shell>> {
let mut system = System::new();
let mut current_pid = sysinfo::get_current_pid().ok();

Expand Down
28 changes: 5 additions & 23 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 Shells {
fn actual_shell(&self) -> Box<dyn Shell> {
match self {
impl From<Shells> for Box<dyn Shell> {
fn from(shell: Shells) -> Box<dyn Shell> {
match shell {
Shells::Zsh => Box::from(super::zsh::Zsh),
Shells::Bash => Box::from(super::bash::Bash),
Shells::Fish => Box::from(super::fish::Fish),
Expand All @@ -49,26 +49,8 @@ impl Shells {
}
}

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 {
impl From<Box<dyn Shell>> for clap_complete::Shell {
fn from(shell: Box<dyn Shell>) -> Self {
shell.to_clap_shell()
}
}

0 comments on commit e37dd32

Please sign in to comment.