Skip to content

Commit 74412cd

Browse files
committed
feat(api): immutable setters for Summary
1 parent 9c9387e commit 74412cd

2 files changed

Lines changed: 35 additions & 30 deletions

File tree

src/hardlink/hardlink_list/summary.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::{iter::Item as IterItem, reflection::ReflectionEntry, HardlinkList, Reflection};
22
use crate::size;
33
use derive_more::{Add, AddAssign, Sum};
4+
use derive_setters::Setters;
45
use std::{
56
cmp::Ordering,
67
fmt::{self, Display},
@@ -10,8 +11,9 @@ use std::{
1011
use serde::{Deserialize, Serialize};
1112

1213
/// Summary from [`HardlinkList`] or [`Reflection`].
13-
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Add, AddAssign, Sum)]
14+
#[derive(Debug, Default, Setters, Clone, Copy, PartialEq, Eq, Add, AddAssign, Sum)]
1415
#[cfg_attr(feature = "json", derive(Deserialize, Serialize))]
16+
#[setters(prefix = "with_")]
1517
#[non_exhaustive]
1618
pub struct Summary<Size> {
1719
/// Number of shared inodes, each with more than 1 links (i.e. `nlink > 1`).

tests/hardlinks_deduplication.rs

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,15 @@ fn multiple_hardlinks_to_a_single_file() {
114114
assert_eq!(actual_shared_details, expected_shared_details);
115115

116116
let actual_shared_summary = tree.shared.summary;
117-
let expected_shared_summary = {
118-
let mut summary = Summary::default();
119-
summary.inodes = 1;
120-
summary.exclusive_inodes = 1;
121-
summary.all_links = 1 + links;
122-
summary.detected_links = 1 + links as usize;
123-
summary.exclusive_links = 1 + links as usize;
124-
summary.shared_size = file_size;
125-
summary.exclusive_shared_size = file_size;
126-
Some(summary)
127-
};
117+
let expected_shared_summary = Summary::default()
118+
.with_inodes(1)
119+
.with_exclusive_inodes(1)
120+
.with_all_links(1 + links)
121+
.with_detected_links(1 + links as usize)
122+
.with_exclusive_links(1 + links as usize)
123+
.with_shared_size(file_size)
124+
.with_exclusive_shared_size(file_size)
125+
.pipe(Some);
128126
assert_eq!(actual_shared_summary, expected_shared_summary);
129127
}
130128

@@ -310,8 +308,7 @@ fn complex_tree_with_shared_and_unique_files() {
310308
// of reasoning.
311309
// It should still produce the same result as the proper
312310
// deduplication formula however.
313-
let mut summary = Summary::default();
314-
summary.inodes = [
311+
let inodes = [
315312
0, // no-hardlinks/*
316313
2 * files_per_branch / 8 + files_per_branch / 2, // some-hardlinks/*
317314
files_per_branch, // only-hardlinks/exclusive/*
@@ -320,8 +317,7 @@ fn complex_tree_with_shared_and_unique_files() {
320317
]
321318
.into_iter()
322319
.sum();
323-
summary.exclusive_inodes = summary.inodes;
324-
summary.all_links = [
320+
let all_links = [
325321
0, // no-hardlinks/*
326322
3 * files_per_branch / 8 + 2 * files_per_branch / 8 + 2 * files_per_branch / 2, // some-hardlinks/*
327323
2 * files_per_branch, // only-hardlinks/exclusive/*
@@ -330,11 +326,16 @@ fn complex_tree_with_shared_and_unique_files() {
330326
]
331327
.into_iter()
332328
.sum::<usize>() as u64;
333-
summary.detected_links = summary.all_links as usize;
334-
summary.exclusive_links = summary.all_links as usize;
335-
summary.shared_size = file_size * summary.inodes;
336-
summary.exclusive_shared_size = summary.shared_size;
337-
Some(summary)
329+
let shared_size = file_size * inodes;
330+
Summary::default()
331+
.with_inodes(inodes)
332+
.with_exclusive_inodes(inodes)
333+
.with_all_links(all_links)
334+
.with_detected_links(all_links as usize)
335+
.with_exclusive_links(all_links as usize)
336+
.with_shared_size(shared_size)
337+
.with_exclusive_shared_size(shared_size)
338+
.pipe(Some)
338339
};
339340
assert_eq!(actual_shared_summary, expected_shared_summary);
340341
}
@@ -448,15 +449,17 @@ fn hardlinks_and_non_hardlinks() {
448449

449450
let actual_shared_summary = tree.shared.summary;
450451
let expected_shared_summary = {
451-
let mut summary = Summary::default();
452-
summary.inodes = expected_shared_details.len();
453-
summary.exclusive_inodes = 2;
454-
summary.all_links = 3 + 2 + 4 * 2;
455-
summary.detected_links = 3 + 2 + 4 * 1;
456-
summary.exclusive_links = 3 + 2;
457-
summary.shared_size = summary.inodes * file_size;
458-
summary.exclusive_shared_size = summary.exclusive_inodes * file_size;
459-
Some(summary)
452+
let inodes = expected_shared_details.len();
453+
let exclusive_inodes = 2;
454+
Summary::default()
455+
.with_inodes(inodes)
456+
.with_exclusive_inodes(exclusive_inodes)
457+
.with_all_links(3 + 2 + 4 * 2)
458+
.with_detected_links(3 + 2 + 4 * 1)
459+
.with_exclusive_links(3 + 2)
460+
.with_shared_size(inodes * file_size)
461+
.with_exclusive_shared_size(exclusive_inodes * file_size)
462+
.pipe(Some)
460463
};
461464
assert_eq!(actual_shared_summary, expected_shared_summary);
462465
assert_eq!(actual_shared_summary.unwrap().inodes, files_per_branch - 2);

0 commit comments

Comments
 (0)