diff --git a/lib/src/merge.rs b/lib/src/merge.rs index 334427cf4..7c77ffe53 100644 --- a/lib/src/merge.rs +++ b/lib/src/merge.rs @@ -17,6 +17,7 @@ use std::borrow::Borrow; use std::collections::HashMap; +use std::fmt::{Debug, Formatter}; use std::hash::Hash; use std::io::Write; use std::iter::zip; @@ -104,12 +105,27 @@ where /// There is exactly one more `adds()` than `removes()`. When interpreted as a /// series of diffs, the merge's (i+1)-st add is matched with the i-th /// remove. The zeroth add is considered a diff from the non-existent state. -#[derive(PartialEq, Eq, Hash, Clone, Debug)] +#[derive(PartialEq, Eq, Hash, Clone)] pub struct Merge { removes: Vec, adds: Vec, } +impl Debug for Merge { + fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { + // Format like an enum with two variants to make it less verbose in the common + // case of a resolved state. + if self.removes.is_empty() { + f.debug_tuple("Resolved").field(&self.adds[0]).finish() + } else { + f.debug_struct("Conflicted") + .field("removes", &self.removes) + .field("adds", &self.adds) + .finish() + } + } +} + impl Merge { /// Creates a new merge object from the given removes and adds. pub fn new(removes: Vec, adds: Vec) -> Self { diff --git a/lib/tests/test_conflicts.rs b/lib/tests/test_conflicts.rs index 352ff3d6b..1bf36332d 100644 --- a/lib/tests/test_conflicts.rs +++ b/lib/tests/test_conflicts.rs @@ -304,7 +304,7 @@ line 5 right @r###" Some( [ - Merge { + Conflicted { removes: [ "line 1\nline 2\n", ], @@ -313,13 +313,10 @@ line 5 right "line 1 right\nline 2\n", ], }, - Merge { - removes: [], - adds: [ - "line 3\n", - ], - }, - Merge { + Resolved( + "line 3\n", + ), + Conflicted { removes: [ "line 4\nline 5\n", ], @@ -464,13 +461,10 @@ line 5 @r###" Some( [ - Merge { - removes: [], - adds: [ - "line 1\n", - ], - }, - Merge { + Resolved( + "line 1\n", + ), + Conflicted { removes: [ "line 2\nline 3\nline 4\n", ], @@ -479,12 +473,9 @@ line 5 "right\n", ], }, - Merge { - removes: [], - adds: [ - "line 5\n", - ], - }, + Resolved( + "line 5\n", + ), ], ) "### @@ -517,13 +508,10 @@ line 5 @r###" Some( [ - Merge { - removes: [], - adds: [ - "line 1\n", - ], - }, - Merge { + Resolved( + "line 1\n", + ), + Conflicted { removes: [ "line 2\nline 3\nline 4\n", "line 2\nline 3\nline 4\n", @@ -534,12 +522,9 @@ line 5 "line 2\nforward\nline 3\nline 4\n", ], }, - Merge { - removes: [], - adds: [ - "line 5\n", - ], - }, + Resolved( + "line 5\n", + ), ], ) "###