podal/src/file/env.rs

31 lines
795 B
Rust
Raw Normal View History

use crate::prelude::*;
use std::fs::{File, OpenOptions};
use std::io::Write;
pub type FileOpenFn = Box<dyn Fn(&str) -> Result<File>>;
pub type FileAppendLineFn = Box<dyn Fn(&str, &str) -> Result<()>>;
pub struct FileEnv {
pub open: FileOpenFn,
pub append_line: FileAppendLineFn,
}
impl Default for FileEnv {
fn default() -> Self {
Self {
open: Box::new(|path| Ok(File::open(path)?)),
append_line: Box::new(|file_name, line| {
let mut file = OpenOptions::new()
.write(true)
.append(true)
.create(true)
.open(file_name)
.unwrap();
writeln!(file, "{}", line)?;
Ok(())
}),
}
}
}