mirror of
https://github.com/Llewellynvdm/zoxide.git
synced 2024-12-29 12:32:40 +00:00
Style nits
This commit is contained in:
parent
ad96db844c
commit
9d0222383f
@ -13,14 +13,13 @@ pub const DB_VERSION: DBVersion = 3;
|
||||
pub fn zo_data_dir() -> Result<PathBuf> {
|
||||
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
|
||||
|
@ -16,6 +16,7 @@ pub struct DB {
|
||||
data: DBData,
|
||||
modified: bool,
|
||||
path: PathBuf,
|
||||
// FIXME: remove after next breaking version
|
||||
path_old: Option<PathBuf>,
|
||||
}
|
||||
|
||||
@ -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<P1, P2>(path_old: P1, path: P2) -> Result<DB>
|
||||
where
|
||||
P1: AsRef<Path>,
|
||||
@ -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);
|
||||
}
|
||||
|
@ -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<Option<Vec<u8>>> {
|
||||
|
15
src/util.rs
15
src/util.rs
@ -13,6 +13,7 @@ use std::time::SystemTime;
|
||||
#[cfg(unix)]
|
||||
pub fn path_to_bytes<P: AsRef<Path>>(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<Dir>) -> Result<Option<Vec<u8>>> {
|
||||
#[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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user