mirror of
https://github.com/denisidoro/navi
synced 2024-11-13 23:37:10 +00:00
Add UnreadableDir error.
This commit is contained in:
parent
11575a8443
commit
5d2c2aca80
3 changed files with 31 additions and 8 deletions
|
@ -1,4 +1,5 @@
|
|||
use crate::structures::error::filesystem::InvalidPath;
|
||||
use crate::structures::error::filesystem::UnreadableDir;
|
||||
use crate::structures::option::Config;
|
||||
use anyhow::Context;
|
||||
use anyhow::Error;
|
||||
|
@ -78,13 +79,13 @@ fn cheat_paths_from_config_dir() -> Result<String, Error> {
|
|||
.and_then(pathbuf_to_string)
|
||||
.and_then(|path| {
|
||||
fs::read_dir(path.clone())
|
||||
.with_context(|| format!("Unable to read directory `{}`", &path))
|
||||
.map_err(|e| UnreadableDir::new(path.clone(), e).into())
|
||||
.map(|entries| (path, entries))
|
||||
})
|
||||
.and_then(|(path, dir_entries)| {
|
||||
let mut paths_str = String::from("");
|
||||
for entry in dir_entries {
|
||||
let path = entry.with_context(|| format!("Unable to read directory `{}`", path))?;
|
||||
let path = entry.map_err(|e| UnreadableDir::new(path.clone(), e))?;
|
||||
paths_str.push_str(
|
||||
path.path()
|
||||
.into_os_string()
|
||||
|
|
|
@ -3,7 +3,10 @@ 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::filesystem::InvalidPath, option::Config};
|
||||
use crate::structures::{
|
||||
error::filesystem::{InvalidPath, UnreadableDir},
|
||||
option::Config,
|
||||
};
|
||||
use crate::welcome;
|
||||
use anyhow::{Context, Error};
|
||||
use regex::Regex;
|
||||
|
@ -224,13 +227,10 @@ pub fn read_all(
|
|||
let folders = paths_from_path_param(&paths);
|
||||
|
||||
for folder in folders {
|
||||
let dir_entries = fs::read_dir(folder)
|
||||
.with_context(|| format!("Unable to read directory `{}`", folder))?;
|
||||
let dir_entries = fs::read_dir(folder).map_err(|e| UnreadableDir::new(folder, e))?;
|
||||
|
||||
for entry in dir_entries {
|
||||
let path = entry
|
||||
.with_context(|| format!("Unable to read directory `{}`", folder))?
|
||||
.path();
|
||||
let path = entry.map_err(|e| UnreadableDir::new(folder, e))?.path();
|
||||
let path_str = path
|
||||
.to_str()
|
||||
.ok_or_else(|| InvalidPath(path.to_path_buf()))?;
|
||||
|
|
|
@ -1,5 +1,27 @@
|
|||
use std::{fmt::Debug, path::PathBuf};
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
#[error("Invalid path `{0}`")]
|
||||
pub struct InvalidPath(pub PathBuf);
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
#[error("Unable to read directory `{dir}`")]
|
||||
pub struct UnreadableDir {
|
||||
dir: PathBuf,
|
||||
#[source]
|
||||
source: anyhow::Error,
|
||||
}
|
||||
|
||||
impl UnreadableDir {
|
||||
pub fn new<DirT, SourceError>(dir: DirT, source: SourceError) -> Self
|
||||
where
|
||||
DirT: Into<PathBuf>,
|
||||
SourceError: std::error::Error + Sync + Send + 'static,
|
||||
{
|
||||
UnreadableDir {
|
||||
dir: dir.into(),
|
||||
source: source.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue