Skip to content

Commit 1c70713

Browse files
authored
Remove feature flags (#9)
Closes #6
1 parent e206a65 commit 1c70713

File tree

7 files changed

+35
-40
lines changed

7 files changed

+35
-40
lines changed

Cargo.toml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,9 @@ push = false # Don't do `git push`.
2424
publish = false # Don't do `cargo publish`.
2525

2626
[dependencies]
27-
shell-words = { version = "1", optional = true }
27+
shell-words = "1"
2828
tracing = { version = "0", optional = true }
29-
utf8-command = { version = "1", optional = true }
30-
31-
[features]
32-
default = ["utf8-command", "shell-words", "tracing"]
29+
utf8-command = "1"
3330

3431
[dev-dependencies]
3532
indoc = "2.0.4"

src/command_display.rs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ pub trait CommandDisplay: Display {
2424
/// );
2525
/// ```
2626
fn program(&self) -> Cow<'_, str>;
27+
28+
/// The command's program name, shell-quoted.
29+
///
30+
/// ```
31+
/// # use std::process::Command;
32+
/// # use command_error::Utf8ProgramAndArgs;
33+
/// # use command_error::CommandDisplay;
34+
/// let command = Command::new("ooga booga");
35+
/// let displayed: Utf8ProgramAndArgs = (&command).into();
36+
/// assert_eq!(
37+
/// displayed.program_quoted(),
38+
/// "'ooga booga'",
39+
/// );
40+
/// ```
41+
fn program_quoted(&self) -> Cow<'_, str> {
42+
Cow::Owned(shell_words::quote(&self.program()).into_owned())
43+
}
44+
2745
/// The command's arguments, decoded as UTF-8.
2846
///
2947
/// ```
@@ -43,10 +61,8 @@ pub trait CommandDisplay: Display {
4361

4462
/// A program name and arguments stored as UTF-8 [`String`]s.
4563
///
46-
/// By default (with the `shell-words` feature enabled), the program name and arguments are
47-
/// shell-quoted when [`Display`]ed, so that spaces are escaped and the displayed command can
48-
/// generally be pasted directly into a shell. Otherwise, the program and arguments are formatted
49-
/// with [`Debug`].
64+
/// The program name and arguments are shell-quoted when [`Display`]ed, so that spaces are escaped
65+
/// and the displayed command can generally be pasted directly into a shell.
5066
///
5167
/// ```
5268
/// # use std::process::Command;
@@ -66,7 +82,6 @@ pub struct Utf8ProgramAndArgs {
6682
args: Vec<String>,
6783
}
6884

69-
#[cfg(feature = "shell-words")]
7085
impl Display for Utf8ProgramAndArgs {
7186
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
7287
write!(f, "{}", shell_words::quote(&self.program))?;
@@ -77,22 +92,15 @@ impl Display for Utf8ProgramAndArgs {
7792
}
7893
}
7994

80-
#[cfg(not(feature = "shell-words"))]
81-
impl Display for Utf8ProgramAndArgs {
82-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83-
write!(f, "{:?}", &self.program)?;
84-
for arg in &self.args {
85-
write!(f, " {arg:?}")?;
86-
}
87-
Ok(())
88-
}
89-
}
90-
9195
impl CommandDisplay for Utf8ProgramAndArgs {
9296
fn program(&self) -> std::borrow::Cow<'_, str> {
9397
Cow::Borrowed(&self.program)
9498
}
9599

100+
fn program_quoted(&self) -> Cow<'_, str> {
101+
shell_words::quote(&self.program)
102+
}
103+
96104
fn args(&self) -> Box<(dyn Iterator<Item = Cow<'_, str>> + '_)> {
97105
Box::new(self.args.iter().map(|arg| Cow::Borrowed(arg.as_str())))
98106
}

src/command_ext.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::fmt::Display;
33
use std::process::ExitStatus;
44
use std::process::{Command, Output};
55

6-
#[cfg(feature = "utf8-command")]
76
use utf8_command::Utf8Output;
87

98
use crate::CommandDisplay;
@@ -281,7 +280,6 @@ pub trait CommandExt {
281280
/// },
282281
/// );
283282
/// ```
284-
#[cfg(feature = "utf8-command")]
285283
#[track_caller]
286284
fn output_checked_utf8(&mut self) -> Result<Utf8Output, Self::Error> {
287285
self.output_checked_with_utf8(|output| {
@@ -319,7 +317,6 @@ pub trait CommandExt {
319317
/// assert_eq!(output.stdout, "puppy\n");
320318
/// assert_eq!(output.status.code(), Some(1));
321319
/// ```
322-
#[cfg(feature = "utf8-command")]
323320
#[track_caller]
324321
fn output_checked_with_utf8<E>(
325322
&mut self,

src/exec_error.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,14 @@ impl Debug for ExecError {
5656

5757
impl Display for ExecError {
5858
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59-
let program = self.command.program();
60-
#[cfg(feature = "shell-words")]
61-
let program = shell_words::quote(&program);
6259
// TODO: Should this contain an additional message like
6360
// "Is `program` installed and present in your `$PATH`?"
64-
write!(f, "Failed to execute `{program}`: {}", self.inner)
61+
write!(
62+
f,
63+
"Failed to execute `{}`: {}",
64+
self.command.program_quoted(),
65+
self.inner
66+
)
6567
}
6668
}
6769

src/output_conversion_error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::process::Command;
66
#[cfg(doc)]
77
use std::process::Output;
88

9-
#[cfg(all(doc, feature = "utf8-command"))]
9+
#[cfg(doc)]
1010
use utf8_command::Utf8Output;
1111

1212
use crate::CommandDisplay;
@@ -72,7 +72,7 @@ impl Display for OutputConversionError {
7272
write!(
7373
f,
7474
"Failed to convert `{}` output: {}",
75-
self.command.program(),
75+
self.command.program_quoted(),
7676
self.inner
7777
)
7878
}

src/output_error.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,7 @@ impl Debug for OutputError {
102102

103103
impl Display for OutputError {
104104
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
105-
#[cfg(feature = "shell-words")]
106-
write!(
107-
f,
108-
"`{}` failed: ",
109-
shell_words::quote(&self.command.program())
110-
)?;
111-
112-
#[cfg(not(feature = "shell-words"))]
113-
write!(f, "`{}` failed: ", self.command.program())?;
105+
write!(f, "`{}` failed: ", self.command.program_quoted())?;
114106

115107
match &self.user_error {
116108
Some(user_error) => {

src/output_like.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::borrow::Cow;
22
use std::process::ExitStatus;
33
use std::process::Output;
44

5-
#[cfg(feature = "utf8-command")]
65
use utf8_command::Utf8Output;
76

87
/// A command output type.

0 commit comments

Comments
 (0)