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::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<()>;
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue