bin: Make hunk items debuggable

This commit is contained in:
Erik Kundt
2024-12-17 14:30:43 +01:00
parent 17683048a5
commit 537e8be681
2 changed files with 38 additions and 2 deletions
+26 -1
View File
@@ -1,3 +1,5 @@
use std::fmt;
use std::fmt::Debug;
use std::path::Path;
use std::{fs, path::PathBuf};
@@ -222,7 +224,7 @@ pub enum HunkState {
/// A single review item. Can be a hunk or eg. a file move.
/// Files are usually split into multiple review items.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, PartialEq)]
pub enum HunkDiff {
Added {
path: PathBuf,
@@ -327,6 +329,29 @@ impl HunkDiff {
}
}
impl Debug for HunkDiff {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (name, path, hunk) = match self {
Self::Added { path, hunk, .. } => ("Added", path, hunk),
Self::Deleted { path, hunk, .. } => ("Deleted", path, hunk),
Self::Moved { moved } => ("Moved", &moved.new_path, &None),
Self::Copied { copied } => ("Copied", &copied.new_path, &None),
Self::Modified { path, hunk, .. } => ("Modified", path, hunk),
Self::EofChanged { path, .. } => ("EofChanged", path, &None),
Self::ModeChanged { path, .. } => ("ModeChanged", path, &None),
};
match hunk {
Some(hunk) => f
.debug_struct(name)
.field("path", path)
.field("hunk", &(hunk.old.clone(), hunk.new.clone()))
.finish(),
_ => f.debug_struct(name).field("path", path).finish(),
}
}
}
#[derive(Clone, Debug)]
pub struct StatefulHunkDiff(HunkDiff, HunkState);
+12 -1
View File
@@ -1,4 +1,6 @@
use std::collections::HashMap;
use std::fmt;
use std::fmt::Debug;
use std::str::FromStr;
use nom::bytes::complete::{tag, take};
@@ -1090,7 +1092,7 @@ impl From<Vec<(EntryId, Comment<CodeLocation>)>> for HunkComments {
/// A [`HunkItem`] that can be rendered. Hunk items are indexed sequentially and
/// provide access to the underlying hunk type.
#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct HunkItem<'a> {
/// The underlying hunk type and its current state (accepted / rejected).
pub inner: StatefulHunkDiff,
@@ -1580,6 +1582,15 @@ impl<'a> HunkItem<'a> {
}
}
impl<'a> Debug for HunkItem<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("HunkItem")
.field("inner", &self.inner)
.field("comments", &self.comments)
.finish()
}
}
pub struct HighlightedLine<'a>(Line<'a>);
impl<'a> From<Line<'a>> for HighlightedLine<'a> {