From 7acfab695aa7f721fa94fb0964926375f000de06 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Sat, 6 Apr 2024 13:21:16 +0900 Subject: [PATCH] matchers: impl custom Debug for RepoPathTree to get stable and concise output The default Debug output entries aren't sorted by name, which was inconvenient while writing snapshot tests. --- lib/src/matchers.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/src/matchers.rs b/lib/src/matchers.rs index 24bfcfde1..41fe9c1e0 100644 --- a/lib/src/matchers.rs +++ b/lib/src/matchers.rs @@ -16,8 +16,9 @@ use std::collections::{HashMap, HashSet}; use std::fmt::Debug; -use std::iter; +use std::{fmt, iter}; +use itertools::Itertools as _; use tracing::instrument; use crate::repo_path::{RepoPath, RepoPathComponentBuf}; @@ -339,7 +340,7 @@ impl Matcher for IntersectionMatcher { /// Keeps track of which subdirectories and files of each directory need to be /// visited. -#[derive(PartialEq, Eq, Debug)] +#[derive(PartialEq, Eq)] struct RepoPathTree { entries: HashMap, // is_dir/is_file aren't exclusive, both can be set to true. If entries is not empty, @@ -415,6 +416,25 @@ impl RepoPathTree { } } +impl Debug for RepoPathTree { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let type_name = match (self.is_dir, self.is_file) { + (true, true) => "Dir|File", + (true, false) => "Dir", + (false, true) => "File", + (false, false) => "_", + }; + write!(f, "{type_name} ")?; + f.debug_map() + .entries( + self.entries + .iter() + .sorted_unstable_by_key(|&(name, _)| name), + ) + .finish() + } +} + #[cfg(test)] mod tests { use maplit::hashset;