du: reduce the complexity by moving get_convert_size_fn option mgmt into a function

This commit is contained in:
Sylvestre Ledru
2023-04-13 13:38:27 +02:00
parent f475a36204
commit b2fd72878e
+17 -13
View File
@@ -400,6 +400,20 @@ fn convert_size_other(size: u64, _multiplier: u64, block_size: u64) -> String {
format!("{}", ((size as f64) / (block_size as f64)).ceil())
}
fn get_convert_size_fn(matches: &ArgMatches) -> Box<dyn Fn(u64, u64, u64) -> String> {
if matches.get_flag(options::HUMAN_READABLE) || matches.get_flag(options::SI) {
Box::new(convert_size_human)
} else if matches.get_flag(options::BYTES) {
Box::new(convert_size_b)
} else if matches.get_flag(options::BLOCK_SIZE_1K) {
Box::new(convert_size_k)
} else if matches.get_flag(options::BLOCK_SIZE_1M) {
Box::new(convert_size_m)
} else {
Box::new(convert_size_other)
}
}
#[derive(Debug)]
enum DuError {
InvalidMaxDepthArg(String),
@@ -556,19 +570,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
} else {
1024
};
let convert_size_fn = {
if matches.get_flag(options::HUMAN_READABLE) || matches.get_flag(options::SI) {
convert_size_human
} else if matches.get_flag(options::BYTES) {
convert_size_b
} else if matches.get_flag(options::BLOCK_SIZE_1K) {
convert_size_k
} else if matches.get_flag(options::BLOCK_SIZE_1M) {
convert_size_m
} else {
convert_size_other
}
};
let convert_size_fn = get_convert_size_fn(&matches);
let convert_size = |size: u64| {
if options.inodes {
size.to_string()