box up FileEnv's functions
This commit is contained in:
parent
e54c136f5a
commit
34fe4da417
1 changed files with 9 additions and 7 deletions
|
@ -1,15 +1,19 @@
|
|||
use std::fs::{File, OpenOptions};
|
||||
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 open: FileOpen,
|
||||
pub append_line: FileAppendLine,
|
||||
pub open: FileOpenFn,
|
||||
pub append_line: FileAppendLineFn,
|
||||
}
|
||||
impl Default for FileEnv {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
open: |path| File::open(path),
|
||||
append_line: |file_name, line| {
|
||||
open: Box::new(|path| File::open(path)),
|
||||
append_line: Box::new(|file_name, line| {
|
||||
let mut file = OpenOptions::new()
|
||||
.write(true)
|
||||
.append(true)
|
||||
|
@ -18,9 +22,7 @@ impl Default for FileEnv {
|
|||
.unwrap();
|
||||
writeln!(file, "{}", line)?;
|
||||
Ok(())
|
||||
},
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
pub type FileOpen = fn(path: &str) -> std::io::Result<File>;
|
||||
pub type FileAppendLine = fn(paht: &str, line: &str) -> std::io::Result<()>;
|
||||
|
|
Loading…
Add table
Reference in a new issue