Skip to content

Commit

Permalink
feat!: improve error handling with thiserror
Browse files Browse the repository at this point in the history
  • Loading branch information
brglng committed Aug 9, 2024
1 parent d8c03ae commit 90be573
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 48 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
[package]
name = "bfind"
edition = "2021"
version = "0.0.1"
authors = ["Zhaosheng Pan <[email protected]"]

[dependencies]
tempfile = "3"
thiserror = "1"
14 changes: 13 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,23 @@ use std::collections::{HashSet, VecDeque};
use std::fs;
use std::path::{Path, PathBuf};
use std::process::exit;
use thiserror::Error;

mod path_queue;
use path_queue::PathQueue;

fn breadth_first_traverse(prog: &str, roots: Vec<String>, allow_hidden: bool, follow_links: bool, ignores: &HashSet<String>) -> path_queue::Result<()> {
#[derive(Error, Debug)]
enum Error {
#[error("path_queue::Error: {source}")]
PathQueue {
#[from]
source: path_queue::Error
}
}

type Result<T> = std::result::Result<T, Error>;

fn breadth_first_traverse(prog: &str, roots: Vec<String>, allow_hidden: bool, follow_links: bool, ignores: &HashSet<String>) -> Result<()> {
let dotdir = Path::new(".");
let dotdotdir = Path::new("..");

Expand Down
68 changes: 21 additions & 47 deletions src/path_queue.rs
Original file line number Diff line number Diff line change
@@ -1,62 +1,36 @@
use std::error;
use std::ffi::OsStr;
use std::fmt;
use std::io;
use std::io::{BufRead, BufReader, BufWriter, Write};
use std::fs::File;
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<T> = std::result::Result<T, Error>;

#[derive(Debug)]
#[derive(Error, Debug)]
pub enum Error {
IoError(io::Error),
RecvError(mpsc::RecvError),
SendError(mpsc::SendError<PathBuf>),
}

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<PathBuf>: {source}")]
Send {
#[from]
source: mpsc::SendError<PathBuf>
},
}

impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Self::IoError(err)
}
}

impl From<mpsc::RecvError> for Error {
fn from(err: mpsc::RecvError) -> Self {
Self::RecvError(err)
}
}

impl From<mpsc::SendError<PathBuf>> for Error {
fn from(err: mpsc::SendError<PathBuf>) -> Self {
Self::SendError(err)
}
}
pub type Result<T> = std::result::Result<T, Error>;

struct TempfilePathQueue {
writer: BufWriter<File>,
Expand Down

0 comments on commit 90be573

Please sign in to comment.