Skip to content

Commit d8902c1

Browse files
committed
refactor: infer reporter via utility traits
1 parent 452786e commit d8902c1

1 file changed

Lines changed: 30 additions & 22 deletions

File tree

src/app.rs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -117,26 +117,35 @@ impl App {
117117
ErrorReport::TEXT
118118
};
119119

120-
#[allow(clippy::extra_unused_type_parameters)]
121-
fn error_only_reporter<Size>(
122-
report_error: fn(ErrorReport),
123-
) -> ErrorOnlyReporter<fn(ErrorReport)> {
124-
ErrorOnlyReporter::new(report_error)
120+
trait CreateReporter<const REPORT_PROGRESS: bool, Size> {
121+
type Reporter;
122+
fn create_reporter(report_error: fn(ErrorReport)) -> Self::Reporter;
125123
}
126124

127-
fn progress_and_error_reporter<Size>(
128-
report_error: fn(ErrorReport),
129-
) -> ProgressAndErrorReporter<Size, fn(ErrorReport)>
125+
impl<Size> CreateReporter<false, Size> for ()
126+
where
127+
Size: size::Size,
128+
{
129+
type Reporter = ErrorOnlyReporter<fn(ErrorReport)>;
130+
fn create_reporter(report_error: fn(ErrorReport)) -> Self::Reporter {
131+
ErrorOnlyReporter::new(report_error)
132+
}
133+
}
134+
135+
impl<Size> CreateReporter<true, Size> for ()
130136
where
131137
Size: size::Size + Into<u64> + Send + Sync,
132138
ProgressReport<Size>: Default + 'static,
133139
u64: Into<Size>,
134140
{
135-
ProgressAndErrorReporter::new(
136-
ProgressReport::TEXT,
137-
Duration::from_millis(100),
138-
report_error,
139-
)
141+
type Reporter = ProgressAndErrorReporter<Size, fn(ErrorReport)>;
142+
fn create_reporter(report_error: fn(ErrorReport)) -> Self::Reporter {
143+
ProgressAndErrorReporter::new(
144+
ProgressReport::TEXT,
145+
Duration::from_millis(100),
146+
report_error,
147+
)
148+
}
140149
}
141150

142151
trait GetSizeUtils: GetSize {
@@ -168,8 +177,7 @@ impl App {
168177
macro_rules! run {
169178
($(
170179
$(#[$variant_attrs:meta])*
171-
$quantity:ident, $size_getter:ident,
172-
$progress:literal, $create_reporter:ident;
180+
$quantity:ident, $size_getter:ident, $progress:literal;
173181
)*) => { match self.args {$(
174182
$(#[$variant_attrs])*
175183
Args {
@@ -188,7 +196,7 @@ impl App {
188196
direction: Direction::from_top_down(top_down),
189197
bar_alignment: BarAlignment::from_align_right(align_right),
190198
size_getter: $size_getter,
191-
reporter: $create_reporter::<<$size_getter as GetSize>::Size>(report_error),
199+
reporter: <() as CreateReporter<$progress, <$size_getter as GetSize>::Size>>::create_reporter(report_error),
192200
bytes_format: <$size_getter as GetSizeUtils>::format_size(bytes_format),
193201
files,
194202
json_output,
@@ -202,12 +210,12 @@ impl App {
202210
}
203211

204212
run! {
205-
ApparentSize, GetApparentSize, false, error_only_reporter;
206-
ApparentSize, GetApparentSize, true, progress_and_error_reporter;
207-
#[cfg(unix)] BlockSize, GetBlockSize, false, error_only_reporter;
208-
#[cfg(unix)] BlockSize, GetBlockSize, true, progress_and_error_reporter;
209-
#[cfg(unix)] BlockCount, GetBlockCount, false, error_only_reporter;
210-
#[cfg(unix)] BlockCount, GetBlockCount, true, progress_and_error_reporter;
213+
ApparentSize, GetApparentSize, false;
214+
ApparentSize, GetApparentSize, true;
215+
#[cfg(unix)] BlockSize, GetBlockSize, false;
216+
#[cfg(unix)] BlockSize, GetBlockSize, true;
217+
#[cfg(unix)] BlockCount, GetBlockCount, false;
218+
#[cfg(unix)] BlockCount, GetBlockCount, true;
211219
}
212220
}
213221
}

0 commit comments

Comments
 (0)