From dad0f60b28434fa933ec14d3c67c1a624e712566 Mon Sep 17 00:00:00 2001 From: Ajeet D'Souza <98ajeet@gmail.com> Date: Sat, 16 May 2020 17:59:21 +0530 Subject: [PATCH] Use util::path_to_str for path conversions --- src/subcommand/add.rs | 14 +++++--------- src/subcommand/import.rs | 8 +++----- src/subcommand/init/shell/posix.rs | 13 ++++--------- src/subcommand/remove.rs | 13 +++++-------- src/util.rs | 8 ++++++++ 5 files changed, 25 insertions(+), 31 deletions(-) diff --git a/src/subcommand/add.rs b/src/subcommand/add.rs index 37ce51f..c648428 100644 --- a/src/subcommand/add.rs +++ b/src/subcommand/add.rs @@ -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(¤t_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()?; diff --git a/src/subcommand/import.rs b/src/subcommand/import.rs index a3f6f27..9c588e8 100644 --- a/src/subcommand/import.rs +++ b/src/subcommand/import.rs @@ -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>(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. diff --git a/src/subcommand/init/shell/posix.rs b/src/subcommand/init/shell/posix.rs index 7a8f90c..a457e30 100644 --- a/src/subcommand/init/shell/posix.rs +++ b/src/subcommand/init/shell/posix.rs @@ -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> { 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#" diff --git a/src/subcommand/remove.rs b/src/subcommand/remove.rs index 6d087c1..a412515 100644 --- a/src/subcommand/remove.rs +++ b/src/subcommand/remove.rs @@ -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() diff --git a/src/util.rs b/src/util.rs index 32e6216..fe9eb65 100644 --- a/src/util.rs +++ b/src/util.rs @@ -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 { @@ -18,3 +19,10 @@ pub fn get_current_time() -> Result { Ok(current_time as Epoch) } + +pub fn path_to_str>(path: &P) -> Result<&str> { + let path = path.as_ref(); + + path.to_str() + .with_context(|| format!("invalid utf-8 sequence in path: {}", path.display())) +}