Skip to content

Commit dd6bb6f

Browse files
committed
surf: Move FileDiff and Diff serialization to container attributes
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.
1 parent a37a40e commit dd6bb6f

File tree

2 files changed

+87
-129
lines changed

2 files changed

+87
-129
lines changed

radicle-surf/src/diff.rs

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub mod git;
3636
/// A [`Diff`] can be retrieved by the following functions:
3737
/// * [`crate::Repository::diff`]
3838
/// * [`crate::Repository::diff_commit`]
39+
#[cfg_attr(feature = "serde", derive(Serialize))]
3940
#[derive(Clone, Debug, Default, PartialEq, Eq)]
4041
pub struct Diff {
4142
files: Vec<FileDiff>,
@@ -350,6 +351,11 @@ pub struct DiffFile {
350351
}
351352

352353
#[derive(Clone, Debug, PartialEq, Eq)]
354+
#[cfg_attr(
355+
feature = "serde",
356+
derive(Serialize),
357+
serde(tag = "state", rename_all = "camelCase")
358+
)]
353359
pub enum FileDiff {
354360
Added(Added),
355361
Deleted(Deleted),
@@ -370,56 +376,6 @@ impl FileDiff {
370376
}
371377
}
372378

373-
#[cfg(feature = "serde")]
374-
impl Serialize for FileDiff {
375-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
376-
where
377-
S: Serializer,
378-
{
379-
let mut state = serializer.serialize_struct("FileDiff", 7)?;
380-
match &self {
381-
FileDiff::Added(x) => {
382-
state.serialize_field("path", &x.path)?;
383-
state.serialize_field("diff", &x.diff)?;
384-
}
385-
FileDiff::Deleted(x) => {
386-
state.serialize_field("path", &x.path)?;
387-
state.serialize_field("diff", &x.diff)?;
388-
}
389-
FileDiff::Modified(x) => {
390-
state.serialize_field("path", &x.path)?;
391-
state.serialize_field("diff", &x.diff)?;
392-
}
393-
FileDiff::Moved(x) => {
394-
state.serialize_field("oldPath", &x.old_path)?;
395-
state.serialize_field("newPath", &x.new_path)?;
396-
}
397-
FileDiff::Copied(x) => {
398-
state.serialize_field("oldPath", &x.old_path)?;
399-
state.serialize_field("newPath", &x.new_path)?;
400-
}
401-
}
402-
state.end()
403-
}
404-
}
405-
406-
#[cfg(feature = "serde")]
407-
impl Serialize for Diff {
408-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
409-
where
410-
S: Serializer,
411-
{
412-
let mut state = serializer.serialize_struct("Diff", 6)?;
413-
state.serialize_field("added", &self.added().collect::<Vec<_>>())?;
414-
state.serialize_field("deleted", &self.deleted().collect::<Vec<_>>())?;
415-
state.serialize_field("moved", &self.moved().collect::<Vec<_>>())?;
416-
state.serialize_field("copied", &self.copied().collect::<Vec<_>>())?;
417-
state.serialize_field("modified", &self.modified().collect::<Vec<_>>())?;
418-
state.serialize_field("stats", &self.stats())?;
419-
state.end()
420-
}
421-
}
422-
423379
/// Statistics describing a particular [`FileDiff`].
424380
#[cfg_attr(feature = "serde", derive(Serialize), serde(rename_all = "camelCase"))]
425381
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]

radicle-surf/t/src/diff.rs

Lines changed: 81 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ fn test_diff_serde() -> Result<(), Error> {
219219
let diff = repo.diff(rev_from, rev_to)?;
220220

221221
let json = serde_json::json!({
222-
"added": [{
222+
"files": [{
223223
"path": "LICENSE",
224224
"diff": {
225225
"type": "plain",
@@ -248,9 +248,85 @@ fn test_diff_serde() -> Result<(), Error> {
248248
"mode": "blob",
249249
"oid": "02f70f56ec62396ceaf38804c37e169e875ab291",
250250
},
251-
}],
252-
"deleted": [{
251+
"state": "added"
252+
},
253+
{
254+
"path": "README.md",
255+
"diff": {
256+
"type": "plain",
257+
"hunks": [{
258+
"header": "@@ -1,2 +1,2 @@\n",
259+
"lines": [
260+
{ "lineNo": 1,
261+
"line": "This repository is a data source for the Upstream front-end tests and the\n",
262+
"type": "deletion"
263+
},
264+
{ "lineNo": 2,
265+
"line": "[`radicle-surf`](https://github.com/radicle-dev/git-platinum) unit tests.\n",
266+
"type": "deletion"
267+
},
268+
{ "lineNo": 1,
269+
"line": "This repository is a data source for the upstream front-end tests and the\n",
270+
"type": "addition"
271+
},
272+
{ "lineNo": 2,
273+
"line": "[`radicle-surf`](https://github.com/radicle-dev/radicle-surf) unit tests.\n",
274+
"type": "addition"
275+
},
276+
],
277+
"old": { "start": 1, "end": 3 },
278+
"new": { "start": 1, "end": 3 },
279+
}],
280+
"stats": {
281+
"additions": 2,
282+
"deletions": 2
283+
},
284+
"eof": "noneMissing",
285+
},
286+
"new": {
287+
"mode": "blob",
288+
"oid": "b033ecf407a44922b28c942c696922a7d7daf06e",
289+
},
290+
"old": {
291+
"mode": "blob",
292+
"oid": "5e07534cd74a6a9b2ccd2729b181c4ef26173a5e",
293+
},
294+
"state": "modified",
295+
},
296+
{
297+
"current": {
298+
"mode": "blob",
299+
"oid": "1570277532948712fea9029d100a4208f9e34241",
300+
},
301+
"oldPath": "text/emoji.txt",
302+
"newPath": "emoji.txt",
303+
"state": "moved"
304+
},
305+
{
306+
"diff": {
307+
"eof": "noneMissing",
308+
"hunks": [],
309+
"stats": {
310+
"additions": 0,
311+
"deletions": 0,
312+
},
313+
"type": "plain",
314+
},
315+
"new": {
316+
"mode": "blob",
317+
"oid": "5e07534cd74a6a9b2ccd2729b181c4ef26173a5e",
318+
},
319+
"newPath": "file_operations/copied.md",
320+
"old": {
321+
"mode": "blob",
322+
"oid": "5e07534cd74a6a9b2ccd2729b181c4ef26173a5e",
323+
},
324+
"oldPath": "README.md",
325+
"state": "copied"
326+
},
327+
{
253328
"path": "text/arrows.txt",
329+
"state": "deleted",
254330
"old": {
255331
"mode": "blob",
256332
"oid": "95418c04010a3cc758fb3a37f9918465f147566f",
@@ -306,77 +382,6 @@ fn test_diff_serde() -> Result<(), Error> {
306382
"eof": "noneMissing",
307383
},
308384
}],
309-
"moved": [{
310-
"current": {
311-
"mode": "blob",
312-
"oid": "1570277532948712fea9029d100a4208f9e34241",
313-
},
314-
"oldPath": "text/emoji.txt",
315-
"newPath": "emoji.txt",
316-
}],
317-
"copied": [{
318-
"diff": {
319-
"eof": "noneMissing",
320-
"hunks": [],
321-
"stats": {
322-
"additions": 0,
323-
"deletions": 0,
324-
},
325-
"type": "plain",
326-
},
327-
"new": {
328-
"mode": "blob",
329-
"oid": "5e07534cd74a6a9b2ccd2729b181c4ef26173a5e",
330-
},
331-
"newPath": "file_operations/copied.md",
332-
"old": {
333-
"mode": "blob",
334-
"oid": "5e07534cd74a6a9b2ccd2729b181c4ef26173a5e",
335-
},
336-
"oldPath": "README.md",
337-
}],
338-
"modified": [{
339-
"path": "README.md",
340-
"diff": {
341-
"type": "plain",
342-
"hunks": [{
343-
"header": "@@ -1,2 +1,2 @@\n",
344-
"lines": [
345-
{ "lineNo": 1,
346-
"line": "This repository is a data source for the Upstream front-end tests and the\n",
347-
"type": "deletion"
348-
},
349-
{ "lineNo": 2,
350-
"line": "[`radicle-surf`](https://github.com/radicle-dev/git-platinum) unit tests.\n",
351-
"type": "deletion"
352-
},
353-
{ "lineNo": 1,
354-
"line": "This repository is a data source for the upstream front-end tests and the\n",
355-
"type": "addition"
356-
},
357-
{ "lineNo": 2,
358-
"line": "[`radicle-surf`](https://github.com/radicle-dev/radicle-surf) unit tests.\n",
359-
"type": "addition"
360-
},
361-
],
362-
"old": { "start": 1, "end": 3 },
363-
"new": { "start": 1, "end": 3 },
364-
}],
365-
"stats": {
366-
"additions": 2,
367-
"deletions": 2
368-
},
369-
"eof": "noneMissing",
370-
},
371-
"new": {
372-
"mode": "blob",
373-
"oid": "b033ecf407a44922b28c942c696922a7d7daf06e",
374-
},
375-
"old": {
376-
"mode": "blob",
377-
"oid": "5e07534cd74a6a9b2ccd2729b181c4ef26173a5e",
378-
},
379-
}],
380385
"stats": {
381386
"deletions": 9,
382387
"filesChanged": 5,
@@ -426,11 +431,7 @@ index 3f69208f3..cbc843c82 100644
426431
let diff = Diff::try_from(diff).unwrap();
427432
let json = serde_json::json!(
428433
{
429-
"added": [],
430-
"copied": [],
431-
"deleted": [],
432-
"modified": [],
433-
"moved": [
434+
"files": [
434435
{
435436
"diff": {
436437
"eof": "noneMissing",
@@ -585,6 +586,7 @@ index 3f69208f3..cbc843c82 100644
585586
"oid": "3f69208f30000000000000000000000000000000",
586587
},
587588
"oldPath": "radicle/src/node/tracking/config.rs",
589+
"state": "moved"
588590
},
589591
],
590592
"stats": {

0 commit comments

Comments
 (0)