Skip to content

Commit

Permalink
fs: split get_file_display into its function
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvestre committed Oct 12, 2023
1 parent f76b53d commit 330f69a
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions src/uucore/src/lib/features/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,20 @@ pub fn display_permissions(metadata: &fs::Metadata, display_file_type: bool) ->
display_permissions_unix(mode, display_file_type)
}

fn get_file_display(mode: mode_t) -> char {
match mode & S_IFMT {
S_IFDIR => 'd',
S_IFCHR => 'c',
S_IFBLK => 'b',

Check warning on line 472 in src/uucore/src/lib/features/fs.rs

View check run for this annotation

Codecov / codecov/patch

src/uucore/src/lib/features/fs.rs#L472

Added line #L472 was not covered by tests
S_IFREG => '-',
S_IFIFO => 'p',
S_IFLNK => 'l',
S_IFSOCK => 's',

Check warning on line 476 in src/uucore/src/lib/features/fs.rs

View check run for this annotation

Codecov / codecov/patch

src/uucore/src/lib/features/fs.rs#L476

Added line #L476 was not covered by tests
// TODO: Other file types
_ => '?',

Check warning on line 478 in src/uucore/src/lib/features/fs.rs

View check run for this annotation

Codecov / codecov/patch

src/uucore/src/lib/features/fs.rs#L478

Added line #L478 was not covered by tests
}
}

// The logic below is more readable written this way.
#[allow(clippy::if_not_else)]
#[allow(clippy::cognitive_complexity)]
Expand All @@ -474,17 +488,7 @@ pub fn display_permissions_unix(mode: mode_t, display_file_type: bool) -> String
let mut result;
if display_file_type {
result = String::with_capacity(10);
result.push(match mode & S_IFMT {
S_IFDIR => 'd',
S_IFCHR => 'c',
S_IFBLK => 'b',
S_IFREG => '-',
S_IFIFO => 'p',
S_IFLNK => 'l',
S_IFSOCK => 's',
// TODO: Other file types
_ => '?',
});
result.push(get_file_display(mode));
} else {
result = String::with_capacity(9);
}
Expand Down Expand Up @@ -881,4 +885,16 @@ mod tests {

assert!(are_hardlinks_to_same_file(&path1, &path2));
}

#[test]
fn test_get_file_display() {
assert_eq!(get_file_display(S_IFDIR | 0o755), 'd');
assert_eq!(get_file_display(S_IFCHR | 0o644), 'c');
assert_eq!(get_file_display(S_IFBLK | 0o600), 'b');
assert_eq!(get_file_display(S_IFREG | 0o777), '-');
assert_eq!(get_file_display(S_IFIFO | 0o666), 'p');
assert_eq!(get_file_display(S_IFLNK | 0o777), 'l');
assert_eq!(get_file_display(S_IFSOCK | 0o600), 's');
assert_eq!(get_file_display(0o777), '?');
}
}

0 comments on commit 330f69a

Please sign in to comment.