Skip to content

Commit

Permalink
Remove feature flags (#9)
Browse files Browse the repository at this point in the history
Closes #6
  • Loading branch information
9999years authored Mar 21, 2024
1 parent e206a65 commit 1c70713
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 40 deletions.
7 changes: 2 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
40 changes: 24 additions & 16 deletions src/command_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
/// ```
Expand All @@ -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;
Expand All @@ -66,7 +82,6 @@ pub struct Utf8ProgramAndArgs {
args: Vec<String>,
}

#[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))?;
Expand All @@ -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<Item = Cow<'_, str>> + '_)> {
Box::new(self.args.iter().map(|arg| Cow::Borrowed(arg.as_str())))
}
Expand Down
3 changes: 0 additions & 3 deletions src/command_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -281,7 +280,6 @@ pub trait CommandExt {
/// },
/// );
/// ```
#[cfg(feature = "utf8-command")]
#[track_caller]
fn output_checked_utf8(&mut self) -> Result<Utf8Output, Self::Error> {
self.output_checked_with_utf8(|output| {
Expand Down Expand Up @@ -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<E>(
&mut self,
Expand Down
10 changes: 6 additions & 4 deletions src/exec_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/output_conversion_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -72,7 +72,7 @@ impl Display for OutputConversionError {
write!(
f,
"Failed to convert `{}` output: {}",
self.command.program(),
self.command.program_quoted(),
self.inner
)
}
Expand Down
10 changes: 1 addition & 9 deletions src/output_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
1 change: 0 additions & 1 deletion src/output_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 1c70713

Please sign in to comment.