diff options
author | Daniel <kingdread@gmx.de> | 2020-05-04 11:50:36 +0200 |
---|---|---|
committer | Daniel <kingdread@gmx.de> | 2020-05-04 11:50:36 +0200 |
commit | 0d9c5a67f97af4d2b0fb543e2f1edf00ed0c7552 (patch) | |
tree | 3f4ff3c9df7a9e0639379b36145aae7324113fc2 /src/main.rs | |
parent | 87669f614eac396035aa9bf4c33f84fac9758846 (diff) | |
download | raidgrep-0d9c5a67f97af4d2b0fb543e2f1edf00ed0c7552.tar.gz raidgrep-0d9c5a67f97af4d2b0fb543e2f1edf00ed0c7552.tar.bz2 raidgrep-0d9c5a67f97af4d2b0fb543e2f1edf00ed0c7552.zip |
rewrite maybe_save_history
This avoids unwrapping (and therefore panicing) when the path doens't
have a parent. It also avoids the explicit Into::into() calls.
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/src/main.rs b/src/main.rs index 5b6d57f..0a98c4e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use std::path::{Path, PathBuf}; use std::str::FromStr; use std::sync::atomic::{AtomicBool, Ordering}; -use anyhow::{anyhow, Error, Result}; +use anyhow::{anyhow, Context, Error, Result}; use chrono::{DateTime, TimeZone, Utc}; use colored::Colorize; use log::debug; @@ -334,13 +334,17 @@ fn maybe_load_history(rl: &mut Editor<()>, path: Option<&Path>) { } fn maybe_save_history(rl: &Editor<()>, path: Option<&Path>) { - if let Some(path) = path { + let run = |path: &Path| -> Result<()> { debug!("Saving history to {:?}", path); - let result: Result<(), Box<dyn std::error::Error>> = - fs::create_dir_all(path.parent().unwrap()) - .map_err(Into::into) - .and_then(|_| rl.save_history(path).map_err(Into::into)); - if let Err(e) = result { + let parent = path + .parent() + .ok_or(anyhow!("Path does not have a parent"))?; + fs::create_dir_all(parent).context("Could not create directory")?; + rl.save_history(path)?; + Ok(()) + }; + if let Some(path) = path { + if let Err(e) = run(path) { debug!("Saving the history failed: {}", e); } } |