From 1c70713a56c04846b04c826914b9b3c45a2382cc Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Thu, 21 Mar 2024 18:47:40 -0400 Subject: [PATCH] Remove feature flags (#9) Closes #6 --- Cargo.toml | 7 ++---- src/command_display.rs | 40 ++++++++++++++++++++-------------- src/command_ext.rs | 3 --- src/exec_error.rs | 10 +++++---- src/output_conversion_error.rs | 4 ++-- src/output_error.rs | 10 +-------- src/output_like.rs | 1 - 7 files changed, 35 insertions(+), 40 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 53e8dde..56cf321 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,12 +24,9 @@ push = false # Don't do `git push`. publish = false # Don't do `cargo publish`. [dependencies] -shell-words = { version = "1", optional = true } +shell-words = "1" tracing = { version = "0", optional = true } -utf8-command = { version = "1", optional = true } - -[features] -default = ["utf8-command", "shell-words", "tracing"] +utf8-command = "1" [dev-dependencies] indoc = "2.0.4" diff --git a/src/command_display.rs b/src/command_display.rs index 10914c1..41a610e 100644 --- a/src/command_display.rs +++ b/src/command_display.rs @@ -24,6 +24,24 @@ pub trait CommandDisplay: Display { /// ); /// ``` fn program(&self) -> Cow<'_, str>; + + /// The command's program name, shell-quoted. + /// + /// ``` + /// # use std::process::Command; + /// # use command_error::Utf8ProgramAndArgs; + /// # use command_error::CommandDisplay; + /// let command = Command::new("ooga booga"); + /// let displayed: Utf8ProgramAndArgs = (&command).into(); + /// assert_eq!( + /// displayed.program_quoted(), + /// "'ooga booga'", + /// ); + /// ``` + fn program_quoted(&self) -> Cow<'_, str> { + Cow::Owned(shell_words::quote(&self.program()).into_owned()) + } + /// The command's arguments, decoded as UTF-8. /// /// ``` @@ -43,10 +61,8 @@ pub trait CommandDisplay: Display { /// A program name and arguments stored as UTF-8 [`String`]s. /// -/// By default (with the `shell-words` feature enabled), the program name and arguments are -/// shell-quoted when [`Display`]ed, so that spaces are escaped and the displayed command can -/// generally be pasted directly into a shell. Otherwise, the program and arguments are formatted -/// with [`Debug`]. +/// The program name and arguments are shell-quoted when [`Display`]ed, so that spaces are escaped +/// and the displayed command can generally be pasted directly into a shell. /// /// ``` /// # use std::process::Command; @@ -66,7 +82,6 @@ pub struct Utf8ProgramAndArgs { args: Vec, } -#[cfg(feature = "shell-words")] impl Display for Utf8ProgramAndArgs { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", shell_words::quote(&self.program))?; @@ -77,22 +92,15 @@ impl Display for Utf8ProgramAndArgs { } } -#[cfg(not(feature = "shell-words"))] -impl Display for Utf8ProgramAndArgs { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{:?}", &self.program)?; - for arg in &self.args { - write!(f, " {arg:?}")?; - } - Ok(()) - } -} - impl CommandDisplay for Utf8ProgramAndArgs { fn program(&self) -> std::borrow::Cow<'_, str> { Cow::Borrowed(&self.program) } + fn program_quoted(&self) -> Cow<'_, str> { + shell_words::quote(&self.program) + } + fn args(&self) -> Box<(dyn Iterator> + '_)> { Box::new(self.args.iter().map(|arg| Cow::Borrowed(arg.as_str()))) } diff --git a/src/command_ext.rs b/src/command_ext.rs index c915980..f2479e9 100644 --- a/src/command_ext.rs +++ b/src/command_ext.rs @@ -3,7 +3,6 @@ use std::fmt::Display; use std::process::ExitStatus; use std::process::{Command, Output}; -#[cfg(feature = "utf8-command")] use utf8_command::Utf8Output; use crate::CommandDisplay; @@ -281,7 +280,6 @@ pub trait CommandExt { /// }, /// ); /// ``` - #[cfg(feature = "utf8-command")] #[track_caller] fn output_checked_utf8(&mut self) -> Result { self.output_checked_with_utf8(|output| { @@ -319,7 +317,6 @@ pub trait CommandExt { /// assert_eq!(output.stdout, "puppy\n"); /// assert_eq!(output.status.code(), Some(1)); /// ``` - #[cfg(feature = "utf8-command")] #[track_caller] fn output_checked_with_utf8( &mut self, diff --git a/src/exec_error.rs b/src/exec_error.rs index ae8d80d..0e80105 100644 --- a/src/exec_error.rs +++ b/src/exec_error.rs @@ -56,12 +56,14 @@ impl Debug for ExecError { impl Display for ExecError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let program = self.command.program(); - #[cfg(feature = "shell-words")] - let program = shell_words::quote(&program); // TODO: Should this contain an additional message like // "Is `program` installed and present in your `$PATH`?" - write!(f, "Failed to execute `{program}`: {}", self.inner) + write!( + f, + "Failed to execute `{}`: {}", + self.command.program_quoted(), + self.inner + ) } } diff --git a/src/output_conversion_error.rs b/src/output_conversion_error.rs index e275aa2..4c0245d 100644 --- a/src/output_conversion_error.rs +++ b/src/output_conversion_error.rs @@ -6,7 +6,7 @@ use std::process::Command; #[cfg(doc)] use std::process::Output; -#[cfg(all(doc, feature = "utf8-command"))] +#[cfg(doc)] use utf8_command::Utf8Output; use crate::CommandDisplay; @@ -72,7 +72,7 @@ impl Display for OutputConversionError { write!( f, "Failed to convert `{}` output: {}", - self.command.program(), + self.command.program_quoted(), self.inner ) } diff --git a/src/output_error.rs b/src/output_error.rs index 36f790b..31556fb 100644 --- a/src/output_error.rs +++ b/src/output_error.rs @@ -102,15 +102,7 @@ impl Debug for OutputError { impl Display for OutputError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - #[cfg(feature = "shell-words")] - write!( - f, - "`{}` failed: ", - shell_words::quote(&self.command.program()) - )?; - - #[cfg(not(feature = "shell-words"))] - write!(f, "`{}` failed: ", self.command.program())?; + write!(f, "`{}` failed: ", self.command.program_quoted())?; match &self.user_error { Some(user_error) => { diff --git a/src/output_like.rs b/src/output_like.rs index 8903ab3..8608d7c 100644 --- a/src/output_like.rs +++ b/src/output_like.rs @@ -2,7 +2,6 @@ use std::borrow::Cow; use std::process::ExitStatus; use std::process::Output; -#[cfg(feature = "utf8-command")] use utf8_command::Utf8Output; /// A command output type.