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 missing File / OpenOptions constructors #69

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
22 changes: 22 additions & 0 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::io::{self, Read, Seek, Write};
use std::path::{Path, PathBuf};

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

/// Wrapper around [`std::fs::File`][std::fs::File] which adds more helpful
/// information to all errors.
Expand Down Expand Up @@ -57,6 +58,27 @@ impl File {
}
}

/// 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();
match fs::File::create_new(&path) {
Ok(file) => Ok(File::from_parts(file, path)),
Err(err) => Err(Error::build(err, ErrorKind::CreateFile, path)),
}
}

/// Returns a new `OpenOptions` object.
///
/// Wrapper for [`File::options`](https://doc.rust-lang.org/stable/std/fs/struct.File.html#method.options).
pub fn options() -> OpenOptions {
OpenOptions::new()
}

/// Attempts to sync all OS-internal metadata to disk.
///
/// Wrapper for [`File::sync_all`](https://doc.rust-lang.org/stable/std/fs/struct.File.html#method.sync_all).
Expand Down
20 changes: 20 additions & 0 deletions src/tokio/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use tokio::fs;
use tokio::fs::File as TokioFile;
use tokio::io::{AsyncRead, AsyncSeek, AsyncWrite, ReadBuf};

use super::OpenOptions;

/// Wrapper around [`tokio::fs::File`] which adds more helpful
/// information to all errors.
#[derive(Debug)]
Expand Down Expand Up @@ -41,6 +43,24 @@ impl File {
}
}

/// Opens a file in read-write mode.
///
/// Wrapper for [`tokio::fs::File::create_new`].
pub async fn create_new(path: impl Into<PathBuf>) -> Result<Self, io::Error> {
let path = path.into();
match fs::File::create_new(&path).await {
Ok(file) => Ok(File::from_parts(file, path)),
Err(err) => Err(Error::build(err, ErrorKind::CreateFile, path)),
}
}

/// Returns a new `OpenOptions` object.
///
/// Wrapper for [`tokio::fs::File::options`].
pub fn options() -> OpenOptions {
OpenOptions::new()
}

/// Converts a [`crate::File`] to a [`tokio::fs::File`].
///
/// Wrapper for [`tokio::fs::File::from_std`].
Expand Down
Loading