diff --git a/Cargo.toml b/Cargo.toml index f12e6fe..695edfd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,9 @@ [package] name = "bfind" +edition = "2021" version = "0.0.1" authors = ["Zhaosheng Pan , allow_hidden: bool, follow_links: bool, ignores: &HashSet) -> path_queue::Result<()> { +#[derive(Error, Debug)] +enum Error { + #[error("path_queue::Error: {source}")] + PathQueue { + #[from] + source: path_queue::Error + } +} + +type Result = std::result::Result; + +fn breadth_first_traverse(prog: &str, roots: Vec, allow_hidden: bool, follow_links: bool, ignores: &HashSet) -> Result<()> { let dotdir = Path::new("."); let dotdotdir = Path::new(".."); diff --git a/src/path_queue.rs b/src/path_queue.rs index a19d283..f44ab4b 100644 --- a/src/path_queue.rs +++ b/src/path_queue.rs @@ -1,6 +1,4 @@ -use std::error; use std::ffi::OsStr; -use std::fmt; use std::io; use std::io::{BufRead, BufReader, BufWriter, Write}; use std::fs::File; @@ -8,55 +6,31 @@ use std::os::unix::ffi::OsStrExt; use std::path::PathBuf; use std::sync::mpsc; use std::sync::mpsc::{Sender, Receiver}; -extern crate tempfile; -use self::tempfile::NamedTempFile; +use tempfile::NamedTempFile; +use thiserror::Error; -pub type Result = std::result::Result; - -#[derive(Debug)] +#[derive(Error, Debug)] pub enum Error { - IoError(io::Error), - RecvError(mpsc::RecvError), - SendError(mpsc::SendError), -} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - Self::IoError(ref e) => e.fmt(f), - Self::RecvError(ref e) => e.fmt(f), - Self::SendError(ref e) => e.fmt(f), - } - } -} - -impl error::Error for Error { - fn source(&self) -> Option<&(dyn error::Error + 'static)> { - match *self { - Self::IoError(ref e) => Some(e), - Self::RecvError(ref e) => Some(e), - Self::SendError(ref e) => Some(e), - } - } + #[error("std::io::Error {{ kind = {} }}: {source}", source.kind())] + Io { + #[from] + source: io::Error + }, + + #[error("mpsc::RecvError: {source}")] + Recv { + #[from] + source: mpsc::RecvError + }, + + #[error("mpsc::SendError: {source}")] + Send { + #[from] + source: mpsc::SendError + }, } -impl From for Error { - fn from(err: io::Error) -> Self { - Self::IoError(err) - } -} - -impl From for Error { - fn from(err: mpsc::RecvError) -> Self { - Self::RecvError(err) - } -} - -impl From> for Error { - fn from(err: mpsc::SendError) -> Self { - Self::SendError(err) - } -} +pub type Result = std::result::Result; struct TempfilePathQueue { writer: BufWriter,