webpack_q/
templating.rs

1/*
2 * Copyright [2022] [Kevin Velasco]
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use 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}