use crate::prelude::*; use std::fs::{File, OpenOptions}; use std::io::Write; pub type FileOpenFn = Box Result>; pub type FileAppendLineFn = Box Result<()>>; pub struct FileEnv { pub open: FileOpenFn, pub append_line: FileAppendLineFn, } impl FileEnv { pub fn create(directory: String) -> Self { let open_dir = directory.clone(); let append_dir = directory.clone(); Self { open: Box::new(move |file_name| { let path = format!("{}/{}", &open_dir, file_name); let file = File::open(&path)?; Ok(file) }), append_line: Box::new(move |file_name, line| { let path = format!("{}/{}", &append_dir, file_name); let mut file = OpenOptions::new() .write(true) .append(true) .create(true) .open(&path) .unwrap(); writeln!(file, "{}", line)?; Ok(()) }), } } }