Skip to content

Commit cc5af27

Browse files
authored
refactor: replace macro args with traits (#289)
* refactor: replace macro args with traits * refactor: shorten the macro calls * refactor: infer reporter via utility traits * refactor: remove unnecessary trait bound * refactor: remove `FormatSizeOutput` * refactor: use better function name * refactor: remove `$size_getter` * refactor: rename a trait * refactor: merge `GetSizeUtils` into `QuantityUtils` * fix: add missing `#[cfg(unix)]` * refactor: simplify call-site syntax of `CreateReporter` * refactor: move alias closer to its use-site * refactor: create an alias
1 parent 2e74a70 commit cc5af27

1 file changed

Lines changed: 92 additions & 70 deletions

File tree

src/app.rs

Lines changed: 92 additions & 70 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 {
@@ -119,40 +117,97 @@ impl App {
119117
ErrorReport::TEXT
120118
};
121119

122-
#[allow(clippy::extra_unused_type_parameters)]
123-
fn error_only_reporter<Size>(
124-
report_error: fn(ErrorReport),
125-
) -> ErrorOnlyReporter<fn(ErrorReport)> {
126-
ErrorOnlyReporter::new(report_error)
120+
// we can't use `Quantity` directly as `const` parameter so we have to use numbers.
121+
mod quantity_index {
122+
pub const APPARENT_SIZE: u8 = 0;
123+
#[cfg(unix)]
124+
pub const BLOCK_SIZE: u8 = 1;
125+
#[cfg(unix)]
126+
pub const BLOCK_COUNT: u8 = 2;
127+
}
128+
129+
type SizeGetterToDisplayFormat<SizeGetter> =
130+
<<SizeGetter as GetSize>::Size as size::Size>::DisplayFormat;
131+
132+
trait QuantityUtils<const INDEX: u8> {
133+
const QUANTITY: Quantity;
134+
type SizeGetter: GetSize<Size: size::Size>;
135+
const SIZE_GETTER: Self::SizeGetter;
136+
fn formatter(bytes_format: BytesFormat) -> SizeGetterToDisplayFormat<Self::SizeGetter>;
137+
}
138+
139+
impl QuantityUtils<{ quantity_index::APPARENT_SIZE }> for () {
140+
const QUANTITY: Quantity = Quantity::ApparentSize;
141+
type SizeGetter = GetApparentSize;
142+
const SIZE_GETTER: Self::SizeGetter = GetApparentSize;
143+
fn formatter(bytes_format: BytesFormat) -> BytesFormat {
144+
bytes_format
145+
}
146+
}
147+
148+
#[cfg(unix)]
149+
impl QuantityUtils<{ quantity_index::BLOCK_SIZE }> for () {
150+
const QUANTITY: Quantity = Quantity::BlockSize;
151+
type SizeGetter = GetBlockSize;
152+
const SIZE_GETTER: Self::SizeGetter = GetBlockSize;
153+
fn formatter(bytes_format: BytesFormat) -> BytesFormat {
154+
bytes_format
155+
}
156+
}
157+
158+
#[cfg(unix)]
159+
impl QuantityUtils<{ quantity_index::BLOCK_COUNT }> for () {
160+
const QUANTITY: Quantity = Quantity::BlockCount;
161+
type SizeGetter = GetBlockCount;
162+
const SIZE_GETTER: Self::SizeGetter = GetBlockCount;
163+
fn formatter(_: BytesFormat) {}
164+
}
165+
166+
trait CreateReporter<const REPORT_PROGRESS: bool, const QUANTITY_INDEX: u8> {
167+
type Reporter;
168+
fn create_reporter(report_error: fn(ErrorReport)) -> Self::Reporter;
169+
}
170+
171+
type QuantityIndexToSizeType<const INDEX: u8> =
172+
<<() as QuantityUtils<INDEX>>::SizeGetter as GetSize>::Size;
173+
174+
impl<const QUANTITY_INDEX: u8> CreateReporter<false, QUANTITY_INDEX> for ()
175+
where
176+
(): QuantityUtils<QUANTITY_INDEX>,
177+
QuantityIndexToSizeType<QUANTITY_INDEX>: size::Size,
178+
{
179+
type Reporter = ErrorOnlyReporter<fn(ErrorReport)>;
180+
fn create_reporter(report_error: fn(ErrorReport)) -> Self::Reporter {
181+
ErrorOnlyReporter::new(report_error)
182+
}
127183
}
128184

129-
fn progress_and_error_reporter<Size>(
130-
report_error: fn(ErrorReport),
131-
) -> ProgressAndErrorReporter<Size, fn(ErrorReport)>
185+
impl<const QUANTITY_INDEX: u8> CreateReporter<true, QUANTITY_INDEX> for ()
132186
where
133-
Size: size::Size + Into<u64> + Send + Sync,
134-
ProgressReport<Size>: Default + 'static,
135-
u64: Into<Size>,
187+
(): QuantityUtils<QUANTITY_INDEX>,
188+
QuantityIndexToSizeType<QUANTITY_INDEX>: size::Size + Into<u64> + Send + Sync,
189+
ProgressReport<QuantityIndexToSizeType<QUANTITY_INDEX>>: Default + 'static,
190+
u64: Into<QuantityIndexToSizeType<QUANTITY_INDEX>>,
136191
{
137-
ProgressAndErrorReporter::new(
138-
ProgressReport::TEXT,
139-
Duration::from_millis(100),
140-
report_error,
141-
)
192+
type Reporter =
193+
ProgressAndErrorReporter<QuantityIndexToSizeType<QUANTITY_INDEX>, fn(ErrorReport)>;
194+
fn create_reporter(report_error: fn(ErrorReport)) -> Self::Reporter {
195+
ProgressAndErrorReporter::new(
196+
ProgressReport::TEXT,
197+
Duration::from_millis(100),
198+
report_error,
199+
)
200+
}
142201
}
143202

144203
macro_rules! run {
145204
($(
146205
$(#[$variant_attrs:meta])*
147-
{
148-
$size:ty => $format:expr;
149-
$quantity:ident => $size_getter:ident;
150-
$progress:literal => $create_reporter:ident;
151-
}
206+
$quantity_index:ident, $progress:literal;
152207
)*) => { match self.args {$(
153208
$(#[$variant_attrs])*
154209
Args {
155-
quantity: Quantity::$quantity,
210+
quantity: <() as QuantityUtils<{ quantity_index::$quantity_index }>>::QUANTITY,
156211
progress: $progress,
157212
files,
158213
json_output,
@@ -166,9 +221,9 @@ impl App {
166221
} => Sub {
167222
direction: Direction::from_top_down(top_down),
168223
bar_alignment: BarAlignment::from_align_right(align_right),
169-
size_getter: $size_getter,
170-
reporter: $create_reporter::<$size>(report_error),
171-
bytes_format: $format(bytes_format),
224+
size_getter: <() as QuantityUtils<{ quantity_index::$quantity_index }>>::SIZE_GETTER,
225+
reporter: <() as CreateReporter<$progress, { quantity_index::$quantity_index }>>::create_reporter(report_error),
226+
bytes_format: <() as QuantityUtils<{ quantity_index::$quantity_index }>>::formatter(bytes_format),
172227
files,
173228
json_output,
174229
column_width_distribution,
@@ -181,45 +236,12 @@ impl App {
181236
}
182237

183238
run! {
184-
{
185-
Bytes => |x| x;
186-
ApparentSize => GetApparentSize;
187-
false => error_only_reporter;
188-
}
189-
190-
{
191-
Bytes => |x| x;
192-
ApparentSize => GetApparentSize;
193-
true => progress_and_error_reporter;
194-
}
195-
196-
#[cfg(unix)]
197-
{
198-
Bytes => |x| x;
199-
BlockSize => GetBlockSize;
200-
false => error_only_reporter;
201-
}
202-
203-
#[cfg(unix)]
204-
{
205-
Bytes => |x| x;
206-
BlockSize => GetBlockSize;
207-
true => progress_and_error_reporter;
208-
}
209-
210-
#[cfg(unix)]
211-
{
212-
Blocks => |_| ();
213-
BlockCount => GetBlockCount;
214-
false => error_only_reporter;
215-
}
216-
217-
#[cfg(unix)]
218-
{
219-
Blocks => |_| ();
220-
BlockCount => GetBlockCount;
221-
true => progress_and_error_reporter;
222-
}
239+
APPARENT_SIZE, false;
240+
APPARENT_SIZE, true;
241+
#[cfg(unix)] BLOCK_SIZE, false;
242+
#[cfg(unix)] BLOCK_SIZE, true;
243+
#[cfg(unix)] BLOCK_COUNT, false;
244+
#[cfg(unix)] BLOCK_COUNT, true;
223245
}
224246
}
225247
}

0 commit comments

Comments
 (0)