@@ -4,11 +4,12 @@ pub use sub::Sub;
44
55use 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} ;
1415use clap:: Parser ;
@@ -18,10 +19,7 @@ use std::{io::stdin, time::Duration};
1819use 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.
2725pub struct App {
@@ -119,40 +117,76 @@ 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+ trait GetSizeUtils : GetSize < Size : size:: Size > {
121+ const INSTANCE : Self ;
122+ const QUANTITY : Quantity ;
123+ fn formatter ( bytes_format : BytesFormat ) -> <Self :: Size as size:: Size >:: DisplayFormat ;
124+ }
125+
126+ impl GetSizeUtils for GetApparentSize {
127+ const INSTANCE : Self = GetApparentSize ;
128+ const QUANTITY : Quantity = Quantity :: ApparentSize ;
129+ fn formatter ( bytes_format : BytesFormat ) -> BytesFormat {
130+ bytes_format
131+ }
132+ }
133+
134+ #[ cfg( unix) ]
135+ impl GetSizeUtils for GetBlockSize {
136+ const INSTANCE : Self = GetBlockSize ;
137+ const QUANTITY : Quantity = Quantity :: BlockSize ;
138+ fn formatter ( bytes_format : BytesFormat ) -> BytesFormat {
139+ bytes_format
140+ }
141+ }
142+
143+ #[ cfg( unix) ]
144+ impl GetSizeUtils for GetBlockCount {
145+ const INSTANCE : Self = GetBlockCount ;
146+ const QUANTITY : Quantity = Quantity :: BlockCount ;
147+ fn formatter ( _: BytesFormat ) { }
148+ }
149+
150+ trait CreateReporter < const REPORT_PROGRESS : bool > : GetSizeUtils {
151+ type Reporter ;
152+ fn create_reporter ( report_error : fn ( ErrorReport ) ) -> Self :: Reporter ;
153+ }
154+
155+ impl < SizeGetter > CreateReporter < false > for SizeGetter
156+ where
157+ SizeGetter : GetSizeUtils ,
158+ {
159+ type Reporter = ErrorOnlyReporter < fn ( ErrorReport ) > ;
160+ fn create_reporter ( report_error : fn ( ErrorReport ) ) -> Self :: Reporter {
161+ ErrorOnlyReporter :: new ( report_error)
162+ }
127163 }
128164
129- fn progress_and_error_reporter < Size > (
130- report_error : fn ( ErrorReport ) ,
131- ) -> ProgressAndErrorReporter < Size , fn ( ErrorReport ) >
165+ impl < SizeGetter > CreateReporter < true > for SizeGetter
132166 where
133- Size : size:: Size + Into < u64 > + Send + Sync ,
134- ProgressReport < Size > : Default + ' static ,
135- u64 : Into < Size > ,
167+ SizeGetter : GetSizeUtils ,
168+ SizeGetter :: Size : Into < u64 > + Send + Sync ,
169+ ProgressReport < SizeGetter :: Size > : Default + ' static ,
170+ u64 : Into < SizeGetter :: Size > ,
136171 {
137- ProgressAndErrorReporter :: new (
138- ProgressReport :: TEXT ,
139- Duration :: from_millis ( 100 ) ,
140- report_error,
141- )
172+ type Reporter = ProgressAndErrorReporter < SizeGetter :: Size , fn ( ErrorReport ) > ;
173+ fn create_reporter ( report_error : fn ( ErrorReport ) ) -> Self :: Reporter {
174+ ProgressAndErrorReporter :: new (
175+ ProgressReport :: TEXT ,
176+ Duration :: from_millis ( 100 ) ,
177+ report_error,
178+ )
179+ }
142180 }
143181
144182 macro_rules! run {
145183 ( $(
146184 $( #[ $variant_attrs: meta] ) *
147- {
148- $size: ty => $format: expr;
149- $quantity: ident => $size_getter: ident;
150- $progress: literal => $create_reporter: ident;
151- }
185+ $size_getter: ident, $progress: literal;
152186 ) * ) => { match self . args { $(
153187 $( #[ $variant_attrs] ) *
154188 Args {
155- quantity: Quantity :: $quantity ,
189+ quantity: <$size_getter as GetSizeUtils > :: QUANTITY ,
156190 progress: $progress,
157191 files,
158192 json_output,
@@ -166,9 +200,9 @@ impl App {
166200 } => Sub {
167201 direction: Direction :: from_top_down( top_down) ,
168202 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) ,
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) ,
172206 files,
173207 json_output,
174208 column_width_distribution,
@@ -181,45 +215,12 @@ impl App {
181215 }
182216
183217 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- }
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 ;
223224 }
224225 }
225226}
0 commit comments