Skip to content

Commit

Permalink
Re-write requires_update logic to remove unwraps
Browse files Browse the repository at this point in the history
And to use a direct comparison instead of the duration_since method
which I assume is ok.

Otherwise, we just and_then the results and exit early if we're not
getting access to the 'modified' times that we need to make the
decision. We conservatively require an update if we fail to access the
modified times.
  • Loading branch information
michaeljones committed Dec 3, 2023
1 parent 0914462 commit d2611d9
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,23 @@ fn color_choice() -> ColorChoice {
}

fn requires_update(matcha_path: &std::path::Path) -> bool {
let matcha_meta = std::fs::metadata(matcha_path).unwrap();
let matcha_time = matcha_meta.modified().unwrap();
let matcha_result = std::fs::metadata(matcha_path).and_then(|data| data.modified());

// If we fail to get the modified time for any reason than assume we have to update
let Ok(matcha_time) = matcha_result else {
return true;
};

let gleam_path = matcha_path.with_extension("gleam");
let gleam_meta = std::fs::metadata(gleam_path).unwrap();
let gleam_time = gleam_meta.modified().unwrap();
let gleam_result = std::fs::metadata(gleam_path).and_then(|data| data.modified());

if let Ok(_duration) = matcha_time.duration_since(gleam_time) {
true
} else {
false
}
// If we fail to get the modified time for any reason than assume we have to update
let Ok(gleam_time) = gleam_result else {
return true;
};

// Should update the gleam file if the matcha file is newer than it
matcha_time > gleam_time
}

#[derive(Debug, StructOpt)]
Expand Down

0 comments on commit d2611d9

Please sign in to comment.