From fcfe98fe5865e2a57144204c0ff51374a0a78ba7 Mon Sep 17 00:00:00 2001 From: squidfunk Date: Wed, 9 Apr 2025 21:24:17 +0200 Subject: [PATCH] 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 --- crates/globset/src/glob.rs | 17 ++++++++++++++++- crates/globset/src/lib.rs | 12 ++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/crates/globset/src/glob.rs b/crates/globset/src/glob.rs index fdd6d16c..8cc7645f 100644 --- a/crates/globset/src/glob.rs +++ b/crates/globset/src/glob.rs @@ -71,7 +71,7 @@ impl MatchStrategy { /// /// It cannot be used directly to match file paths, but it can be converted /// to a regular expression string or a matcher. -#[derive(Clone, Debug, Eq)] +#[derive(Clone, Eq)] #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] pub struct Glob { 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 { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.glob.fmt(f) diff --git a/crates/globset/src/lib.rs b/crates/globset/src/lib.rs index 42e6d0ba..623d7f9d 100644 --- a/crates/globset/src/lib.rs +++ b/crates/globset/src/lib.rs @@ -1100,4 +1100,16 @@ mod tests { let matches = set.matches("nada"); 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*\")] }", + ); + } }