Skip to content

Commit

Permalink
git: Update serialization for copied files
Browse files Browse the repository at this point in the history
Use the same way of serialization of moved files for copied files
  • Loading branch information
sebastinez committed Apr 25, 2024
1 parent 2e7f12b commit d71b65a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
27 changes: 25 additions & 2 deletions radicle-surf/src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl Serialize for Moved {
S: Serializer,
{
if self.old == self.new {
let mut state = serializer.serialize_struct("Moved", 2)?;
let mut state = serializer.serialize_struct("Moved", 3)?;
state.serialize_field("oldPath", &self.old_path)?;
state.serialize_field("newPath", &self.new_path)?;
state.serialize_field("current", &self.new)?;
Expand All @@ -238,7 +238,6 @@ impl Serialize for Moved {
}

/// A file that was copied within a [`Diff`].
#[cfg_attr(feature = "serde", derive(Serialize), serde(rename_all = "camelCase"))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Copied {
/// The old path to this file, relative to the repository root.
Expand All @@ -250,6 +249,30 @@ pub struct Copied {
pub diff: DiffContent,
}

#[cfg(feature = "serde")]
impl Serialize for Copied {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
if self.old == self.new {
let mut state = serializer.serialize_struct("Copied", 3)?;
state.serialize_field("oldPath", &self.old_path)?;
state.serialize_field("newPath", &self.new_path)?;
state.serialize_field("current", &self.new)?;
state.end()
} else {
let mut state = serializer.serialize_struct("Copied", 5)?;
state.serialize_field("oldPath", &self.old_path)?;
state.serialize_field("newPath", &self.new_path)?;
state.serialize_field("old", &self.old)?;
state.serialize_field("new", &self.new)?;
state.serialize_field("diff", &self.diff)?;
state.end()
}
}
}

#[cfg_attr(feature = "serde", derive(Serialize), serde(rename_all = "camelCase"))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum EofNewLine {
Expand Down
15 changes: 1 addition & 14 deletions radicle-surf/t/src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,24 +303,11 @@ fn test_diff_serde() -> Result<(), Error> {
"state": "moved"
},
{
"diff": {
"eof": "noneMissing",
"hunks": [],
"stats": {
"additions": 0,
"deletions": 0,
},
"type": "plain",
},
"new": {
"current": {
"mode": "blob",
"oid": "5e07534cd74a6a9b2ccd2729b181c4ef26173a5e",
},
"newPath": "file_operations/copied.md",
"old": {
"mode": "blob",
"oid": "5e07534cd74a6a9b2ccd2729b181c4ef26173a5e",
},
"oldPath": "README.md",
"state": "copied"
},
Expand Down

0 comments on commit d71b65a

Please sign in to comment.