Skip to content

Commit c4e17fd

Browse files
committed
Refactored things to use more .filter()s, .and_then()s, and .map()s to make it cleaner and more concise.
1 parent 18e95f5 commit c4e17fd

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

c2rust/src/main.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,22 @@ impl SubCommand {
4242
for entry in dir.read_dir()? {
4343
let entry = entry?;
4444
let file_type = entry.file_type()?;
45-
if !(file_type.is_file() || file_type.is_symlink()) {
46-
continue;
47-
}
48-
let file_name = entry.file_name();
49-
let name = match Path::new(&file_name)
50-
.to_str()
45+
let path = entry.path();
46+
let name = match path.file_name()
47+
.and_then(|name| name.to_str())
5148
.and_then(|name| name.strip_prefix(c2rust_name))
5249
.and_then(|name| name.strip_prefix('-'))
50+
.map(|name| name.to_owned())
51+
.map(Cow::from)
52+
.filter(|_| file_type.is_file() || file_type.is_symlink())
53+
.filter(|_| path.is_executable())
5354
{
5455
Some(name) => name,
5556
None => continue,
5657
};
57-
let path = entry.path();
58-
if !path.is_executable() {
59-
continue;
60-
}
6158
sub_commands.push(Self {
6259
path: Some(path),
63-
name: name.to_owned().into(),
60+
name,
6461
});
6562
}
6663
Ok(sub_commands)

0 commit comments

Comments
 (0)