Skip to content

Commit

Permalink
Use if-let chains when it improves readability (#295)
Browse files Browse the repository at this point in the history
Fixes #195.
  • Loading branch information
crawfordleeds authored and marco-c committed Jul 16, 2019
1 parent c005222 commit 0ac13c2
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 113 deletions.
7 changes: 4 additions & 3 deletions src/gcov.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use std::path::PathBuf;
use std::process::Command;

fn get_gcov() -> String {
match env::var("GCOV") {
Ok(s) => s,
Err(_) => "gcov".to_string(),
if let Ok(s) = env::var("GCOV") {
s
} else {
"gcov".to_string()
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ fn add_results(
let path = match source_dir {
Some(source_dir) => {
// the goal here is to be able to merge results for paths like foo/./bar and foo/bar
match canonicalize_path(source_dir.join(&result.0)) {
Ok(p) => String::from(p.to_str().unwrap()),
Err(_) => result.0,
if let Ok(p) = canonicalize_path(source_dir.join(&result.0)) {
String::from(p.to_str().unwrap())
} else {
result.0
}
}
None => result.0,
Expand Down
23 changes: 11 additions & 12 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub fn output_covdir(results: CovResultIter, output_file: Option<&str>) {
let mut relative: FxHashMap<PathBuf, Rc<RefCell<CDDirStats>>> = FxHashMap::default();
let global = Rc::new(RefCell::new(CDDirStats::new("".to_string())));
relative.insert(PathBuf::from(""), global.clone());

for (abs_path, rel_path, result) in results {
let path = if rel_path.is_relative() {
rel_path
Expand Down Expand Up @@ -253,15 +253,14 @@ pub fn output_lcov(results: CovResultIter, output_file: Option<&str>) {
}

fn get_digest(path: PathBuf) -> String {
match File::open(path) {
Ok(mut f) => {
let mut buffer = Vec::new();
f.read_to_end(&mut buffer).unwrap();
let mut hasher = Md5::new();
hasher.input(buffer.as_slice());
format!("{:x}", hasher.result())
}
Err(_) => Uuid::new_v4().to_string(),
if let Ok(mut f) = File::open(path) {
let mut buffer = Vec::new();
f.read_to_end(&mut buffer).unwrap();
let mut hasher = Md5::new();
hasher.input(buffer.as_slice());
format!("{:x}", hasher.result())
} else {
Uuid::new_v4().to_string()
}
}

Expand Down Expand Up @@ -367,7 +366,7 @@ mod tests {
f.read_to_string(&mut s).unwrap();
s
}

#[test]
fn test_covdir() {
let tmp_dir = tempfile::tempdir().expect("Failed to create temporary directory");
Expand Down Expand Up @@ -406,7 +405,7 @@ mod tests {
];

let results = Box::new(results.into_iter());
output_covdir(results, Some(file_path.to_str().unwrap()));
output_covdir(results, Some(file_path.to_str().unwrap()));

let results: Value = serde_json::from_str(&read_file(&file_path)).unwrap();
let expected_path = PathBuf::from("./test/").join(&file_name);
Expand Down
7 changes: 4 additions & 3 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ macro_rules! try_parse {

macro_rules! try_next {
($v:expr, $l:expr) => {
match $v.next() {
Some(val) => val,
None => return Err(ParserError::InvalidRecord($l.to_string())),
if let Some(val) = $v.next() {
val
} else {
return Err(ParserError::InvalidRecord($l.to_string()))
}
};
}
Expand Down
9 changes: 5 additions & 4 deletions src/path_rewriting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ fn get_abs_path(

// Fixup the relative path, in case the absolute path was a symlink.
let rel_path = fixup_rel_path(&source_dir, &abs_path, rel_path);

// Normalize the path in removing './' or '//' or '..'
let rel_path = normalize_path(rel_path);
let abs_path = normalize_path(abs_path);
Expand Down Expand Up @@ -207,9 +207,10 @@ fn map_partial_path(file_to_paths: &FxHashMap<String, Vec<PathBuf>>, path: PathB
}
}

match result {
Some(result) => result.clone(),
None => path,
if let Some(result) = result {
result.clone()
} else {
path
}
}

Expand Down
165 changes: 77 additions & 88 deletions src/producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,23 +250,19 @@ impl Archive {
pub fn extract(&self, name: &str, path: &PathBuf) -> bool {
let dest_parent = path.parent().unwrap();
if !dest_parent.exists() {
match fs::create_dir_all(dest_parent) {
Ok(_) => {}
Err(_) => panic!("Cannot create parent directory"),
};
fs::create_dir_all(dest_parent).expect("Cannot create parent directory");
}

match *self.item.borrow_mut() {
ArchiveType::Zip(ref mut zip) => {
let mut zip = zip.borrow_mut();
let zipfile = zip.by_name(&name);
match zipfile {
Ok(mut f) => {
let mut file = File::create(&path).expect("Failed to create file");
io::copy(&mut f, &mut file).expect("Failed to copy file from ZIP");
true
}
Err(_) => false,
if let Ok(mut f) = zipfile {
let mut file = File::create(&path).expect("Failed to create file");
io::copy(&mut f, &mut file).expect("Failed to copy file from ZIP");
true
} else {
false
}
}
ArchiveType::Dir(ref dir) => {
Expand Down Expand Up @@ -309,79 +305,76 @@ fn gcno_gcda_producer(

for (gcno_stem, gcno_archive) in gcno_stem_archives {
let stem = &gcno_stem.stem;
match gcda_stem_archives.get(stem) {
Some(gcda_archives) => {
if let Some(gcda_archives) = gcda_stem_archives.get(stem) {
let gcno_archive = *gcno_archive;
let gcno = format!("{}.gcno", stem).to_string();
let physical_gcno_path = tmp_dir.join(format!("{}_{}.gcno", stem, 1));
if gcno_stem.llvm {
let mut gcno_buffer: Vec<u8> = Vec::new();
let mut gcda_buffers: Vec<Vec<u8>> = Vec::with_capacity(gcda_archives.len());
gcno_archive.read_in_buffer(&gcno, &mut gcno_buffer);
for gcda_archive in gcda_archives {
let mut gcda_buf: Vec<u8> = Vec::new();
let gcda = format!("{}.gcda", stem).to_string();
if gcda_archive.read_in_buffer(&gcda, &mut gcda_buf) {
gcda_buffers.push(gcda_buf);
}
}
send_job(
ItemType::Buffers(GcnoBuffers {
stem: stem.clone(),
gcno_buf: gcno_buffer,
gcda_buf: gcda_buffers,
}),
"".to_string(),
);
} else {
gcno_archive.extract(&gcno, &physical_gcno_path);
for (num, &gcda_archive) in gcda_archives.iter().enumerate() {
let gcno_path = tmp_dir.join(format!("{}_{}.gcno", stem, num + 1));
let gcda = format!("{}.gcda", stem).to_string();

// Create symlinks.
if num != 0 {
fs::hard_link(&physical_gcno_path, &gcno_path).unwrap_or_else(|_| {
panic!("Failed to create hardlink {:?}", gcno_path)
});
}

let gcda_path = tmp_dir.join(format!("{}_{}.gcda", stem, num + 1));
if gcda_archive.extract(&gcda, &gcda_path)
|| (num == 0 && !ignore_orphan_gcno)
{
send_job(
ItemType::Path((stem.clone(), gcno_path)),
gcda_archive.get_name().to_string(),
);
}
}
}
} else {
if !ignore_orphan_gcno {
let gcno_archive = *gcno_archive;
let gcno = format!("{}.gcno", stem).to_string();
let physical_gcno_path = tmp_dir.join(format!("{}_{}.gcno", stem, 1));
if gcno_stem.llvm {
let mut gcno_buffer: Vec<u8> = Vec::new();
let mut gcda_buffers: Vec<Vec<u8>> = Vec::with_capacity(gcda_archives.len());
gcno_archive.read_in_buffer(&gcno, &mut gcno_buffer);
for gcda_archive in gcda_archives {
let mut gcda_buf: Vec<u8> = Vec::new();
let gcda = format!("{}.gcda", stem).to_string();
if gcda_archive.read_in_buffer(&gcda, &mut gcda_buf) {
gcda_buffers.push(gcda_buf);
}
}
let mut buffer: Vec<u8> = Vec::new();
gcno_archive.read_in_buffer(&gcno, &mut buffer);

send_job(
ItemType::Buffers(GcnoBuffers {
stem: stem.clone(),
gcno_buf: gcno_buffer,
gcda_buf: gcda_buffers,
gcno_buf: buffer,
gcda_buf: Vec::new(),
}),
"".to_string(),
gcno_archive.get_name().to_string(),
);
} else {
gcno_archive.extract(&gcno, &physical_gcno_path);
for (num, &gcda_archive) in gcda_archives.iter().enumerate() {
let gcno_path = tmp_dir.join(format!("{}_{}.gcno", stem, num + 1));
let gcda = format!("{}.gcda", stem).to_string();

// Create symlinks.
if num != 0 {
fs::hard_link(&physical_gcno_path, &gcno_path).unwrap_or_else(|_| {
panic!("Failed to create hardlink {:?}", gcno_path)
});
}

let gcda_path = tmp_dir.join(format!("{}_{}.gcda", stem, num + 1));
if gcda_archive.extract(&gcda, &gcda_path)
|| (num == 0 && !ignore_orphan_gcno)
{
send_job(
ItemType::Path((stem.clone(), gcno_path)),
gcda_archive.get_name().to_string(),
);
}
}
}
}
None => {
if !ignore_orphan_gcno {
let gcno_archive = *gcno_archive;
let gcno = format!("{}.gcno", stem).to_string();
if gcno_stem.llvm {
let mut buffer: Vec<u8> = Vec::new();
gcno_archive.read_in_buffer(&gcno, &mut buffer);

let physical_gcno_path = tmp_dir.join(format!("{}_{}.gcno", stem, 1));
if gcno_archive.extract(&gcno, &physical_gcno_path) {
send_job(
ItemType::Buffers(GcnoBuffers {
stem: stem.clone(),
gcno_buf: buffer,
gcda_buf: Vec::new(),
}),
ItemType::Path((stem.clone(), physical_gcno_path)),
gcno_archive.get_name().to_string(),
);
} else {
let physical_gcno_path = tmp_dir.join(format!("{}_{}.gcno", stem, 1));
if gcno_archive.extract(&gcno, &physical_gcno_path) {
send_job(
ItemType::Path((stem.clone(), physical_gcno_path)),
gcno_archive.get_name().to_string(),
);
}
}
}
}
Expand All @@ -408,13 +401,12 @@ fn file_content_producer(
}

pub fn get_mapping(linked_files_maps: &FxHashMap<String, &Archive>) -> Option<Vec<u8>> {
match linked_files_maps.iter().next() {
Some((ref name, archive)) => {
let mut buffer = Vec::new();
archive.read_in_buffer(name, &mut buffer);
Some(buffer)
}
None => None,
if let Some((ref name, archive)) = linked_files_maps.iter().next() {
let mut buffer = Vec::new();
archive.read_in_buffer(name, &mut buffer);
Some(buffer)
} else {
None
}
}

Expand Down Expand Up @@ -1464,15 +1456,12 @@ mod tests {

let expected = vec![(ItemFormat::INFO, false, "prova_1.info", true)];

match File::open(json_path) {
Ok(mut reader) => {
let mut json = Vec::new();
reader.read_to_end(&mut json).unwrap();
assert_eq!(json, mapping);
}
Err(_) => {
assert!(false, format!("Failed to read the file: {}", json_path));
}
if let Ok(mut reader) = File::open(json_path) {
let mut json = Vec::new();
reader.read_to_end(&mut json).unwrap();
assert_eq!(json, mapping);
} else {
assert!(false, format!("Failed to read the file: {}", json_path));
}

check_produced(tmp_path, &receiver, expected);
Expand Down

0 comments on commit 0ac13c2

Please sign in to comment.