Some simple Option / Result / iterator pattern simplifications (#3205)

This commit is contained in:
François Garillot
2020-01-29 09:20:57 -05:00
committed by GitHub
parent 616dad43fd
commit dcdbdd4bcc
10 changed files with 39 additions and 57 deletions
+8 -11
View File
@@ -70,15 +70,12 @@ fn copy_to(src: &Path, src_type: &fs::FileType, dst: &Path) -> io::Result<u64> {
/// Retrieve first line from file
pub fn get_first_line(file_path: Option<String>) -> Option<String> {
match file_path {
Some(path) => match fs::File::open(path) {
Ok(file) => {
let buf_reader = io::BufReader::new(file);
let mut lines_iter = buf_reader.lines().map(|l| l.unwrap());
lines_iter.next()
}
Err(_) => None,
},
None => None,
}
file_path.and_then(|path| match fs::File::open(path) {
Ok(file) => {
let buf_reader = io::BufReader::new(file);
let mut lines_iter = buf_reader.lines().map(|l| l.unwrap());
lines_iter.next()
}
Err(_) => None,
})
}