Skip to content

Commit f8f1fca

Browse files
committed
refactor: replace macro args with traits
1 parent 2e74a70 commit f8f1fca

1 file changed

Lines changed: 32 additions & 15 deletions

File tree

src/app.rs

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

55
use crate::{
66
args::{Args, Quantity, Threads},
7-
get_size::GetApparentSize,
7+
bytes_format::BytesFormat,
8+
get_size::{GetApparentSize, GetSize},
89
json_data::{JsonData, UnitAndTree},
910
reporter::{ErrorOnlyReporter, ErrorReport, ProgressAndErrorReporter, ProgressReport},
1011
runtime_error::RuntimeError,
11-
size::{self, Bytes},
12+
size,
1213
visualizer::{BarAlignment, Direction, Visualizer},
1314
};
1415
use clap::Parser;
@@ -18,10 +19,7 @@ use std::{io::stdin, time::Duration};
1819
use sysinfo::Disks;
1920

2021
#[cfg(unix)]
21-
use crate::{
22-
get_size::{GetBlockCount, GetBlockSize},
23-
size::Blocks,
24-
};
22+
use crate::get_size::{GetBlockCount, GetBlockSize};
2523

2624
/// The main application.
2725
pub struct App {
@@ -141,11 +139,36 @@ impl App {
141139
)
142140
}
143141

142+
trait GetSizeUtils: GetSize {
143+
type FormatSizeOutput;
144+
fn format_size(bytes_format: BytesFormat) -> Self::FormatSizeOutput;
145+
}
146+
147+
impl GetSizeUtils for GetApparentSize {
148+
type FormatSizeOutput = BytesFormat;
149+
fn format_size(bytes_format: BytesFormat) -> Self::FormatSizeOutput {
150+
bytes_format
151+
}
152+
}
153+
154+
#[cfg(unix)]
155+
impl GetSizeUtils for GetBlockSize {
156+
type FormatSizeOutput = BytesFormat;
157+
fn format_size(bytes_format: BytesFormat) -> Self::FormatSizeOutput {
158+
bytes_format
159+
}
160+
}
161+
162+
#[cfg(unix)]
163+
impl GetSizeUtils for GetBlockCount {
164+
type FormatSizeOutput = ();
165+
fn format_size(_: BytesFormat) -> Self::FormatSizeOutput {}
166+
}
167+
144168
macro_rules! run {
145169
($(
146170
$(#[$variant_attrs:meta])*
147171
{
148-
$size:ty => $format:expr;
149172
$quantity:ident => $size_getter:ident;
150173
$progress:literal => $create_reporter:ident;
151174
}
@@ -167,8 +190,8 @@ impl App {
167190
direction: Direction::from_top_down(top_down),
168191
bar_alignment: BarAlignment::from_align_right(align_right),
169192
size_getter: $size_getter,
170-
reporter: $create_reporter::<$size>(report_error),
171-
bytes_format: $format(bytes_format),
193+
reporter: $create_reporter::<<$size_getter as GetSize>::Size>(report_error),
194+
bytes_format: <$size_getter as GetSizeUtils>::format_size(bytes_format),
172195
files,
173196
json_output,
174197
column_width_distribution,
@@ -182,41 +205,35 @@ impl App {
182205

183206
run! {
184207
{
185-
Bytes => |x| x;
186208
ApparentSize => GetApparentSize;
187209
false => error_only_reporter;
188210
}
189211

190212
{
191-
Bytes => |x| x;
192213
ApparentSize => GetApparentSize;
193214
true => progress_and_error_reporter;
194215
}
195216

196217
#[cfg(unix)]
197218
{
198-
Bytes => |x| x;
199219
BlockSize => GetBlockSize;
200220
false => error_only_reporter;
201221
}
202222

203223
#[cfg(unix)]
204224
{
205-
Bytes => |x| x;
206225
BlockSize => GetBlockSize;
207226
true => progress_and_error_reporter;
208227
}
209228

210229
#[cfg(unix)]
211230
{
212-
Blocks => |_| ();
213231
BlockCount => GetBlockCount;
214232
false => error_only_reporter;
215233
}
216234

217235
#[cfg(unix)]
218236
{
219-
Blocks => |_| ();
220237
BlockCount => GetBlockCount;
221238
true => progress_and_error_reporter;
222239
}

0 commit comments

Comments
 (0)