Skip to content

Commit eb16ae4

Browse files
committed
refactor(builder): Consolidate escaping with Escape
1 parent 806d981 commit eb16ae4

File tree

5 files changed

+39
-23
lines changed

5 files changed

+39
-23
lines changed

clap_builder/src/builder/possible_value.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use crate::builder::IntoResettable;
22
use crate::builder::Str;
33
use crate::builder::StyledStr;
44
use crate::util::eq_ignore_case;
5+
#[cfg(feature = "help")]
6+
use crate::util::Escape;
57

68
/// A possible value of an argument.
79
///
@@ -186,11 +188,7 @@ impl PossibleValue {
186188
#[cfg(feature = "help")]
187189
pub(crate) fn get_visible_quoted_name(&self) -> Option<std::borrow::Cow<'_, str>> {
188190
if !self.hide {
189-
Some(if self.name.contains(char::is_whitespace) {
190-
format!("{:?}", self.name).into()
191-
} else {
192-
self.name.as_str().into()
193-
})
191+
Some(Escape(self.name.as_str()).to_cow())
194192
} else {
195193
None
196194
}

clap_builder/src/error/format.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::error::ContextKind;
1414
use crate::error::ContextValue;
1515
use crate::error::ErrorKind;
1616
use crate::output::TAB;
17+
use crate::util::Escape;
1718
use crate::ArgAction;
1819

1920
/// Defines how to format an error for displaying to the user
@@ -491,15 +492,3 @@ fn did_you_mean(styled: &mut StyledStr, styles: &Styles, context: &str, possible
491492
}
492493
}
493494
}
494-
495-
struct Escape<'s>(&'s str);
496-
497-
impl std::fmt::Display for Escape<'_> {
498-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
499-
if self.0.contains(char::is_whitespace) {
500-
std::fmt::Debug::fmt(self.0, f)
501-
} else {
502-
self.0.fmt(f)
503-
}
504-
}
505-
}

clap_builder/src/output/help_template.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::output::wrap;
1919
use crate::output::Usage;
2020
use crate::output::TAB;
2121
use crate::output::TAB_WIDTH;
22+
use crate::util::Escape;
2223
use crate::util::FlatSet;
2324

2425
/// `clap` auto-generated help writer
@@ -795,12 +796,9 @@ impl HelpTemplate<'_, '_> {
795796
.default_vals
796797
.iter()
797798
.map(|dv| dv.to_string_lossy())
798-
.map(|dv| {
799-
if dv.contains(char::is_whitespace) {
800-
Cow::from(format!("{dv:?}"))
801-
} else {
802-
dv
803-
}
799+
.map(|dv| match Escape(dv.as_ref()).to_cow() {
800+
Cow::Borrowed(_) => dv,
801+
Cow::Owned(escaped) => Cow::Owned(escaped),
804802
})
805803
.collect::<Vec<_>>()
806804
.join(" ");

clap_builder/src/util/escape.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#[cfg(feature = "help")]
2+
use std::borrow::Cow;
3+
4+
pub(crate) struct Escape<'s>(pub(crate) &'s str);
5+
6+
impl<'s> Escape<'s> {
7+
pub(crate) fn needs_escaping(&self) -> bool {
8+
self.0.contains(char::is_whitespace)
9+
}
10+
11+
#[cfg(feature = "help")]
12+
pub(crate) fn to_cow(&self) -> Cow<'s, str> {
13+
if self.needs_escaping() {
14+
Cow::Owned(format!("{:?}", self.0))
15+
} else {
16+
Cow::Borrowed(self.0)
17+
}
18+
}
19+
}
20+
21+
impl std::fmt::Display for Escape<'_> {
22+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23+
if self.needs_escaping() {
24+
std::fmt::Debug::fmt(self.0, f)
25+
} else {
26+
self.0.fmt(f)
27+
}
28+
}
29+
}

clap_builder/src/util/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![allow(clippy::single_component_path_imports)]
22

33
mod any_value;
4+
mod escape;
45
pub(crate) mod flat_map;
56
pub(crate) mod flat_set;
67
mod graph;
@@ -11,6 +12,7 @@ pub use self::id::Id;
1112

1213
pub(crate) use self::any_value::AnyValue;
1314
pub(crate) use self::any_value::AnyValueId;
15+
pub(crate) use self::escape::Escape;
1416
pub(crate) use self::flat_map::Entry;
1517
pub(crate) use self::flat_map::FlatMap;
1618
pub(crate) use self::flat_set::FlatSet;

0 commit comments

Comments
 (0)