1
0
mirror of https://github.com/BurntSushi/ripgrep.git synced 2025-08-04 21:52:54 +02:00

globset: compact Debug impl for GlobSetBuilder and Glob

Ideally we'd have a compact impl for `GlobSet` too, but that's a lot
more work. In particular, the constituent types don't all store the
original pattern string, so that would need to be added.

Closes #3026
This commit is contained in:
squidfunk
2025-04-09 21:24:17 +02:00
committed by Andrew Gallant
parent 0434b5034d
commit fcfe98fe58
2 changed files with 28 additions and 1 deletions

View File

@ -71,7 +71,7 @@ impl MatchStrategy {
/// ///
/// It cannot be used directly to match file paths, but it can be converted /// It cannot be used directly to match file paths, but it can be converted
/// to a regular expression string or a matcher. /// to a regular expression string or a matcher.
#[derive(Clone, Debug, Eq)] #[derive(Clone, Eq)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct Glob { pub struct Glob {
glob: String, glob: String,
@ -93,6 +93,21 @@ impl std::hash::Hash for Glob {
} }
} }
impl std::fmt::Debug for Glob {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if f.alternate() {
f.debug_struct("Glob")
.field("glob", &self.glob)
.field("re", &self.re)
.field("opts", &self.opts)
.field("tokens", &self.tokens)
.finish()
} else {
f.debug_tuple("Glob").field(&self.glob).finish()
}
}
}
impl std::fmt::Display for Glob { impl std::fmt::Display for Glob {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.glob.fmt(f) self.glob.fmt(f)

View File

@ -1100,4 +1100,16 @@ mod tests {
let matches = set.matches("nada"); let matches = set.matches("nada");
assert_eq!(0, matches.len()); assert_eq!(0, matches.len());
} }
#[test]
fn debug() {
let mut builder = GlobSetBuilder::new();
builder.add(Glob::new("*foo*").unwrap());
builder.add(Glob::new("*bar*").unwrap());
builder.add(Glob::new("*quux*").unwrap());
assert_eq!(
format!("{builder:?}"),
"GlobSetBuilder { pats: [Glob(\"*foo*\"), Glob(\"*bar*\"), Glob(\"*quux*\")] }",
);
}
} }