Skip to content

Commit

Permalink
surf: Move FileDiff and Diff serialization to container attributes
Browse files Browse the repository at this point in the history
To keep the sorting of diffs that is provided by git we should change
the way we serialize them and avoid putting them into separate
modification specific properties.
  • Loading branch information
sebastinez committed Apr 17, 2024
1 parent a37a40e commit dd6bb6f
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 129 deletions.
56 changes: 6 additions & 50 deletions radicle-surf/src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub mod git;
/// A [`Diff`] can be retrieved by the following functions:
/// * [`crate::Repository::diff`]
/// * [`crate::Repository::diff_commit`]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Diff {
files: Vec<FileDiff>,
Expand Down Expand Up @@ -350,6 +351,11 @@ pub struct DiffFile {
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
feature = "serde",
derive(Serialize),
serde(tag = "state", rename_all = "camelCase")
)]
pub enum FileDiff {
Added(Added),
Deleted(Deleted),
Expand All @@ -370,56 +376,6 @@ impl FileDiff {
}
}

#[cfg(feature = "serde")]
impl Serialize for FileDiff {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut state = serializer.serialize_struct("FileDiff", 7)?;
match &self {
FileDiff::Added(x) => {
state.serialize_field("path", &x.path)?;
state.serialize_field("diff", &x.diff)?;
}
FileDiff::Deleted(x) => {
state.serialize_field("path", &x.path)?;
state.serialize_field("diff", &x.diff)?;
}
FileDiff::Modified(x) => {
state.serialize_field("path", &x.path)?;
state.serialize_field("diff", &x.diff)?;
}
FileDiff::Moved(x) => {
state.serialize_field("oldPath", &x.old_path)?;
state.serialize_field("newPath", &x.new_path)?;
}
FileDiff::Copied(x) => {
state.serialize_field("oldPath", &x.old_path)?;
state.serialize_field("newPath", &x.new_path)?;
}
}
state.end()
}
}

#[cfg(feature = "serde")]
impl Serialize for Diff {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut state = serializer.serialize_struct("Diff", 6)?;
state.serialize_field("added", &self.added().collect::<Vec<_>>())?;
state.serialize_field("deleted", &self.deleted().collect::<Vec<_>>())?;
state.serialize_field("moved", &self.moved().collect::<Vec<_>>())?;
state.serialize_field("copied", &self.copied().collect::<Vec<_>>())?;
state.serialize_field("modified", &self.modified().collect::<Vec<_>>())?;
state.serialize_field("stats", &self.stats())?;
state.end()
}
}

/// Statistics describing a particular [`FileDiff`].
#[cfg_attr(feature = "serde", derive(Serialize), serde(rename_all = "camelCase"))]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
Expand Down
160 changes: 81 additions & 79 deletions radicle-surf/t/src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ fn test_diff_serde() -> Result<(), Error> {
let diff = repo.diff(rev_from, rev_to)?;

let json = serde_json::json!({
"added": [{
"files": [{
"path": "LICENSE",
"diff": {
"type": "plain",
Expand Down Expand Up @@ -248,9 +248,85 @@ fn test_diff_serde() -> Result<(), Error> {
"mode": "blob",
"oid": "02f70f56ec62396ceaf38804c37e169e875ab291",
},
}],
"deleted": [{
"state": "added"
},
{
"path": "README.md",
"diff": {
"type": "plain",
"hunks": [{
"header": "@@ -1,2 +1,2 @@\n",
"lines": [
{ "lineNo": 1,
"line": "This repository is a data source for the Upstream front-end tests and the\n",
"type": "deletion"
},
{ "lineNo": 2,
"line": "[`radicle-surf`](https://github.com/radicle-dev/git-platinum) unit tests.\n",
"type": "deletion"
},
{ "lineNo": 1,
"line": "This repository is a data source for the upstream front-end tests and the\n",
"type": "addition"
},
{ "lineNo": 2,
"line": "[`radicle-surf`](https://github.com/radicle-dev/radicle-surf) unit tests.\n",
"type": "addition"
},
],
"old": { "start": 1, "end": 3 },
"new": { "start": 1, "end": 3 },
}],
"stats": {
"additions": 2,
"deletions": 2
},
"eof": "noneMissing",
},
"new": {
"mode": "blob",
"oid": "b033ecf407a44922b28c942c696922a7d7daf06e",
},
"old": {
"mode": "blob",
"oid": "5e07534cd74a6a9b2ccd2729b181c4ef26173a5e",
},
"state": "modified",
},
{
"current": {
"mode": "blob",
"oid": "1570277532948712fea9029d100a4208f9e34241",
},
"oldPath": "text/emoji.txt",
"newPath": "emoji.txt",
"state": "moved"
},
{
"diff": {
"eof": "noneMissing",
"hunks": [],
"stats": {
"additions": 0,
"deletions": 0,
},
"type": "plain",
},
"new": {
"mode": "blob",
"oid": "5e07534cd74a6a9b2ccd2729b181c4ef26173a5e",
},
"newPath": "file_operations/copied.md",
"old": {
"mode": "blob",
"oid": "5e07534cd74a6a9b2ccd2729b181c4ef26173a5e",
},
"oldPath": "README.md",
"state": "copied"
},
{
"path": "text/arrows.txt",
"state": "deleted",
"old": {
"mode": "blob",
"oid": "95418c04010a3cc758fb3a37f9918465f147566f",
Expand Down Expand Up @@ -306,77 +382,6 @@ fn test_diff_serde() -> Result<(), Error> {
"eof": "noneMissing",
},
}],
"moved": [{
"current": {
"mode": "blob",
"oid": "1570277532948712fea9029d100a4208f9e34241",
},
"oldPath": "text/emoji.txt",
"newPath": "emoji.txt",
}],
"copied": [{
"diff": {
"eof": "noneMissing",
"hunks": [],
"stats": {
"additions": 0,
"deletions": 0,
},
"type": "plain",
},
"new": {
"mode": "blob",
"oid": "5e07534cd74a6a9b2ccd2729b181c4ef26173a5e",
},
"newPath": "file_operations/copied.md",
"old": {
"mode": "blob",
"oid": "5e07534cd74a6a9b2ccd2729b181c4ef26173a5e",
},
"oldPath": "README.md",
}],
"modified": [{
"path": "README.md",
"diff": {
"type": "plain",
"hunks": [{
"header": "@@ -1,2 +1,2 @@\n",
"lines": [
{ "lineNo": 1,
"line": "This repository is a data source for the Upstream front-end tests and the\n",
"type": "deletion"
},
{ "lineNo": 2,
"line": "[`radicle-surf`](https://github.com/radicle-dev/git-platinum) unit tests.\n",
"type": "deletion"
},
{ "lineNo": 1,
"line": "This repository is a data source for the upstream front-end tests and the\n",
"type": "addition"
},
{ "lineNo": 2,
"line": "[`radicle-surf`](https://github.com/radicle-dev/radicle-surf) unit tests.\n",
"type": "addition"
},
],
"old": { "start": 1, "end": 3 },
"new": { "start": 1, "end": 3 },
}],
"stats": {
"additions": 2,
"deletions": 2
},
"eof": "noneMissing",
},
"new": {
"mode": "blob",
"oid": "b033ecf407a44922b28c942c696922a7d7daf06e",
},
"old": {
"mode": "blob",
"oid": "5e07534cd74a6a9b2ccd2729b181c4ef26173a5e",
},
}],
"stats": {
"deletions": 9,
"filesChanged": 5,
Expand Down Expand Up @@ -426,11 +431,7 @@ index 3f69208f3..cbc843c82 100644
let diff = Diff::try_from(diff).unwrap();
let json = serde_json::json!(
{
"added": [],
"copied": [],
"deleted": [],
"modified": [],
"moved": [
"files": [
{
"diff": {
"eof": "noneMissing",
Expand Down Expand Up @@ -585,6 +586,7 @@ index 3f69208f3..cbc843c82 100644
"oid": "3f69208f30000000000000000000000000000000",
},
"oldPath": "radicle/src/node/tracking/config.rs",
"state": "moved"
},
],
"stats": {
Expand Down

0 comments on commit dd6bb6f

Please sign in to comment.