Skip to content
Open
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
12 changes: 6 additions & 6 deletions src/dir.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use std::ffi::OsString;
use std::fs;
use std::io;
use std::path::PathBuf;
use std::path::{Path, PathBuf};

use crate::errors::{Error, ErrorKind};

/// Returns an iterator over the entries within a directory.
///
/// Wrapper for [`fs::read_dir`](https://doc.rust-lang.org/stable/std/fs/fn.read_dir.html).
pub fn read_dir<P: Into<PathBuf>>(path: P) -> io::Result<ReadDir> {
let path = path.into();
pub fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> {
let path = path.as_ref();

match fs::read_dir(&path) {
Ok(inner) => Ok(ReadDir { inner, path }),
Err(source) => Err(Error::build(source, ErrorKind::ReadDir, path)),
match fs::read_dir(path) {
Ok(inner) => Ok(ReadDir { inner, path: path.to_path_buf() }),
Err(source) => Err(Error::build(source, ErrorKind::ReadDir, path.to_path_buf())),
}
}

Expand Down
37 changes: 14 additions & 23 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,47 +33,38 @@ impl File {
/// Attempts to open a file in read-only mode.
///
/// Wrapper for [`File::open`](https://doc.rust-lang.org/stable/std/fs/struct.File.html#method.open).
pub fn open<P>(path: P) -> Result<Self, io::Error>
where
P: Into<PathBuf>,
{
let path = path.into();
match open(&path) {
Ok(file) => Ok(File::from_parts(file, path)),
Err(err_gen) => Err(err_gen(path)),
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self, io::Error> {
let path = path.as_ref();
match open(path) {
Ok(file) => Ok(File::from_parts(file, path.to_path_buf())),
Err(err_gen) => Err(err_gen(path.to_path_buf())),
}
}

/// Opens a file in write-only mode.
///
/// Wrapper for [`File::create`](https://doc.rust-lang.org/stable/std/fs/struct.File.html#method.create).
pub fn create<P>(path: P) -> Result<Self, io::Error>
where
P: Into<PathBuf>,
{
let path = path.into();
match create(&path) {
Ok(file) => Ok(File::from_parts(file, path)),
Err(err_gen) => Err(err_gen(path)),
pub fn create<P: AsRef<Path>>(path: P) -> Result<Self, io::Error> {
let path = path.as_ref();
match create(path) {
Ok(file) => Ok(File::from_parts(file, path.to_path_buf())),
Err(err_gen) => Err(err_gen(path.to_path_buf())),
}
}

/// Opens a file in read-write mode.
///
/// Wrapper for [`File::create_new`](https://doc.rust-lang.org/stable/std/fs/struct.File.html#method.create_new).
pub fn create_new<P>(path: P) -> Result<Self, io::Error>
where
P: Into<PathBuf>,
{
let path = path.into();
pub fn create_new<P: AsRef<Path>>(path: P) -> Result<Self, io::Error> {
let path = path.as_ref();
// TODO: Use fs::File::create_new once MSRV is at least 1.77
match fs::OpenOptions::new()
.read(true)
.write(true)
.create_new(true)
.open(&path)
.open(path)
{
Ok(file) => Ok(File::from_parts(file, path)),
Ok(file) => Ok(File::from_parts(file, path.to_path_buf())),
Err(err) => Err(Error::build(err, ErrorKind::CreateFile, path)),
}
}
Expand Down
11 changes: 4 additions & 7 deletions src/open_options.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fs, io, path::PathBuf};
use std::{fs, io, path::{Path, PathBuf}};

use crate::errors::{Error, ErrorKind};

Expand Down Expand Up @@ -66,13 +66,10 @@ impl OpenOptions {
/// Opens a file at `path` with the options specified by `self`.
///
/// Wrapper for [`std::fs::OpenOptions::open`](https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.open)
pub fn open<P>(&self, path: P) -> io::Result<crate::File>
where
P: Into<PathBuf>,
{
let path = path.into();
pub fn open<P: AsRef<Path>>(&self, path: P) -> io::Result<crate::File> {
let path = path.as_ref();
match self.0.open(&path) {
Ok(file) => Ok(crate::File::from_parts(file, path)),
Ok(file) => Ok(crate::File::from_parts(file, path.to_path_buf())),
Err(source) => Err(Error::build(source, ErrorKind::OpenFile, path)),
}
}
Expand Down