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
|
|
|
}
|
2023-08-06 12:14:02 +01:00
|
|
|
impl FileEnv {
|
|
|
|
pub fn create(directory: String) -> Self {
|
|
|
|
let open_dir = directory.clone();
|
|
|
|
let append_dir = directory.clone();
|
2023-07-29 20:36:03 +01:00
|
|
|
Self {
|
2023-08-06 12:14:02 +01:00
|
|
|
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);
|
2023-07-29 20:36:03 +01:00
|
|
|
let mut file = OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.append(true)
|
|
|
|
.create(true)
|
2023-08-06 12:14:02 +01:00
|
|
|
.open(&path)
|
2023-07-29 20:36:03 +01:00
|
|
|
.unwrap();
|
|
|
|
writeln!(file, "{}", line)?;
|
|
|
|
Ok(())
|
2023-08-05 06:50:00 +01:00
|
|
|
}),
|
2023-07-29 20:36:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|