Fix remove not throwing errors

This commit is contained in:
Ajeet D'Souza 2022-09-07 11:56:38 +05:30
parent e4aa0692a4
commit e8a1e11848
9 changed files with 24 additions and 22 deletions

View File

@ -4,10 +4,10 @@ use std::{env, io};
fn main() { fn main() {
let pkg_version = env!("CARGO_PKG_VERSION"); let pkg_version = env!("CARGO_PKG_VERSION");
let version = match env::var_os("PROFILE") { let version = match env::var_os("PROFILE") {
Some(profile) if profile == "release" => format!("v{}", pkg_version), Some(profile) if profile == "release" => format!("v{pkg_version}"),
_ => git_version().unwrap_or_else(|| format!("v{}-unknown", pkg_version)), _ => git_version().unwrap_or_else(|| format!("v{pkg_version}-unknown")),
}; };
println!("cargo:rustc-env=ZOXIDE_VERSION={}", version); println!("cargo:rustc-env=ZOXIDE_VERSION={version}");
// Since we are generating completions in the package directory, we need to set this so that // Since we are generating completions in the package directory, we need to set this so that
// Cargo doesn't rebuild every time. // Cargo doesn't rebuild every time.

View File

@ -27,7 +27,7 @@ impl Run for Add {
continue; continue;
} }
if !Path::new(path).is_dir() { if !Path::new(path).is_dir() {
bail!("not a directory: {}", path); bail!("not a directory: {path}");
} }
db.add(path, now); db.add(path, now);
} }

View File

@ -33,13 +33,13 @@ fn from_autojump<'a>(db: &mut Database<'a>, buffer: &'a str) -> Result<()> {
} }
let mut split = line.splitn(2, '\t'); let mut split = line.splitn(2, '\t');
let rank = split.next().with_context(|| format!("invalid entry: {}", line))?; let rank = split.next().with_context(|| format!("invalid entry: {line}"))?;
let mut rank = rank.parse::<f64>().with_context(|| format!("invalid rank: {}", rank))?; let mut rank = rank.parse::<f64>().with_context(|| format!("invalid rank: {rank}"))?;
// Normalize the rank using a sigmoid function. Don't import actual ranks from autojump, // Normalize the rank using a sigmoid function. Don't import actual ranks from autojump,
// since its scoring algorithm is very different and might take a while to get normalized. // since its scoring algorithm is very different and might take a while to get normalized.
rank = sigmoid(rank); rank = sigmoid(rank);
let path = split.next().with_context(|| format!("invalid entry: {}", line))?; let path = split.next().with_context(|| format!("invalid entry: {line}"))?;
db.dirs.push(Dir { path: path.into(), rank, last_accessed: 0 }); db.dirs.push(Dir { path: path.into(), rank, last_accessed: 0 });
db.modified = true; db.modified = true;
@ -59,13 +59,13 @@ fn from_z<'a>(db: &mut Database<'a>, buffer: &'a str) -> Result<()> {
} }
let mut split = line.rsplitn(3, '|'); let mut split = line.rsplitn(3, '|');
let last_accessed = split.next().with_context(|| format!("invalid entry: {}", line))?; let last_accessed = split.next().with_context(|| format!("invalid entry: {line}"))?;
let last_accessed = last_accessed.parse().with_context(|| format!("invalid epoch: {}", last_accessed))?; let last_accessed = last_accessed.parse().with_context(|| format!("invalid epoch: {last_accessed}"))?;
let rank = split.next().with_context(|| format!("invalid entry: {}", line))?; let rank = split.next().with_context(|| format!("invalid entry: {line}"))?;
let rank = rank.parse().with_context(|| format!("invalid rank: {}", rank))?; let rank = rank.parse().with_context(|| format!("invalid rank: {rank}"))?;
let path = split.next().with_context(|| format!("invalid entry: {}", line))?; let path = split.next().with_context(|| format!("invalid entry: {line}"))?;
db.dirs.push(Dir { path: path.into(), rank, last_accessed }); db.dirs.push(Dir { path: path.into(), rank, last_accessed });
db.modified = true; db.modified = true;

View File

@ -26,6 +26,6 @@ impl Run for Init {
InitShell::Zsh => shell::Zsh(opts).render(), InitShell::Zsh => shell::Zsh(opts).render(),
} }
.context("could not render template")?; .context("could not render template")?;
writeln!(io::stdout(), "{}", source).pipe_exit("stdout") writeln!(io::stdout(), "{source}").pipe_exit("stdout")
} }
} }

View File

@ -45,10 +45,10 @@ impl Query {
}; };
if self.score { if self.score {
print!("{}", selection); print!("{selection}");
} else { } else {
let path = selection.get(5..).context("could not read selection from fzf")?; let path = selection.get(5..).context("could not read selection from fzf")?;
print!("{}", path); print!("{path}");
} }
} else if self.list { } else if self.list {
let handle = &mut io::stdout().lock(); let handle = &mut io::stdout().lock();

View File

@ -34,7 +34,8 @@ impl Run for Remove {
let paths = selection.lines().filter_map(|line| line.get(5..)); let paths = selection.lines().filter_map(|line| line.get(5..));
for path in paths { for path in paths {
if !db.remove(path) { if !db.remove(path) {
bail!("path not found in database: {}", path); db.modified = false;
bail!("path not found in database: {path}");
} }
} }
} else { } else {
@ -42,8 +43,9 @@ impl Run for Remove {
if !db.remove(path) { if !db.remove(path) {
let path_abs = util::resolve_path(path)?; let path_abs = util::resolve_path(path)?;
let path_abs = util::path_to_str(&path_abs)?; let path_abs = util::path_to_str(&path_abs)?;
if path_abs != path && !db.remove(path_abs) { if path_abs == path || !db.remove(path_abs) {
bail!("path not found in database: {} ({})", path, path_abs) db.modified = false;
bail!("path not found in database: {path}")
} }
} }
} }

View File

@ -24,7 +24,7 @@ pub fn exclude_dirs() -> Result<Vec<Pattern>> {
Some(paths) => env::split_paths(&paths) Some(paths) => env::split_paths(&paths)
.map(|path| { .map(|path| {
let pattern = path.to_str().context("invalid unicode in _ZO_EXCLUDE_DIRS")?; let pattern = path.to_str().context("invalid unicode in _ZO_EXCLUDE_DIRS")?;
Pattern::new(pattern).with_context(|| format!("invalid glob in _ZO_EXCLUDE_DIRS: {}", pattern)) Pattern::new(pattern).with_context(|| format!("invalid glob in _ZO_EXCLUDE_DIRS: {pattern}"))
}) })
.collect(), .collect(),
None => { None => {
@ -46,7 +46,7 @@ pub fn maxage() -> Result<Rank> {
env::var_os("_ZO_MAXAGE").map_or(Ok(10_000.0), |maxage| { env::var_os("_ZO_MAXAGE").map_or(Ok(10_000.0), |maxage| {
let maxage = maxage.to_str().context("invalid unicode in _ZO_MAXAGE")?; let maxage = maxage.to_str().context("invalid unicode in _ZO_MAXAGE")?;
let maxage = let maxage =
maxage.parse::<u32>().with_context(|| format!("unable to parse _ZO_MAXAGE as integer: {}", maxage))?; maxage.parse::<u32>().with_context(|| format!("unable to parse _ZO_MAXAGE as integer: {maxage}"))?;
Ok(maxage as Rank) Ok(maxage as Rank)
}) })
} }

View File

@ -34,7 +34,7 @@ impl DirList<'_> {
match version { match version {
Self::VERSION => Ok(deserializer.deserialize(bytes_dirs)?), Self::VERSION => Ok(deserializer.deserialize(bytes_dirs)?),
version => { version => {
bail!("unsupported version (got {}, supports {})", version, Self::VERSION,) bail!("unsupported version (got {version}, supports {})", Self::VERSION)
} }
} }
})() })()

View File

@ -22,7 +22,7 @@ impl BrokenPipeHandler for io::Result<()> {
fn pipe_exit(self, device: &str) -> Result<()> { fn pipe_exit(self, device: &str) -> Result<()> {
match self { match self {
Err(e) if e.kind() == io::ErrorKind::BrokenPipe => bail!(SilentExit { code: 0 }), Err(e) if e.kind() == io::ErrorKind::BrokenPipe => bail!(SilentExit { code: 0 }),
result => result.with_context(|| format!("could not write to {}", device)), result => result.with_context(|| format!("could not write to {device}")),
} }
} }
} }