Skip to content

Commit ec1e93f

Browse files
committed
refactor: base the traits on the size getter types
1 parent cc5af27 commit ec1e93f

1 file changed

Lines changed: 29 additions & 50 deletions

File tree

src/app.rs

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -117,80 +117,59 @@ impl App {
117117
ErrorReport::TEXT
118118
};
119119

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> {
120+
trait GetSizeUtils: GetSize<Size: size::Size> {
121+
const INSTANCE: Self;
133122
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>;
123+
fn formatter(bytes_format: BytesFormat) -> <Self::Size as size::Size>::DisplayFormat;
137124
}
138125

139-
impl QuantityUtils<{ quantity_index::APPARENT_SIZE }> for () {
126+
impl GetSizeUtils for GetApparentSize {
127+
const INSTANCE: Self = GetApparentSize;
140128
const QUANTITY: Quantity = Quantity::ApparentSize;
141-
type SizeGetter = GetApparentSize;
142-
const SIZE_GETTER: Self::SizeGetter = GetApparentSize;
143129
fn formatter(bytes_format: BytesFormat) -> BytesFormat {
144130
bytes_format
145131
}
146132
}
147133

148134
#[cfg(unix)]
149-
impl QuantityUtils<{ quantity_index::BLOCK_SIZE }> for () {
135+
impl GetSizeUtils for GetBlockSize {
136+
const INSTANCE: Self = GetBlockSize;
150137
const QUANTITY: Quantity = Quantity::BlockSize;
151-
type SizeGetter = GetBlockSize;
152-
const SIZE_GETTER: Self::SizeGetter = GetBlockSize;
153138
fn formatter(bytes_format: BytesFormat) -> BytesFormat {
154139
bytes_format
155140
}
156141
}
157142

158143
#[cfg(unix)]
159-
impl QuantityUtils<{ quantity_index::BLOCK_COUNT }> for () {
144+
impl GetSizeUtils for GetBlockCount {
145+
const INSTANCE: Self = GetBlockCount;
160146
const QUANTITY: Quantity = Quantity::BlockCount;
161-
type SizeGetter = GetBlockCount;
162-
const SIZE_GETTER: Self::SizeGetter = GetBlockCount;
163147
fn formatter(_: BytesFormat) {}
164148
}
165149

166-
trait CreateReporter<const REPORT_PROGRESS: bool, const QUANTITY_INDEX: u8> {
150+
trait CreateReporter<const REPORT_PROGRESS: bool>: GetSizeUtils {
167151
type Reporter;
168152
fn create_reporter(report_error: fn(ErrorReport)) -> Self::Reporter;
169153
}
170154

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 ()
155+
impl<SizeGetter> CreateReporter<false> for SizeGetter
175156
where
176-
(): QuantityUtils<QUANTITY_INDEX>,
177-
QuantityIndexToSizeType<QUANTITY_INDEX>: size::Size,
157+
SizeGetter: GetSizeUtils,
178158
{
179159
type Reporter = ErrorOnlyReporter<fn(ErrorReport)>;
180160
fn create_reporter(report_error: fn(ErrorReport)) -> Self::Reporter {
181161
ErrorOnlyReporter::new(report_error)
182162
}
183163
}
184164

185-
impl<const QUANTITY_INDEX: u8> CreateReporter<true, QUANTITY_INDEX> for ()
165+
impl<SizeGetter> CreateReporter<true> for SizeGetter
186166
where
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>>,
167+
SizeGetter: GetSizeUtils,
168+
SizeGetter::Size: Into<u64> + Send + Sync,
169+
ProgressReport<SizeGetter::Size>: Default + 'static,
170+
u64: Into<SizeGetter::Size>,
191171
{
192-
type Reporter =
193-
ProgressAndErrorReporter<QuantityIndexToSizeType<QUANTITY_INDEX>, fn(ErrorReport)>;
172+
type Reporter = ProgressAndErrorReporter<SizeGetter::Size, fn(ErrorReport)>;
194173
fn create_reporter(report_error: fn(ErrorReport)) -> Self::Reporter {
195174
ProgressAndErrorReporter::new(
196175
ProgressReport::TEXT,
@@ -203,11 +182,11 @@ impl App {
203182
macro_rules! run {
204183
($(
205184
$(#[$variant_attrs:meta])*
206-
$quantity_index:ident, $progress:literal;
185+
$size_getter:ident, $progress:literal;
207186
)*) => { match self.args {$(
208187
$(#[$variant_attrs])*
209188
Args {
210-
quantity: <() as QuantityUtils<{ quantity_index::$quantity_index }>>::QUANTITY,
189+
quantity: <$size_getter as GetSizeUtils>::QUANTITY,
211190
progress: $progress,
212191
files,
213192
json_output,
@@ -221,9 +200,9 @@ impl App {
221200
} => Sub {
222201
direction: Direction::from_top_down(top_down),
223202
bar_alignment: BarAlignment::from_align_right(align_right),
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),
203+
size_getter: <$size_getter as GetSizeUtils>::INSTANCE,
204+
reporter: <$size_getter as CreateReporter<$progress>>::create_reporter(report_error),
205+
bytes_format: <$size_getter as GetSizeUtils>::formatter(bytes_format),
227206
files,
228207
json_output,
229208
column_width_distribution,
@@ -236,12 +215,12 @@ impl App {
236215
}
237216

238217
run! {
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;
218+
GetApparentSize, false;
219+
GetApparentSize, true;
220+
#[cfg(unix)] GetBlockSize, false;
221+
#[cfg(unix)] GetBlockSize, true;
222+
#[cfg(unix)] GetBlockCount, false;
223+
#[cfg(unix)] GetBlockCount, true;
245224
}
246225
}
247226
}

0 commit comments

Comments
 (0)