cli: Define UnifiedDiff on concrete type

Simplify the trait by defining it on the concrete type, which appears to
be the standard rust approach.  Remove some oddities in the process (refs
of refs).
This commit is contained in:
Slack Coder 2023-08-07 10:37:28 -05:00 committed by cloudhead
parent 5078396028
commit b337e1b1cb
No known key found for this signature in database
1 changed files with 9 additions and 9 deletions

View File

@ -85,10 +85,10 @@ pub struct HunkHeader {
} }
/// A Trait for converting a value to its UnifiedDiff format. /// A Trait for converting a value to its UnifiedDiff format.
pub trait UnifiedDiff: Clone { pub trait UnifiedDiff: Sized {
fn encode(&self, w: &mut Writer) -> io::Result<()>; fn encode(&self, w: &mut Writer) -> io::Result<()>;
fn to_unified_string(self) -> String { fn to_unified_string(&self) -> String {
let mut buf = Vec::new(); let mut buf = Vec::new();
{ {
@ -100,7 +100,7 @@ pub trait UnifiedDiff: Clone {
} }
} }
impl UnifiedDiff for &Diff { impl UnifiedDiff for Diff {
fn encode(&self, w: &mut Writer) -> io::Result<()> { fn encode(&self, w: &mut Writer) -> io::Result<()> {
for fdiff in self.files() { for fdiff in self.files() {
fdiff.encode(w)?; fdiff.encode(w)?;
@ -110,7 +110,7 @@ impl UnifiedDiff for &Diff {
} }
} }
impl UnifiedDiff for &DiffContent { impl UnifiedDiff for DiffContent {
fn encode(&self, w: &mut Writer) -> io::Result<()> { fn encode(&self, w: &mut Writer) -> io::Result<()> {
match self { match self {
DiffContent::Plain { hunks, .. } => { DiffContent::Plain { hunks, .. } => {
@ -151,7 +151,7 @@ impl UnifiedDiff for FileDiff {
} }
} }
impl UnifiedDiff for &FileHeader { impl UnifiedDiff for FileHeader {
fn encode(&self, w: &mut Writer) -> io::Result<()> { fn encode(&self, w: &mut Writer) -> io::Result<()> {
match self { match self {
FileHeader::Modified { path, old, new } => { FileHeader::Modified { path, old, new } => {
@ -234,7 +234,7 @@ impl UnifiedDiff for &FileHeader {
} }
} }
impl UnifiedDiff for &HunkHeader { impl UnifiedDiff for HunkHeader {
fn encode(&self, w: &mut Writer) -> io::Result<()> { fn encode(&self, w: &mut Writer) -> io::Result<()> {
let old = if self.old_size == 1 { let old = if self.old_size == 1 {
format!("{}", self.old_line_no) format!("{}", self.old_line_no)
@ -256,7 +256,7 @@ impl UnifiedDiff for &HunkHeader {
} }
} }
impl UnifiedDiff for &Hunk<Modification> { impl UnifiedDiff for Hunk<Modification> {
fn encode(&self, w: &mut Writer) -> io::Result<()> { fn encode(&self, w: &mut Writer) -> io::Result<()> {
// TODO: Remove trailing newlines accurately. // TODO: Remove trailing newlines accurately.
// trim_end() will destroy diff information if the diff has a trailing whitespace on // trim_end() will destroy diff information if the diff has a trailing whitespace on
@ -270,7 +270,7 @@ impl UnifiedDiff for &Hunk<Modification> {
} }
} }
impl UnifiedDiff for &Modification { impl UnifiedDiff for Modification {
fn encode(&self, w: &mut Writer) -> io::Result<()> { fn encode(&self, w: &mut Writer) -> io::Result<()> {
match self { match self {
Modification::Deletion(radicle_surf::diff::Deletion { line, .. }) => { Modification::Deletion(radicle_surf::diff::Deletion { line, .. }) => {
@ -303,7 +303,7 @@ impl<'a> Writer<'a> {
} }
} }
pub fn encode(&mut self, arg: impl UnifiedDiff) -> io::Result<()> { pub fn encode(&mut self, arg: &impl UnifiedDiff) -> io::Result<()> {
arg.encode(self) arg.encode(self)
} }