Skip to content

Commit

Permalink
Implement Path::try_exists
Browse files Browse the repository at this point in the history
Implements the `Path::try_exists` associated function mentioned in #46. The aliased function was stabilized in 1.63 so to preserve compatibility with older Rust versions (ci targets 1.40) a new feature flag was added.
  • Loading branch information
schneems committed Oct 17, 2023
1 parent 9f3feb6 commit 72d2d51
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ jobs:
args: --tests -- -D warnings
if: matrix.rust_version == 'stable'

- name: cargo check --features io_safety
- name: cargo check --features "io_safety path_try_exists"
uses: actions-rs/cargo@v1
with:
command: clippy
args: --features io_safety
args: --features "io_safety path_try_exists"
if: matrix.rust_version == 'stable'
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# fs-err Changelog

* Add `fs_err_try_exists` to `std::path::Path` via extension trait. This feature requires Rust 1.63 or later and is gated behind the `path_try_exists` feature flag. ([]())

## 2.9.0

* Add wrappers for [`tokio::fs`](https://docs.rs/tokio/latest/tokio/fs/index.html) ([#40](https://github.com/andrewhickman/fs-err/pull/40)).
Expand Down
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ tokio = { version = "1.21", optional = true, default_features = false, features
serde_json = "1.0.64"

[features]
# Adds I/O safety traits introduced in Rust 1.63
# Adds `fs_err_try_exists` to `Path`, introduced in Rust 1.6.3
path_try_exists = []
# Adds I/O safety traits, introduced in Rust 1.63
io_safety = []

[package.metadata.release]
Expand Down
3 changes: 3 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub(crate) enum ErrorKind {
Canonicalize,
ReadLink,
SymlinkMetadata,
#[allow(dead_code)]
FileExists,

#[cfg(windows)]
SeekRead,
Expand Down Expand Up @@ -84,6 +86,7 @@ impl fmt::Display for Error {
Canonicalize => write!(formatter, "failed to canonicalize path `{}`", path),
ReadLink => write!(formatter, "failed to read symbolic link `{}`", path),
SymlinkMetadata => write!(formatter, "failed to query metadata of symlink `{}`", path),
FileExists => write!(formatter, "failed to check file existance `{}`", path),

#[cfg(windows)]
SeekRead => write!(formatter, "failed to seek and read from `{}`", path),
Expand Down
11 changes: 11 additions & 0 deletions src/path.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[allow(unused_imports)]
use crate::errors::{Error, ErrorKind};
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
Expand All @@ -8,6 +10,9 @@ use std::path::{Path, PathBuf};
//
// Because no one else can implement it, we can add methods backwards-compatibly.
pub trait PathExt: crate::Sealed {
/// Wrapper for [`Path::try_exists`](https://doc.rust-lang.org/std/path/struct.Path.html#method.try_exists).
#[cfg(feature = "path_try_exists")]
fn fs_err_try_exists(&self) -> io::Result<bool>;
/// Wrapper for [`crate::metadata`].
fn fs_err_metadata(&self) -> io::Result<fs::Metadata>;
/// Wrapper for [`crate::symlink_metadata`].
Expand All @@ -21,6 +26,12 @@ pub trait PathExt: crate::Sealed {
}

impl PathExt for Path {
#[cfg(feature = "path_try_exists")]
fn fs_err_try_exists(&self) -> io::Result<bool> {
self.try_exists()
.map_err(|source| Error::build(source, ErrorKind::FileExists, self))
}

fn fs_err_metadata(&self) -> io::Result<fs::Metadata> {
crate::metadata(self)
}
Expand Down

0 comments on commit 72d2d51

Please sign in to comment.