-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use wrapped error that includes path
- Loading branch information
Showing
3 changed files
with
89 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "async-walkdir" | ||
version = "1.0.0" | ||
version = "1.1.0" | ||
authors = ["Ririsoft <[email protected]>"] | ||
edition = "2021" | ||
description = "Asynchronous directory traversal for Rust." | ||
|
@@ -18,4 +18,4 @@ futures-lite = "2.1.0" | |
async-fs = "2.1.0" | ||
|
||
[dev-dependencies] | ||
tempfile = "3.9.0" | ||
tempfile = "3.9.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use std::{ | ||
error, fmt, io, ops::Deref, path::{Path, PathBuf} | ||
}; | ||
|
||
/// A wrapper around [`io::Error`] that includes the associated path. | ||
#[derive(Debug)] | ||
pub struct Error { | ||
path: PathBuf, | ||
inner: io::Error, | ||
} | ||
|
||
impl Error { | ||
/// Create a new [`Error`] | ||
pub fn new(path: PathBuf, inner: io::Error) -> Self { | ||
Error { path, inner } | ||
} | ||
|
||
/// Returns the path associated with this error. | ||
pub fn path(&self) -> &Path { | ||
&self.path | ||
} | ||
} | ||
|
||
impl Deref for Error { | ||
type Target = io::Error; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.inner | ||
} | ||
} | ||
|
||
impl error::Error for Error { | ||
#[allow(deprecated)] | ||
fn description(&self) -> &str { | ||
self.inner.description() | ||
} | ||
|
||
fn cause(&self) -> Option<&dyn error::Error> { | ||
self.source() | ||
} | ||
|
||
fn source(&self) -> Option<&(dyn error::Error + 'static)> { | ||
Some(&self.inner) | ||
} | ||
} | ||
|
||
impl fmt::Display for Error { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!( | ||
f, | ||
"IO error for operation on {}: {}", | ||
self.path.display(), | ||
self.inner | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters