Skip to content

Commit 700c0d7

Browse files
committed
refactor!: replace closures with traits
1 parent 12fb123 commit 700c0d7

File tree

11 files changed

+99
-68
lines changed

11 files changed

+99
-68
lines changed

src/app.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ pub use sub::Sub;
44

55
use crate::{
66
args::{Args, Quantity},
7+
get_size::GetApparentSize,
78
json_data::{JsonData, UnitAndTree},
89
reporter::{ErrorOnlyReporter, ErrorReport, ProgressAndErrorReporter, ProgressReport},
910
runtime_error::RuntimeError,
1011
size::{Bytes, Size},
11-
size_getters::GET_APPARENT_SIZE,
1212
visualizer::{BarAlignment, Direction, Visualizer},
1313
};
1414
use clap::Parser;
@@ -17,8 +17,8 @@ use std::{io::stdin, time::Duration};
1717

1818
#[cfg(unix)]
1919
use crate::{
20+
get_size::{GetBlockCount, GetBlockSize},
2021
size::Blocks,
21-
size_getters::{GET_BLOCK_COUNT, GET_BLOCK_SIZE},
2222
};
2323

2424
/// The main application.
@@ -162,41 +162,41 @@ impl App {
162162
run! {
163163
{
164164
Bytes => |x| x;
165-
ApparentSize => GET_APPARENT_SIZE;
165+
ApparentSize => GetApparentSize;
166166
false => error_only_reporter;
167167
}
168168

169169
{
170170
Bytes => |x| x;
171-
ApparentSize => GET_APPARENT_SIZE;
171+
ApparentSize => GetApparentSize;
172172
true => progress_and_error_reporter;
173173
}
174174

175175
#[cfg(unix)]
176176
{
177177
Bytes => |x| x;
178-
BlockSize => GET_BLOCK_SIZE;
178+
BlockSize => GetBlockSize;
179179
false => error_only_reporter;
180180
}
181181

182182
#[cfg(unix)]
183183
{
184184
Bytes => |x| x;
185-
BlockSize => GET_BLOCK_SIZE;
185+
BlockSize => GetBlockSize;
186186
true => progress_and_error_reporter;
187187
}
188188

189189
#[cfg(unix)]
190190
{
191191
Blocks => |_| ();
192-
BlockCount => GET_BLOCK_COUNT;
192+
BlockCount => GetBlockCount;
193193
false => error_only_reporter;
194194
}
195195

196196
#[cfg(unix)]
197197
{
198198
Blocks => |_| ();
199-
BlockCount => GET_BLOCK_COUNT;
199+
BlockCount => GetBlockCount;
200200
true => progress_and_error_reporter;
201201
}
202202
}

src/app/sub.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::{
22
args::Fraction,
33
data_tree::{DataTree, DataTreeReflection},
44
fs_tree_builder::FsTreeBuilder,
5+
get_size::GetSize,
56
json_data::{BinaryVersion, JsonData, SchemaVersion, UnitAndTree},
67
os_string_display::OsStringDisplay,
78
reporter::ParallelReporter,
@@ -11,14 +12,14 @@ use crate::{
1112
visualizer::{BarAlignment, ColumnWidthDistribution, Direction, Visualizer},
1213
};
1314
use serde::Serialize;
14-
use std::{fs::Metadata, io::stdout, iter::once, num::NonZeroUsize, path::PathBuf};
15+
use std::{io::stdout, iter::once, num::NonZeroUsize, path::PathBuf};
1516

1617
/// The sub program of the main application.
1718
pub struct Sub<Data, GetData, Report>
1819
where
1920
Data: Size + Into<u64> + Serialize + Send + Sync,
2021
Report: ParallelReporter<Data> + Sync,
21-
GetData: Fn(&Metadata) -> Data + Copy + Sync,
22+
GetData: GetSize<Size = Data> + Copy + Sync,
2223
DataTreeReflection<String, Data>: Into<UnitAndTree>,
2324
{
2425
/// List of files and/or directories.
@@ -49,7 +50,7 @@ impl<Data, GetData, Report> Sub<Data, GetData, Report>
4950
where
5051
Data: Size + Into<u64> + Serialize + Send + Sync,
5152
Report: ParallelReporter<Data> + Sync,
52-
GetData: Fn(&Metadata) -> Data + Copy + Sync,
53+
GetData: GetSize<Size = Data> + Copy + Sync,
5354
DataTreeReflection<String, Data>: Into<UnitAndTree>,
5455
{
5556
/// Run the sub program.

src/fs_tree_builder.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use super::{
22
data_tree::DataTree,
3+
get_size::GetSize,
34
os_string_display::OsStringDisplay,
45
reporter::{error_report::Operation::*, ErrorReport, Event, Reporter},
56
size::Size,
67
tree_builder::{Info, TreeBuilder},
78
};
89
use pipe_trait::Pipe;
910
use std::{
10-
fs::{read_dir, symlink_metadata, Metadata},
11+
fs::{read_dir, symlink_metadata},
1112
path::PathBuf,
1213
};
1314

@@ -19,14 +20,14 @@ use std::{
1920
/// # use parallel_disk_usage::fs_tree_builder::FsTreeBuilder;
2021
/// use parallel_disk_usage::{
2122
/// data_tree::DataTree,
23+
/// get_size::GetApparentSize,
2224
/// os_string_display::OsStringDisplay,
2325
/// reporter::{ErrorOnlyReporter, ErrorReport},
2426
/// size::Bytes,
25-
/// size_getters::GET_APPARENT_SIZE,
2627
/// };
2728
/// let builder = FsTreeBuilder {
2829
/// root: std::env::current_dir().unwrap(),
29-
/// get_data: GET_APPARENT_SIZE,
30+
/// get_data: GetApparentSize,
3031
/// reporter: ErrorOnlyReporter::new(ErrorReport::SILENT),
3132
/// };
3233
/// let data_tree: DataTree<OsStringDisplay, Bytes> = builder.into();
@@ -35,7 +36,7 @@ use std::{
3536
pub struct FsTreeBuilder<Data, GetData, Report>
3637
where
3738
Data: Size + Send + Sync,
38-
GetData: Fn(&Metadata) -> Data + Sync,
39+
GetData: GetSize<Size = Data> + Sync,
3940
Report: Reporter<Data> + Sync,
4041
{
4142
/// Root of the directory tree.
@@ -50,7 +51,7 @@ impl<Data, GetData, Report> From<FsTreeBuilder<Data, GetData, Report>>
5051
for DataTree<OsStringDisplay, Data>
5152
where
5253
Data: Size + Send + Sync,
53-
GetData: Fn(&Metadata) -> Data + Sync,
54+
GetData: GetSize<Size = Data> + Sync,
5455
Report: Reporter<Data> + Sync,
5556
{
5657
/// Create a [`DataTree`] from an [`FsTreeBuilder`].
@@ -110,7 +111,7 @@ where
110111
Vec::new()
111112
};
112113

113-
let data = get_data(&stats);
114+
let data = get_data.get_size(&stats);
114115
reporter.report(Event::ReceiveData(data));
115116

116117
Info { data, children }

src/get_size.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use super::size::Bytes;
2+
use std::fs::Metadata;
3+
4+
#[cfg(unix)]
5+
use super::size::Blocks;
6+
#[cfg(unix)]
7+
use std::os::unix::prelude::MetadataExt;
8+
9+
/// Infers size from a [`Metadata`].
10+
pub trait GetSize {
11+
type Size;
12+
fn get_size(&self, metadata: &Metadata) -> Self::Size;
13+
}
14+
15+
/// Returns [`metadata.len()`](Metadata::len).
16+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
17+
pub struct GetApparentSize;
18+
impl GetSize for GetApparentSize {
19+
type Size = Bytes;
20+
fn get_size(&self, metadata: &Metadata) -> Self::Size {
21+
metadata.len().into()
22+
}
23+
}
24+
25+
/// Returns [`metadata.blocks() * 512`](Metadata::blksize) (POSIX only).
26+
#[cfg(unix)]
27+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
28+
pub struct GetBlockSize;
29+
#[cfg(unix)]
30+
impl GetSize for GetBlockSize {
31+
type Size = Bytes;
32+
fn get_size(&self, metadata: &Metadata) -> Self::Size {
33+
(metadata.blocks() * 512).into()
34+
}
35+
}
36+
37+
/// Returns [`metadata.blocks()`](Metadata::blocks) (POSIX only).
38+
#[cfg(unix)]
39+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
40+
pub struct GetBlockCount;
41+
#[cfg(unix)]
42+
impl GetSize for GetBlockCount {
43+
type Size = Blocks;
44+
fn get_size(&self, metadata: &Metadata) -> Self::Size {
45+
metadata.blocks().into()
46+
}
47+
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ pub use clap_utilities;
3232
pub mod bytes_format;
3333
pub mod data_tree;
3434
pub mod fs_tree_builder;
35+
pub mod get_size;
3536
pub mod json_data;
3637
pub mod os_string_display;
3738
pub mod reporter;
3839
pub mod size;
39-
pub mod size_getters;
4040
pub mod status_board;
4141
pub mod tree_builder;
4242
pub mod visualizer;

src/size_getters.rs

Lines changed: 0 additions & 18 deletions
This file was deleted.

tests/_utils.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,29 @@ use derive_more::{AsRef, Deref};
44
use parallel_disk_usage::{
55
data_tree::{DataTree, DataTreeReflection},
66
fs_tree_builder::FsTreeBuilder,
7+
get_size::{self, GetSize},
78
os_string_display::OsStringDisplay,
89
reporter::ErrorOnlyReporter,
9-
size::{Bytes, Size},
10-
size_getters::{self, SizeGetter},
10+
size::Size,
1111
};
1212
use pipe_trait::Pipe;
1313
use pretty_assertions::assert_eq;
1414
use rand::{distributions::Alphanumeric, thread_rng, Rng};
1515
use rayon::prelude::*;
1616
use std::{
1717
env::temp_dir,
18-
fs::{create_dir, metadata, remove_dir_all, Metadata},
18+
fs::{create_dir, metadata, remove_dir_all},
1919
io::Error,
2020
path::{Path, PathBuf},
2121
process::{Command, Output},
2222
};
2323

2424
/// Default size getter method.
2525
#[cfg(unix)]
26-
pub const DEFAULT_GET_SIZE: SizeGetter<Bytes> = size_getters::GET_BLOCK_SIZE;
26+
pub const DEFAULT_GET_SIZE: get_size::GetBlockSize = get_size::GetBlockSize;
2727
/// Default size getter method.
2828
#[cfg(not(unix))]
29-
pub const DEFAULT_GET_SIZE: SizeGetter<Bytes> = size_getters::GET_APPARENT_SIZE;
29+
pub const DEFAULT_GET_SIZE: get_size::GetApparentSize = get_size::GetApparentSize;
3030

3131
/// Representation of a temporary filesystem item.
3232
///
@@ -125,17 +125,16 @@ where
125125
}
126126

127127
/// Test the result of tree builder on the sample workspace.
128-
pub fn test_sample_tree<Data, SizeFromMetadata>(root: &Path, size_from_metadata: SizeFromMetadata)
128+
pub fn test_sample_tree<Data, SizeGetter>(root: &Path, size_getter: SizeGetter)
129129
where
130130
Data: Size<Inner = u64> + From<u64> + Send + Sync,
131-
SizeFromMetadata: Fn(&Metadata) -> u64 + Copy + Sync,
131+
SizeGetter: GetSize<Size = Data> + Copy + Sync,
132132
{
133133
let suffix_size = |suffix: &str| -> Data {
134134
root.join(suffix)
135135
.pipe(metadata)
136136
.unwrap_or_else(|error| panic!("get_size {}: {}", suffix, error))
137-
.pipe_ref(size_from_metadata)
138-
.into()
137+
.pipe(|ref metadata| size_getter.get_size(metadata))
139138
};
140139

141140
macro_rules! suffix_size {
@@ -149,7 +148,7 @@ where
149148

150149
let measure = |suffix: &str| {
151150
FsTreeBuilder {
152-
get_data: |metadata| size_from_metadata(metadata).into(),
151+
get_data: size_getter,
153152
reporter: ErrorOnlyReporter::new(|error| {
154153
panic!("Unexpected call to report_error: {:?}", error)
155154
}),

tests/cli_errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use parallel_disk_usage::{
99
bytes_format::BytesFormat,
1010
data_tree::DataTree,
1111
fs_tree_builder::FsTreeBuilder,
12+
get_size::GetApparentSize,
1213
os_string_display::OsStringDisplay,
1314
reporter::{ErrorOnlyReporter, ErrorReport},
14-
size_getters::GET_APPARENT_SIZE,
1515
visualizer::{BarAlignment, ColumnWidthDistribution, Direction, Visualizer},
1616
};
1717
use pipe_trait::Pipe;
@@ -131,7 +131,7 @@ fn fs_errors() {
131131

132132
let builder = FsTreeBuilder {
133133
root: workspace.to_path_buf(),
134-
get_data: GET_APPARENT_SIZE,
134+
get_data: GetApparentSize,
135135
reporter: ErrorOnlyReporter::new(ErrorReport::SILENT),
136136
};
137137
let mut data_tree: DataTree<OsStringDisplay, _> = builder.into();

tests/fs_tree_builder.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
pub mod _utils;
22
pub use _utils::*;
33

4-
use parallel_disk_usage::size::Bytes;
4+
use parallel_disk_usage::{
5+
get_size::{GetApparentSize, GetBlockCount, GetBlockSize},
6+
size::Bytes,
7+
};
58

69
#[cfg(unix)]
710
use parallel_disk_usage::size::Blocks;
8-
#[cfg(unix)]
9-
use std::os::unix::fs::MetadataExt;
1011

1112
#[test]
1213
fn len_as_bytes() {
1314
let workspace = SampleWorkspace::default();
14-
test_sample_tree::<Bytes, _>(&workspace, |metadata| metadata.len());
15+
test_sample_tree::<Bytes, _>(&workspace, GetApparentSize);
1516
}
1617

1718
#[cfg(unix)]
1819
#[test]
1920
fn blocks_as_bytes() {
2021
let workspace = SampleWorkspace::default();
21-
test_sample_tree::<Bytes, _>(&workspace, |metadata| metadata.blocks() * 512);
22+
test_sample_tree::<Bytes, _>(&workspace, GetBlockSize);
2223
}
2324

2425
#[cfg(unix)]
2526
#[test]
2627
fn blocks_as_blocks() {
2728
let workspace = SampleWorkspace::default();
28-
test_sample_tree::<Blocks, _>(&workspace, |metadata| metadata.blocks());
29+
test_sample_tree::<Blocks, _>(&workspace, GetBlockCount);
2930
}

tests/json.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ use parallel_disk_usage::{
99
bytes_format::BytesFormat,
1010
data_tree::{DataTree, Reflection},
1111
fs_tree_builder::FsTreeBuilder,
12+
get_size::GetApparentSize,
1213
json_data::{JsonData, SchemaVersion},
1314
reporter::{ErrorOnlyReporter, ErrorReport},
1415
size::Bytes,
15-
size_getters::GET_APPARENT_SIZE,
1616
visualizer::{BarAlignment, ColumnWidthDistribution, Direction, Visualizer},
1717
};
1818
use pipe_trait::Pipe;
@@ -80,7 +80,7 @@ fn json_output() {
8080
dbg!(&actual);
8181
let builder = FsTreeBuilder {
8282
root: workspace.to_path_buf(),
83-
get_data: GET_APPARENT_SIZE,
83+
get_data: GetApparentSize,
8484
reporter: ErrorOnlyReporter::new(ErrorReport::SILENT),
8585
};
8686
let expected = builder

0 commit comments

Comments
 (0)