From be4f18fe5db59bd9280414e0b6648e7c6ef7d815 Mon Sep 17 00:00:00 2001 From: simonsan <14062932+simonsan@users.noreply.github.com> Date: Sun, 6 Oct 2024 22:08:20 +0200 Subject: [PATCH] fix(errors): handle out of bounds access in PathList display (#313) Fixes #309 --------- Signed-off-by: simonsan <14062932+simonsan@users.noreply.github.com> Co-authored-by: Alexander Weiss --- crates/core/src/repofile/snapshotfile.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/crates/core/src/repofile/snapshotfile.rs b/crates/core/src/repofile/snapshotfile.rs index 5141aa54..a973bfce 100644 --- a/crates/core/src/repofile/snapshotfile.rs +++ b/crates/core/src/repofile/snapshotfile.rs @@ -1193,13 +1193,11 @@ pub struct PathList(Vec); impl Display for PathList { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - if !self.0.is_empty() { - write!(f, "{:?}", self.0[0])?; - } - for p in &self.0[1..] { - write!(f, ",{p:?}")?; - } - Ok(()) + self.0 + .iter() + .map(|p| p.to_string_lossy()) + .format(",") + .fmt(f) } } @@ -1368,4 +1366,14 @@ mod tests { assert_eq!(crit.paths, is_path); assert_eq!(crit.tags, is_tags); } + + #[rstest] + #[case(vec![], "")] + #[case(vec!["test"], "test")] + #[case(vec!["test", "test", "test"], "test,test,test")] + fn test_display_path_list_passes(#[case] input: Vec<&str>, #[case] expected: &str) { + let path_list = PathList::from_iter(input); + let result = path_list.to_string(); + assert_eq!(expected, &result); + } }