i5-add-tests (part 4) #10

Merged
kemitix merged 39 commits from i5-add-tests into main 2023-08-05 06:50:01 +01:00
Showing only changes of commit 34fe4da417 - Show all commits

View file

@ -1,15 +1,19 @@
use std::fs::{File, OpenOptions}; use std::fs::{File, OpenOptions};
use std::io::Write; use std::io::Write;
pub type FileOpenFn = Box<dyn Fn(&str) -> std::io::Result<File>>;
pub type FileAppendLineFn = Box<dyn Fn(&str, &str) -> std::io::Result<()>>;
pub struct FileEnv { pub struct FileEnv {
pub open: FileOpen, pub open: FileOpenFn,
pub append_line: FileAppendLine, pub append_line: FileAppendLineFn,
} }
impl Default for FileEnv { impl Default for FileEnv {
fn default() -> Self { fn default() -> Self {
Self { Self {
open: |path| File::open(path), open: Box::new(|path| File::open(path)),
append_line: |file_name, line| { append_line: Box::new(|file_name, line| {
let mut file = OpenOptions::new() let mut file = OpenOptions::new()
.write(true) .write(true)
.append(true) .append(true)
@ -18,9 +22,7 @@ impl Default for FileEnv {
.unwrap(); .unwrap();
writeln!(file, "{}", line)?; writeln!(file, "{}", line)?;
Ok(()) Ok(())
}, }),
} }
} }
} }
pub type FileOpen = fn(path: &str) -> std::io::Result<File>;
pub type FileAppendLine = fn(paht: &str, line: &str) -> std::io::Result<()>;