Skip to content

Commit 7f076f6

Browse files
zertoshalexcrichton
authored andcommitted
Implement Debug for Patch and related diff structs
1 parent 1772541 commit 7f076f6

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/diff.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,17 @@ impl<'a> Binding for DiffDelta<'a> {
444444
}
445445
}
446446

447+
impl<'a> std::fmt::Debug for DiffDelta<'a> {
448+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
449+
f.debug_struct("DiffDelta")
450+
.field("nfiles", &self.nfiles())
451+
.field("status", &self.status())
452+
.field("old_file", &self.old_file())
453+
.field("new_file", &self.new_file())
454+
.finish()
455+
}
456+
}
457+
447458
impl<'a> DiffFile<'a> {
448459
/// Returns the Oid of this item.
449460
///
@@ -487,6 +498,20 @@ impl<'a> Binding for DiffFile<'a> {
487498
}
488499
}
489500

501+
impl<'a> std::fmt::Debug for DiffFile<'a> {
502+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
503+
let mut ds = f.debug_struct("DiffFile");
504+
ds.field("id", &self.id());
505+
if let Some(path_bytes) = &self.path_bytes() {
506+
ds.field("path_bytes", path_bytes);
507+
}
508+
if let Some(path) = &self.path() {
509+
ds.field("path", path);
510+
}
511+
ds.field("size", &self.size()).finish()
512+
}
513+
}
514+
490515
impl Default for DiffOptions {
491516
fn default() -> Self {
492517
Self::new()
@@ -863,6 +888,23 @@ impl<'a> Binding for DiffLine<'a> {
863888
}
864889
}
865890

891+
impl<'a> std::fmt::Debug for DiffLine<'a> {
892+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
893+
let mut ds = f.debug_struct("DiffLine");
894+
if let Some(old_lineno) = &self.old_lineno() {
895+
ds.field("old_lineno", old_lineno);
896+
}
897+
if let Some(new_lineno) = &self.new_lineno() {
898+
ds.field("new_lineno", new_lineno);
899+
}
900+
ds.field("num_lines", &self.num_lines())
901+
.field("content_offset", &self.content_offset())
902+
.field("content", &self.content())
903+
.field("origin", &self.origin())
904+
.finish()
905+
}
906+
}
907+
866908
impl<'a> DiffHunk<'a> {
867909
/// Starting line number in old_file
868910
pub fn old_start(&self) -> u32 {
@@ -908,6 +950,18 @@ impl<'a> Binding for DiffHunk<'a> {
908950
}
909951
}
910952

953+
impl<'a> std::fmt::Debug for DiffHunk<'a> {
954+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
955+
f.debug_struct("DiffHunk")
956+
.field("old_start", &self.old_start())
957+
.field("old_lines", &self.old_lines())
958+
.field("new_start", &self.new_start())
959+
.field("new_lines", &self.new_lines())
960+
.field("header", &self.header())
961+
.finish()
962+
}
963+
}
964+
911965
impl DiffStats {
912966
/// Get the total number of files chaned in a diff.
913967
pub fn files_changed(&self) -> usize {
@@ -956,6 +1010,16 @@ impl Drop for DiffStats {
9561010
}
9571011
}
9581012

1013+
impl std::fmt::Debug for DiffStats {
1014+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
1015+
f.debug_struct("DiffStats")
1016+
.field("files_changed", &self.files_changed())
1017+
.field("insertions", &self.insertions())
1018+
.field("deletions", &self.deletions())
1019+
.finish()
1020+
}
1021+
}
1022+
9591023
impl<'a> DiffBinary<'a> {
9601024
/// Returns whether there is data in this binary structure or not.
9611025
///

src/patch.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,15 @@ impl Patch {
214214
Ok(buf)
215215
}
216216
}
217+
218+
impl std::fmt::Debug for Patch {
219+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
220+
let mut ds = f.debug_struct("Patch");
221+
ds.field("delta", &self.delta())
222+
.field("num_hunks", &self.num_hunks());
223+
if let Ok(line_stats) = &self.line_stats() {
224+
ds.field("line_stats", line_stats);
225+
}
226+
ds.finish()
227+
}
228+
}

0 commit comments

Comments
 (0)