Use util::path_to_str for path conversions

This commit is contained in:
Ajeet D'Souza 2020-05-16 17:59:21 +05:30
parent 208a6a9eb8
commit dad0f60b28
5 changed files with 25 additions and 31 deletions

View File

@ -1,6 +1,6 @@
use crate::config;
use crate::db::{Dir, Rank};
use crate::util;
use crate::util::{get_current_time, get_db, path_to_str};
use anyhow::{Context, Result};
use structopt::StructOpt;
@ -20,9 +20,7 @@ impl Add {
Some(path) => path,
None => {
current_dir = env::current_dir().context("unable to fetch current directory")?;
current_dir.to_str().with_context(|| {
format!("invalid utf-8 sequence in path: {}", current_dir.display())
})?
path_to_str(&current_dir)?
}
};
@ -42,12 +40,10 @@ fn add(path: &str) -> Result<()> {
return Ok(());
}
let path_abs_str = path_abs
.to_str()
.with_context(|| format!("invalid utf-8 sequence in path: {}", path_abs.display()))?;
let path_abs_str = path_to_str(&path_abs)?;
let mut db = util::get_db()?;
let now = util::get_current_time()?;
let mut db = get_db()?;
let now = get_current_time()?;
let maxage = config::zo_maxage()?;

View File

@ -1,5 +1,5 @@
use crate::db::{Db, Dir};
use crate::util;
use crate::util::{get_db, path_to_str};
use anyhow::{bail, Context, Result};
use structopt::StructOpt;
@ -23,7 +23,7 @@ impl Import {
}
fn import<P: AsRef<Path>>(path: P, merge: bool) -> Result<()> {
let mut db = util::get_db()?;
let mut db = get_db()?;
if !db.dirs.is_empty() && !merge {
bail!(
@ -70,9 +70,7 @@ fn import_line(db: &mut Db, line: &str) -> Result<()> {
let path_abs = dunce::canonicalize(path_str)
.with_context(|| format!("could not resolve path: {}", path_str))?;
let path_abs_str = path_abs
.to_str()
.with_context(|| format!("invalid utf-8 sequence in path: {}", path_abs.display()))?;
let path_abs_str = path_to_str(&path_abs)?;
// If the path exists in the database, add the ranks and set the epoch to
// the largest of the parsed epoch and the already present epoch.

View File

@ -1,6 +1,7 @@
use super::{HookConfig, ShellConfig};
use crate::util::path_to_str;
use anyhow::{Context, Result};
use anyhow::Result;
use uuid::Uuid;
use std::borrow::Cow;
@ -80,16 +81,10 @@ esac
fn hook_pwd() -> Result<Cow<'static, str>> {
let mut tmp_path = std::env::temp_dir();
tmp_path.push("zoxide");
let tmp_path_str = tmp_path
.to_str()
.context("invalid utf-8 sequence in zoxide tmp path")?;
let tmp_path_str = path_to_str(&tmp_path)?;
let pwd_path = tmp_path.join(format!("pwd-{}", Uuid::new_v4()));
let pwd_path_str = pwd_path
.to_str()
.context("invalid utf-8 sequence in zoxide pwd path")?;
let pwd_path_str = path_to_str(&pwd_path)?;
let hook_pwd = format!(
r#"

View File

@ -1,5 +1,5 @@
use crate::fzf::Fzf;
use crate::util;
use crate::util::{get_current_time, get_db, path_to_str};
use anyhow::{bail, Context, Result};
use structopt::StructOpt;
@ -34,7 +34,7 @@ impl Remove {
}
fn remove(path: &str) -> Result<()> {
let mut db = util::get_db()?;
let mut db = get_db()?;
if let Some(idx) = db.dirs.iter().position(|dir| &dir.path == path) {
db.dirs.swap_remove(idx);
@ -44,10 +44,7 @@ fn remove(path: &str) -> Result<()> {
let path_abs =
dunce::canonicalize(path).with_context(|| format!("could not resolve path: {}", path))?;
let path_abs_str = path_abs
.to_str()
.with_context(|| format!("invalid utf-8 sequence in path: {}", path_abs.display()))?;
let path_abs_str = path_to_str(&path_abs)?;
if let Some(idx) = db.dirs.iter().position(|dir| dir.path == path_abs_str) {
db.dirs.swap_remove(idx);
@ -59,8 +56,8 @@ fn remove(path: &str) -> Result<()> {
}
fn remove_interactive(keywords: &[String]) -> Result<()> {
let mut db = util::get_db()?;
let now = util::get_current_time()?;
let mut db = get_db()?;
let now = get_current_time()?;
let keywords = keywords
.iter()

View File

@ -3,6 +3,7 @@ use crate::db::{Db, Epoch};
use anyhow::{Context, Result};
use std::path::Path;
use std::time::SystemTime;
pub fn get_db() -> Result<Db> {
@ -18,3 +19,10 @@ pub fn get_current_time() -> Result<Epoch> {
Ok(current_time as Epoch)
}
pub fn path_to_str<P: AsRef<Path>>(path: &P) -> Result<&str> {
let path = path.as_ref();
path.to_str()
.with_context(|| format!("invalid utf-8 sequence in path: {}", path.display()))
}