Restructure error module

This commit is contained in:
Csonka Mihaly 2020-03-22 20:47:13 +01:00
parent 4bbccc2cfa
commit 11575a8443
5 changed files with 13 additions and 14 deletions

View file

@ -1,4 +1,4 @@
use crate::structures::error::FilesystemError;
use crate::structures::error::filesystem::InvalidPath;
use crate::structures::option::Config;
use anyhow::Context;
use anyhow::Error;
@ -25,7 +25,7 @@ pub fn pathbuf_to_string(pathbuf: PathBuf) -> Result<String, Error> {
Ok(pathbuf
.as_os_str()
.to_str()
.ok_or_else(|| FilesystemError::InvalidPath(pathbuf.to_path_buf()))
.ok_or_else(|| InvalidPath(pathbuf.to_path_buf()))
.map(str::to_string)?)
}
@ -45,7 +45,7 @@ fn follow_symlink(pathbuf: PathBuf) -> Result<PathBuf, Error> {
let o_str = o
.as_os_str()
.to_str()
.ok_or_else(|| FilesystemError::InvalidPath(o.to_path_buf()))?;
.ok_or_else(|| InvalidPath(o.to_path_buf()))?;
if o_str.starts_with('.') {
let parent = pathbuf
.parent()
@ -53,7 +53,7 @@ fn follow_symlink(pathbuf: PathBuf) -> Result<PathBuf, Error> {
let parent_str = parent
.as_os_str()
.to_str()
.ok_or_else(|| FilesystemError::InvalidPath(parent.to_path_buf()))?;
.ok_or_else(|| 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<String, Error> {
path.path()
.into_os_string()
.to_str()
.ok_or_else(|| FilesystemError::InvalidPath(path.path()))?,
.ok_or_else(|| InvalidPath(path.path()))?,
);
paths_str.push_str(":");
}

View file

@ -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::{error::FilesystemError, option::Config};
use crate::structures::{error::filesystem::InvalidPath, 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(|| FilesystemError::InvalidPath(path.to_path_buf()))?;
.ok_or_else(|| 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

View file

@ -1,7 +0,0 @@
use std::{fmt::Debug, path::PathBuf};
use thiserror::Error;
#[derive(Error, Debug)]
pub enum FilesystemError {
#[error("Invalid path `{0}`")]
InvalidPath(PathBuf),
}

View file

@ -0,0 +1,5 @@
use std::{fmt::Debug, path::PathBuf};
use thiserror::Error;
#[derive(Error, Debug)]
#[error("Invalid path `{0}`")]
pub struct InvalidPath(pub PathBuf);

View file

@ -0,0 +1 @@
pub mod filesystem;