Function fsio::file::modify_file
source · [−]pub fn modify_file<T: AsPath + ?Sized>(
path: &T,
write_content: &dyn Fn(&mut File) -> Result<()>,
append: bool
) -> FsIOResult<()>
Expand description
Overwrites or appends the requested file and triggers the provided write_content function to enable custom writing.
Arguments
path
- The file pathwrite_content
- The custom writing functionappend
- True to append false to overwrite
Example
use crate::fsio::file;
use std::fs::File;
use std::io::Write;
use std::str;
fn main() {
let file_path = "./target/__test/file_test/modify_file/file.txt";
let mut result = file::modify_file(
file_path,
&move |file: &mut File| file.write_all("some content".as_bytes()),
false,
);
assert!(result.is_ok());
result = file::modify_file(
file_path,
&move |file: &mut File| file.write_all("\nmore content".as_bytes()),
true,
);
assert!(result.is_ok());
let data = file::read_file(file_path).unwrap();
assert_eq!(str::from_utf8(&data).unwrap(), "some content\nmore content");
}