use std::{future::Future, io, path::Path};
use super::{File, sealed::Sealed};
use crate::unblock;
#[derive(Clone, Debug)]
pub struct OpenOptions(std::fs::OpenOptions);
impl OpenOptions {
pub fn new() -> Self {
Self(std::fs::OpenOptions::new())
}
pub fn read(&mut self, read: bool) -> &mut Self {
self.0.read(read);
self
}
pub fn write(&mut self, write: bool) -> &mut Self {
self.0.write(write);
self
}
pub fn append(&mut self, append: bool) -> &mut Self {
self.0.append(append);
self
}
pub fn truncate(&mut self, truncate: bool) -> &mut Self {
self.0.truncate(truncate);
self
}
pub fn create(&mut self, create: bool) -> &mut Self {
self.0.create(create);
self
}
pub fn create_new(&mut self, create_new: bool) -> &mut Self {
self.0.create_new(create_new);
self
}
pub fn open<P>(&self, path: P) -> impl Future<Output = io::Result<File>> + use<P>
where
P: AsRef<Path>,
{
let options = self.0.clone();
let path = path.as_ref().to_owned();
async move {
let file = unblock(move || options.open(path)).await?;
Ok(File::from(file))
}
}
}
impl Default for OpenOptions {
fn default() -> Self {
Self::new()
}
}
impl Sealed for OpenOptions {}
#[cfg(unix)]
impl super::unix::OpenOptionsExt for OpenOptions {
fn mode(&mut self, mode: u32) -> &mut Self {
std::os::unix::fs::OpenOptionsExt::mode(&mut self.0, mode);
self
}
fn custom_flags(&mut self, flags: i32) -> &mut Self {
std::os::unix::fs::OpenOptionsExt::custom_flags(&mut self.0, flags);
self
}
}
#[cfg(windows)]
impl super::windows::OpenOptionsExt for OpenOptions {
fn access_mode(&mut self, access: u32) -> &mut Self {
std::os::windows::fs::OpenOptionsExt::access_mode(&mut self.0, access);
self
}
fn share_mode(&mut self, share: u32) -> &mut Self {
std::os::windows::fs::OpenOptionsExt::share_mode(&mut self.0, share);
self
}
fn custom_flags(&mut self, flags: u32) -> &mut Self {
std::os::windows::fs::OpenOptionsExt::custom_flags(&mut self.0, flags);
self
}
fn attributes(&mut self, attributes: u32) -> &mut Self {
std::os::windows::fs::OpenOptionsExt::attributes(&mut self.0, attributes);
self
}
fn security_qos_flags(&mut self, flags: u32) -> &mut Self {
std::os::windows::fs::OpenOptionsExt::security_qos_flags(&mut self.0, flags);
self
}
}