merge: make non-conflicted debug string for Merge shorter

Resolves states are most common and the current format is pretty
verbose. Let's print it as if `Merge` were an enum with `Resolved` and
`Conflicted` variants instead.
This commit is contained in:
Martin von Zweigbergk 2023-10-23 22:15:29 -07:00 committed by Martin von Zweigbergk
parent 831dc84c5b
commit 21b11df8a9
2 changed files with 36 additions and 35 deletions

View file

@ -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<T> {
removes: Vec<T>,
adds: Vec<T>,
}
impl<T: Debug> Debug for Merge<T> {
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<T> Merge<T> {
/// Creates a new merge object from the given removes and adds.
pub fn new(removes: Vec<T>, adds: Vec<T>) -> Self {

View file

@ -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",
),
],
)
"###