1use std::fs::{File, OpenOptions};
18use std::io;
19use std::io::BufWriter;
20use std::path::Path;
21
22pub fn write_html_files_to_directory(
23 directory: &Path,
24 write: impl FnOnce(&mut BufWriter<&mut File>) -> io::Result<()>,
25) -> std::io::Result<()> {
26 let html_file = directory.join("index.html");
27 let data_json = directory.join("data.json");
28
29 use std::io::Write;
30
31 let mut html_file_handle = OpenOptions::new()
32 .create(true)
33 .write(true)
34 .open(html_file)?;
35 let mut data_file_handle = OpenOptions::new()
36 .create(true)
37 .write(true)
38 .open(data_json)?;
39 html_file_handle.set_len(0)?;
40 data_file_handle.set_len(0)?;
41
42 write!(
43 &mut html_file_handle,
44 "{}",
45 include_str!("../templates/index.html")
46 )?;
47 let mut writer = BufWriter::new(&mut data_file_handle);
48 write(&mut writer)?;
49
50 Ok(())
51}