use crate::fs::{asyncify, File};
use std::io;
use std::path::Path;
#[cfg(test)]
mod mock_open_options;
#[cfg(test)]
use mock_open_options::MockOpenOptions as StdOpenOptions;
#[cfg(not(test))]
use std::fs::OpenOptions as StdOpenOptions;
#[derive(Clone, Debug)]
pub struct OpenOptions(StdOpenOptions);
impl OpenOptions {
pub fn new() -> OpenOptions {
OpenOptions(StdOpenOptions::new())
}
pub fn read(&mut self, read: bool) -> &mut OpenOptions {
self.0.read(read);
self
}
pub fn write(&mut self, write: bool) -> &mut OpenOptions {
self.0.write(write);
self
}
pub fn append(&mut self, append: bool) -> &mut OpenOptions {
self.0.append(append);
self
}
pub fn truncate(&mut self, truncate: bool) -> &mut OpenOptions {
self.0.truncate(truncate);
self
}
pub fn create(&mut self, create: bool) -> &mut OpenOptions {
self.0.create(create);
self
}
pub fn create_new(&mut self, create_new: bool) -> &mut OpenOptions {
self.0.create_new(create_new);
self
}
pub async fn open(&self, path: impl AsRef<Path>) -> io::Result<File> {
let path = path.as_ref().to_owned();
let opts = self.0.clone();
let std = asyncify(move || opts.open(path)).await?;
Ok(File::from_std(std))
}
pub(super) fn as_inner_mut(&mut self) -> &mut StdOpenOptions {
&mut self.0
}
}
feature! {
#![unix]
use std::os::unix::fs::OpenOptionsExt;
impl OpenOptions {
pub fn mode(&mut self, mode: u32) -> &mut OpenOptions {
self.as_inner_mut().mode(mode);
self
}
pub fn custom_flags(&mut self, flags: i32) -> &mut OpenOptions {
self.as_inner_mut().custom_flags(flags);
self
}
}
}
feature! {
#![windows]
use std::os::windows::fs::OpenOptionsExt;
impl OpenOptions {
pub fn access_mode(&mut self, access: u32) -> &mut OpenOptions {
self.as_inner_mut().access_mode(access);
self
}
pub fn share_mode(&mut self, share: u32) -> &mut OpenOptions {
self.as_inner_mut().share_mode(share);
self
}
pub fn custom_flags(&mut self, flags: u32) -> &mut OpenOptions {
self.as_inner_mut().custom_flags(flags);
self
}
pub fn attributes(&mut self, attributes: u32) -> &mut OpenOptions {
self.as_inner_mut().attributes(attributes);
self
}
pub fn security_qos_flags(&mut self, flags: u32) -> &mut OpenOptions {
self.as_inner_mut().security_qos_flags(flags);
self
}
}
}
impl From<StdOpenOptions> for OpenOptions {
fn from(options: StdOpenOptions) -> OpenOptions {
OpenOptions(options)
}
}
impl Default for OpenOptions {
fn default() -> Self {
Self::new()
}
}