Style nits

This commit is contained in:
Ajeet D'Souza 2020-03-30 06:49:41 +05:30
parent ad96db844c
commit 9d0222383f
4 changed files with 21 additions and 19 deletions

View File

@ -13,14 +13,13 @@ pub const DB_VERSION: DBVersion = 3;
pub fn zo_data_dir() -> Result<PathBuf> { pub fn zo_data_dir() -> Result<PathBuf> {
let data_dir = match env::var_os("_ZO_DATA_DIR") { let data_dir = match env::var_os("_ZO_DATA_DIR") {
Some(data_osstr) => PathBuf::from(data_osstr), Some(data_osstr) => PathBuf::from(data_osstr),
None => { None => match dirs::data_local_dir() {
if let Some(mut data_dir) = dirs::data_local_dir() { Some(mut data_dir) => {
data_dir.push("zoxide"); data_dir.push("zoxide");
data_dir 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 // This will fail when `data_dir` points to a file or a broken symlink, but

View File

@ -16,6 +16,7 @@ pub struct DB {
data: DBData, data: DBData,
modified: bool, modified: bool,
path: PathBuf, path: PathBuf,
// FIXME: remove after next breaking version
path_old: Option<PathBuf>, path_old: Option<PathBuf>,
} }
@ -36,7 +37,7 @@ impl DB {
}; };
if data.version != config::DB_VERSION { if data.version != config::DB_VERSION {
bail!("this database version ({}) is unsupported", data.version); bail!("database version '{}' is unsupported", data.version);
} }
Ok(DB { Ok(DB {
@ -47,6 +48,7 @@ impl DB {
}) })
} }
// FIXME: remove after next breaking version
pub fn open_and_migrate<P1, P2>(path_old: P1, path: P2) -> Result<DB> pub fn open_and_migrate<P1, P2>(path_old: P1, path: P2) -> Result<DB>
where where
P1: AsRef<Path>, P1: AsRef<Path>,
@ -79,7 +81,6 @@ impl DB {
let file_tmp = OpenOptions::new() let file_tmp = OpenOptions::new()
.write(true) .write(true)
// ensure that we are not overwriting an existing file_tmp
.create_new(true) .create_new(true)
.open(&path_tmp) .open(&path_tmp)
.context("could not open temporary database file")?; .context("could not open temporary database file")?;
@ -280,6 +281,7 @@ impl Drop for DB {
if let Err(e) = self.save() { if let Err(e) = self.save() {
eprintln!("{:#}", e); eprintln!("{:#}", e);
} else if let Some(path_old) = &self.path_old { } 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") { if let Err(e) = fs::remove_file(path_old).context("could not remove old database") {
eprintln!("{:#}", e); eprintln!("{:#}", e);
} }

View File

@ -49,14 +49,14 @@ impl Query {
*keyword = keyword.to_lowercase(); *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 // `path_to_bytes` is guaranteed to succeed here since
// the path has already been queried successfully // the path has already been queried successfully
let path_bytes = util::path_to_bytes(&dir.path).unwrap(); let path_bytes = util::path_to_bytes(&dir.path).unwrap();
Ok(Some(path_bytes.to_vec())) path_bytes.to_vec()
} else { });
Ok(None)
} Ok(path_opt)
} }
fn query_interactive(&mut self) -> Result<Option<Vec<u8>>> { fn query_interactive(&mut self) -> Result<Option<Vec<u8>>> {

View File

@ -13,6 +13,7 @@ use std::time::SystemTime;
#[cfg(unix)] #[cfg(unix)]
pub fn path_to_bytes<P: AsRef<Path>>(path: &P) -> Option<&[u8]> { pub fn path_to_bytes<P: AsRef<Path>>(path: &P) -> Option<&[u8]> {
use std::os::unix::ffi::OsStrExt; use std::os::unix::ffi::OsStrExt;
Some(path.as_ref().as_os_str().as_bytes()) Some(path.as_ref().as_os_str().as_bytes())
} }
@ -123,12 +124,12 @@ pub fn fzf_helper(now: Epoch, mut dirs: Vec<Dir>) -> Result<Option<Vec<u8>>> {
#[inline] #[inline]
pub fn clamp(val: f64, min: f64, max: f64) -> f64 { pub fn clamp(val: f64, min: f64, max: f64) -> f64 {
assert!(min <= max); assert!(min <= max);
let mut x = val;
if x < min { if val < min {
x = min; min
} else if val > max {
max
} else {
val
} }
if x > max {
x = max;
}
x
} }