clap_builder/util/escape.rs
1#[cfg(feature = "help")]
2use std::borrow::Cow;
3
4pub(crate) struct Escape<'s>(pub(crate) &'s str);
5
6impl<'s> Escape<'s> {
7 pub(crate) fn needs_escaping(&self) -> bool {
8 self.0.is_empty() || 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
21impl 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}