From f475a362042301319665f68c5869d5145cd38da4 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Tue, 11 Apr 2023 20:41:57 +0200 Subject: [PATCH] du: reduce the complexity by moving the time option mgmt into a function --- src/uu/du/src/du.rs | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index 2dcfbf2ac..c5aafb44e 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -625,20 +625,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { if matches.contains_id(options::TIME) { let tm = { - let secs = { - match matches.get_one::(options::TIME) { - Some(s) => match s.as_str() { - "ctime" | "status" => stat.modified, - "access" | "atime" | "use" => stat.accessed, - "birth" | "creation" => stat - .created - .ok_or_else(|| DuError::InvalidTimeArg(s.into()))?, - // below should never happen as clap already restricts the values. - _ => unreachable!("Invalid field for --time"), - }, - None => stat.modified, - } - }; + let secs = matches + .get_one::(options::TIME) + .map(|s| get_time_secs(s, &stat)) + .transpose()? + .unwrap_or(stat.modified); DateTime::::from(UNIX_EPOCH + Duration::from_secs(secs)) }; if !summarize || index == len - 1 { @@ -676,6 +667,19 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { Ok(()) } +fn get_time_secs(s: &str, stat: &Stat) -> std::result::Result { + let secs = match s { + "ctime" | "status" => stat.modified, + "access" | "atime" | "use" => stat.accessed, + "birth" | "creation" => stat + .created + .ok_or_else(|| DuError::InvalidTimeArg(s.into()))?, + // below should never happen as clap already restricts the values. + _ => unreachable!("Invalid field for --time"), + }; + Ok(secs) +} + fn parse_time_style(s: Option<&str>) -> UResult<&str> { match s { Some(s) => match s {