use std::fs;
#[allow(clippy::struct_excessive_bools)]
#[derive(Debug, Clone, Copy)]
#[allow(missing_docs)]
pub struct OpenOptions
{
pub read: bool,
pub write: bool,
pub append: bool,
pub truncate: bool,
pub create: bool,
pub create_new: bool,
}
impl Default for OpenOptions
{
fn default() -> Self
{
Self::new()
}
}
#[allow(clippy::return_self_not_must_use)]
impl OpenOptions
{
#[must_use]
pub const fn new() -> Self
{
Self {
read: false,
write: false,
append: false,
truncate: false,
create: false,
create_new: false,
}
}
pub fn read(&mut self, flag: bool) -> Self
{
self.read = flag;
*self
}
pub fn write(&mut self, flag: bool) -> Self
{
self.write = flag;
*self
}
pub fn append(&mut self, flag: bool) -> Self
{
self.append = flag;
*self
}
pub fn truncate(&mut self, flag: bool) -> Self
{
self.truncate = flag;
*self
}
pub fn create(&mut self, flag: bool) -> Self
{
self.create = flag;
*self
}
pub fn create_new(&mut self, flag: bool) -> Self
{
self.create_new = flag;
*self
}
}
impl From<&fs::OpenOptions> for OpenOptions
{
fn from(open_options: &fs::OpenOptions) -> Self
{
let mut rv = Self::new();
for line in format!("{:#?}", open_options).lines()
{
let line: &str = line;
if line.starts_with(" read: ")
{
rv.read = line.as_bytes()[14] == b't';
}
if line.starts_with(" write: ")
{
rv.write = line.as_bytes()[15] == b't';
}
if line.starts_with(" append: ")
{
rv.append = line.as_bytes()[16] == b't';
}
if line.starts_with(" truncate: ")
{
rv.truncate = line.as_bytes()[18] == b't';
}
if line.starts_with(" create: ")
{
rv.create = line.as_bytes()[16] == b't';
}
if line.starts_with(" create_new: ")
{
rv.create_new = line.as_bytes()[20] == b't';
}
}
rv
}
}
impl From<OpenOptions> for fs::OpenOptions
{
fn from(open_options: OpenOptions) -> Self
{
let mut rv = Self::new();
rv.read(open_options.read);
rv.write(open_options.write);
rv.append(open_options.append);
rv.truncate(open_options.truncate);
rv.create(open_options.create);
rv.create_new(open_options.create_new);
rv
}
}
#[cfg(test)]
mod tests
{
use std::fs;
use super::OpenOptions;
#[test]
fn deconstruct_test()
{
for i in 0..(1 << 6)
{
let options: OpenOptions = From::from(
&*fs::OpenOptions::new()
.read(i % 2 == 1)
.write((i / 2) % 2 == 1)
.append((i / 4) % 2 == 1)
.truncate((i / 8) % 2 == 1)
.create((i / 16) % 2 == 1)
.create_new((i / 32) % 2 == 1),
);
let num: u8 = options.read as u8
+ options.write as u8 * 2
+ options.append as u8 * 4
+ options.truncate as u8 * 8
+ options.create as u8 * 16
+ options.create_new as u8 * 32;
assert_eq!(num, i);
}
}
}