Skip to content

Commit 47aff05

Browse files
committed
uucore: fsext: Provide From<&str> helper for MetadataTimeField
We assume that the string has been validated by clap ahead of time (just like the current code does).
1 parent 8985afa commit 47aff05

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

src/uu/du/src/du.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -648,12 +648,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
648648
};
649649

650650
let time = matches.contains_id(options::TIME).then(|| {
651-
match matches.get_one::<String>(options::TIME).map(AsRef::as_ref) {
652-
None | Some("ctime" | "status") => MetadataTimeField::Modification,
653-
Some("access" | "atime" | "use") => MetadataTimeField::Access,
654-
Some("birth" | "creation") => MetadataTimeField::Birth,
655-
_ => unreachable!("should be caught by clap"),
656-
}
651+
matches
652+
.get_one::<String>(options::TIME)
653+
.map_or(MetadataTimeField::Modification, |s| s.as_str().into())
657654
});
658655

659656
let size_format = if matches.get_flag(options::HUMAN_READABLE) {

src/uu/ls/src/ls.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -464,14 +464,7 @@ fn extract_sort(options: &clap::ArgMatches) -> Sort {
464464
/// A `MetadataTimeField` variant representing the time to use.
465465
fn extract_time(options: &clap::ArgMatches) -> MetadataTimeField {
466466
if let Some(field) = options.get_one::<String>(options::TIME) {
467-
match field.as_str() {
468-
"ctime" | "status" => MetadataTimeField::Change,
469-
"access" | "atime" | "use" => MetadataTimeField::Access,
470-
"mtime" | "modification" => MetadataTimeField::Modification,
471-
"birth" | "creation" => MetadataTimeField::Birth,
472-
// below should never happen as clap already restricts the values.
473-
_ => unreachable!("Invalid field for --time"),
474-
}
467+
field.as_str().into()
475468
} else if options.get_flag(options::time::ACCESS) {
476469
MetadataTimeField::Access
477470
} else if options.get_flag(options::time::CHANGE) {

src/uucore/src/lib/features/fsext.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,22 @@ pub enum MetadataTimeField {
135135
Birth,
136136
}
137137

138+
impl From<&str> for MetadataTimeField {
139+
/// Get a `MetadataTimeField` from a string, we expect the value
140+
/// to come from clap, and be constrained there (e.g. if Modification is
141+
/// not supported), and the default branch should not be reached.
142+
fn from(value: &str) -> Self {
143+
match value {
144+
"ctime" | "status" => MetadataTimeField::Change,
145+
"access" | "atime" | "use" => MetadataTimeField::Access,
146+
"mtime" | "modification" => MetadataTimeField::Modification,
147+
"birth" | "creation" => MetadataTimeField::Birth,
148+
// below should never happen as clap already restricts the values.
149+
_ => unreachable!("Invalid metadata time field."),
150+
}
151+
}
152+
}
153+
138154
// The implementations for get_system_time are separated because some options, such
139155
// as ctime will not be available
140156
#[cfg(unix)]

0 commit comments

Comments
 (0)