Skip to content

Commit

Permalink
Add FromPathError::into_io_error and FromPathBufError::into_io_error
Browse files Browse the repository at this point in the history
These convenience methods are useful surprisingly often, in my
experience.
  • Loading branch information
sunshowers committed Jan 8, 2023
1 parent ca15d05 commit 636071f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.1.2] - 2022-08-12

### Added

- Added convenience methods [`FromPathBufError::into_io_error`] and
[`FromPathError::into_io_error`].

## [1.1.1] - 2022-08-12

### Fixed
Expand Down Expand Up @@ -101,6 +108,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

Initial release.

[1.1.2]: https://github.com/camino-rs/camino/releases/tag/camino-1.1.2
[1.1.1]: https://github.com/camino-rs/camino/releases/tag/camino-1.1.1
[1.1.0]: https://github.com/camino-rs/camino/releases/tag/camino-1.1.0
[1.0.9]: https://github.com/camino-rs/camino/releases/tag/camino-1.0.9
Expand Down
38 changes: 29 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1175,10 +1175,8 @@ impl Utf8Path {
/// assert_eq!(path.canonicalize_utf8().unwrap(), Utf8PathBuf::from("/foo/test/bar.rs"));
/// ```
pub fn canonicalize_utf8(&self) -> io::Result<Utf8PathBuf> {
self.canonicalize().and_then(|path| {
path.try_into()
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))
})
self.canonicalize()
.and_then(|path| path.try_into().map_err(FromPathBufError::into_io_error))
}

/// Reads a symbolic link, returning the file that the link points to.
Expand Down Expand Up @@ -1224,10 +1222,8 @@ impl Utf8Path {
/// let path_link = path.read_link_utf8().expect("read_link call failed");
/// ```
pub fn read_link_utf8(&self) -> io::Result<Utf8PathBuf> {
self.read_link().and_then(|path| {
path.try_into()
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))
})
self.read_link()
.and_then(|path| path.try_into().map_err(FromPathBufError::into_io_error))
}

/// Returns an iterator over the entries within a directory.
Expand Down Expand Up @@ -2488,13 +2484,24 @@ impl FromPathBufError {
self.path
}

/// Fetch a [`FromPathError`] for more about the conversion failure.
/// Fetches a [`FromPathError`] for more about the conversion failure.
///
/// At the moment this struct does not contain any additional information, but is provided for
/// completeness.
pub fn from_path_error(&self) -> FromPathError {
self.error
}

/// Converts self into a [`std::io::Error`] with kind
/// [`InvalidData`](io::ErrorKind::InvalidData).
///
/// Many users of `FromPathBufError` will want to convert it into an `io::Error`. This is a
/// convenience method to do that.
pub fn into_io_error(self) -> io::Error {
// NOTE: we don't currently implement `From<FromPathBufError> for io::Error` because we want
// to ensure the user actually desires that conversion.
io::Error::new(io::ErrorKind::InvalidData, self)
}
}

impl fmt::Display for FromPathBufError {
Expand Down Expand Up @@ -2539,6 +2546,19 @@ impl error::Error for FromPathBufError {
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct FromPathError(());

impl FromPathError {
/// Converts self into a [`std::io::Error`] with kind
/// [`InvalidData`](io::ErrorKind::InvalidData).
///
/// Many users of `FromPathError` will want to convert it into an `io::Error`. This is a
/// convenience method to do that.
pub fn into_io_error(self) -> io::Error {
// NOTE: we don't currently implement `From<FromPathBufError> for io::Error` because we want
// to ensure the user actually desires that conversion.
io::Error::new(io::ErrorKind::InvalidData, self)
}
}

impl fmt::Display for FromPathError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Path contains invalid UTF-8")
Expand Down

0 comments on commit 636071f

Please sign in to comment.