diff --git a/src/config.rs b/src/config.rs index 2376fff..61ca135 100644 --- a/src/config.rs +++ b/src/config.rs @@ -13,14 +13,13 @@ pub const DB_VERSION: DBVersion = 3; pub fn zo_data_dir() -> Result { let data_dir = match env::var_os("_ZO_DATA_DIR") { Some(data_osstr) => PathBuf::from(data_osstr), - None => { - if let Some(mut data_dir) = dirs::data_local_dir() { + None => match dirs::data_local_dir() { + Some(mut data_dir) => { data_dir.push("zoxide"); data_dir - } else { - bail!("could not find database directory, please set _ZO_DATA_DIR manually"); } - } + None => bail!("could not find database directory, please set _ZO_DATA_DIR manually"), + }, }; // This will fail when `data_dir` points to a file or a broken symlink, but diff --git a/src/db.rs b/src/db.rs index e8f9e7e..9e7c126 100644 --- a/src/db.rs +++ b/src/db.rs @@ -16,6 +16,7 @@ pub struct DB { data: DBData, modified: bool, path: PathBuf, + // FIXME: remove after next breaking version path_old: Option, } @@ -36,7 +37,7 @@ impl DB { }; if data.version != config::DB_VERSION { - bail!("this database version ({}) is unsupported", data.version); + bail!("database version '{}' is unsupported", data.version); } Ok(DB { @@ -47,6 +48,7 @@ impl DB { }) } + // FIXME: remove after next breaking version pub fn open_and_migrate(path_old: P1, path: P2) -> Result where P1: AsRef, @@ -79,7 +81,6 @@ impl DB { let file_tmp = OpenOptions::new() .write(true) - // ensure that we are not overwriting an existing file_tmp .create_new(true) .open(&path_tmp) .context("could not open temporary database file")?; @@ -280,6 +281,7 @@ impl Drop for DB { if let Err(e) = self.save() { eprintln!("{:#}", e); } else if let Some(path_old) = &self.path_old { + // FIXME: remove this branch after next breaking release if let Err(e) = fs::remove_file(path_old).context("could not remove old database") { eprintln!("{:#}", e); } diff --git a/src/subcommand/query.rs b/src/subcommand/query.rs index cba97d1..187544c 100644 --- a/src/subcommand/query.rs +++ b/src/subcommand/query.rs @@ -49,14 +49,14 @@ impl Query { *keyword = keyword.to_lowercase(); } - if let Some(dir) = util::get_db()?.query(&self.keywords, now) { + let path_opt = util::get_db()?.query(&self.keywords, now).map(|dir| { // `path_to_bytes` is guaranteed to succeed here since // the path has already been queried successfully let path_bytes = util::path_to_bytes(&dir.path).unwrap(); - Ok(Some(path_bytes.to_vec())) - } else { - Ok(None) - } + path_bytes.to_vec() + }); + + Ok(path_opt) } fn query_interactive(&mut self) -> Result>> { diff --git a/src/util.rs b/src/util.rs index 80dcccd..74614d5 100644 --- a/src/util.rs +++ b/src/util.rs @@ -13,6 +13,7 @@ use std::time::SystemTime; #[cfg(unix)] pub fn path_to_bytes>(path: &P) -> Option<&[u8]> { use std::os::unix::ffi::OsStrExt; + Some(path.as_ref().as_os_str().as_bytes()) } @@ -123,12 +124,12 @@ pub fn fzf_helper(now: Epoch, mut dirs: Vec) -> Result>> { #[inline] pub fn clamp(val: f64, min: f64, max: f64) -> f64 { assert!(min <= max); - let mut x = val; - if x < min { - x = min; + + if val < min { + min + } else if val > max { + max + } else { + val } - if x > max { - x = max; - } - x }