From 4bbccc2cfa642964c707ef7477ff61297a52158e Mon Sep 17 00:00:00 2001 From: Csonka Mihaly Date: Sun, 22 Mar 2020 20:14:10 +0100 Subject: [PATCH] Add InvalidPath error --- src/filesystem.rs | 20 ++++++++++---------- src/parser.rs | 4 ++-- src/structures/error.rs | 7 +++++++ src/structures/mod.rs | 1 + 4 files changed, 20 insertions(+), 12 deletions(-) create mode 100644 src/structures/error.rs diff --git a/src/filesystem.rs b/src/filesystem.rs index c463b07..d3806c1 100644 --- a/src/filesystem.rs +++ b/src/filesystem.rs @@ -1,3 +1,4 @@ +use crate::structures::error::FilesystemError; use crate::structures::option::Config; use anyhow::Context; use anyhow::Error; @@ -21,11 +22,11 @@ where } pub fn pathbuf_to_string(pathbuf: PathBuf) -> Result { - pathbuf + Ok(pathbuf .as_os_str() .to_str() - .ok_or_else(|| anyhow!("Invalid path `{}`", pathbuf.display())) - .map(str::to_string) + .ok_or_else(|| FilesystemError::InvalidPath(pathbuf.to_path_buf())) + .map(str::to_string)?) } pub fn cheat_pathbuf() -> Result { @@ -44,16 +45,15 @@ fn follow_symlink(pathbuf: PathBuf) -> Result { let o_str = o .as_os_str() .to_str() - .ok_or_else(|| anyhow!("Invalid path `{}`", o.display()))?; + .ok_or_else(|| FilesystemError::InvalidPath(o.to_path_buf()))?; if o_str.starts_with('.') { - let parent_str = pathbuf + let parent = pathbuf .parent() - .ok_or_else(|| anyhow!("`{}` has no parent", pathbuf.display()))? + .ok_or_else(|| anyhow!("`{}` has no parent", pathbuf.display()))?; + let parent_str = parent .as_os_str() .to_str() - .ok_or_else(|| { - anyhow!("Parent of `{}` is an invalid path", pathbuf.display()) - })?; + .ok_or_else(|| FilesystemError::InvalidPath(parent.to_path_buf()))?; let path_str = format!("{}/{}", parent_str, o_str); let p = PathBuf::from(path_str); follow_symlink(p) @@ -89,7 +89,7 @@ fn cheat_paths_from_config_dir() -> Result { path.path() .into_os_string() .to_str() - .ok_or_else(|| anyhow!("Invalid path `{}`", path.path().display()))?, + .ok_or_else(|| FilesystemError::InvalidPath(path.path()))?, ); paths_str.push_str(":"); } diff --git a/src/parser.rs b/src/parser.rs index 6c3d80d..782ef1b 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -3,7 +3,7 @@ use crate::filesystem; use crate::structures::cheat::VariableMap; use crate::structures::fnv::HashLine; use crate::structures::fzf::{Opts as FzfOpts, SuggestionType}; -use crate::structures::option::Config; +use crate::structures::{error::FilesystemError, option::Config}; use crate::welcome; use anyhow::{Context, Error}; use regex::Regex; @@ -233,7 +233,7 @@ pub fn read_all( .path(); let path_str = path .to_str() - .ok_or_else(|| anyhow!("Invalid path `{}`", path.display()))?; + .ok_or_else(|| FilesystemError::InvalidPath(path.to_path_buf()))?; if path_str.ends_with(".cheat") && read_file(path_str, &mut variables, &mut visited_lines, stdin).is_ok() && !found_something diff --git a/src/structures/error.rs b/src/structures/error.rs new file mode 100644 index 0000000..d181778 --- /dev/null +++ b/src/structures/error.rs @@ -0,0 +1,7 @@ +use std::{fmt::Debug, path::PathBuf}; +use thiserror::Error; +#[derive(Error, Debug)] +pub enum FilesystemError { + #[error("Invalid path `{0}`")] + InvalidPath(PathBuf), +} diff --git a/src/structures/mod.rs b/src/structures/mod.rs index 801ec0c..b3cb886 100644 --- a/src/structures/mod.rs +++ b/src/structures/mod.rs @@ -1,4 +1,5 @@ pub mod cheat; +pub mod error; pub mod fnv; pub mod fzf; pub mod option;