Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add aliases to std::path::Path via extension trait #26

Merged
merged 1 commit into from
Sep 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ println!("Program config: {:?}", decoded);
mod dir;
mod errors;
mod file;
mod path;

use std::fs;
use std::io::{self, Read, Write};
Expand All @@ -81,6 +82,7 @@ use errors::{Error, ErrorKind, SourceDestError, SourceDestErrorKind};

pub use dir::*;
pub use file::*;
pub use path::PathExt;

/// Wrapper for [`fs::read`](https://doc.rust-lang.org/stable/std/fs/fn.read.html).
pub fn read<P: AsRef<Path> + Into<PathBuf>>(path: P) -> io::Result<Vec<u8>> {
Expand Down
49 changes: 49 additions & 0 deletions src/path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use std::fs;
use std::io;
use std::path::{Path, PathBuf};

/// Defines aliases on [`Path`](https://doc.rust-lang.org/std/path/struct.Path.html) for `fs_err` functions.
///
/// This trait is sealed and can not be implemented by other crates.
//
// Because noone else can implement it, we can add methods backwards-compatibly.
pub trait PathExt: private::Sealed {
/// Wrapper for [`crate::metadata`].
fn fs_err_metadata(&self) -> io::Result<fs::Metadata>;
/// Wrapper for [`crate::symlink_metadata`].
fn fs_err_symlink_metadata(&self) -> io::Result<fs::Metadata>;
/// Wrapper for [`crate::canonicalize`].
fn fs_err_canonicalize(&self) -> io::Result<PathBuf>;
/// Wrapper for [`crate::read_link`].
fn fs_err_read_link(&self) -> io::Result<PathBuf>;
/// Wrapper for [`crate::read_dir`].
fn fs_err_read_dir(&self) -> io::Result<crate::ReadDir>;
}

impl PathExt for Path {
fn fs_err_metadata(&self) -> io::Result<fs::Metadata> {
crate::metadata(self)
}

fn fs_err_symlink_metadata(&self) -> io::Result<fs::Metadata> {
crate::symlink_metadata(self)
}

fn fs_err_canonicalize(&self) -> io::Result<PathBuf> {
crate::canonicalize(self)
}

fn fs_err_read_link(&self) -> io::Result<PathBuf> {
crate::read_link(self)
}

fn fs_err_read_dir(&self) -> io::Result<crate::ReadDir> {
crate::read_dir(self)
}
}

mod private {
pub trait Sealed {}

impl Sealed for std::path::Path {}
}