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