extracted writing files into its own function

main
Alina Marquardt 2023-04-10 06:24:17 +02:00
parent aab7616ebf
commit 7421e8a5e1
2 changed files with 25 additions and 17 deletions

View File

@ -2,9 +2,11 @@
name = "lf-builder" name = "lf-builder"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
authors = [ "Alina Marquardt" ] authors = [ "Alina Marquardt <lastfuture@lastfuture.de>" ]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
chrono = "0.4.24" chrono = "0.4.24"
json = "0.12.4"
mustache = "0.9.0"

View File

@ -6,41 +6,47 @@ use std::time::Instant;
const PUBLISH_PATH: &str = "./pub/"; const PUBLISH_PATH: &str = "./pub/";
const TEMPLATE_PATH: &str = "./src/templates/"; const TEMPLATE_PATH: &str = "./src/templates/";
fn render_html(file: &str, template: &str, data: Vec<i32>) -> Result<(), Error> { fn render_html(file: &str, template_file: &str) {
let output_path = &format!("{}{}", PUBLISH_PATH, file); let output_path = &format!("{}{}", PUBLISH_PATH, file);
let template_path = &format!("{}{}", TEMPLATE_PATH, template); let template_path = &format!("{}{}", TEMPLATE_PATH, template_file);
let now = Instant::now(); let now = Instant::now();
// TODO: Templating with template and data // TODO: Generate HTML with template and data
let html_string = "<h1>Hello!</h1>"; let html_string = "<h1>Hello!</h1>";
let elapsed = now.elapsed();
let mut output_file = File::create(output_path)?; let output = format!(
write!(output_file, "{}\n\n<!--\n {}\n {}\n generated in {:.2?}\n-->",
"{}\n
<!--\n
{}\n
{}\n
generated in {:.2?}\n
-->",
html_string, html_string,
template, template_path,
chrono::offset::Local::now(), chrono::offset::Local::now(),
elapsed now.elapsed(),
)?; );
let result = write_file(output_path, output);
match result {
Ok(file) => file,
Err(e) => panic!("Problem creating {:?}", e)
}
/*
let input = File::open(output_path)?; let input = File::open(output_path)?;
let buffered = BufReader::new(input); let buffered = BufReader::new(input);
for line in buffered.lines() { for line in buffered.lines() {
println!("{}", line?); println!("{}", line?);
} }
*/
}
fn write_file(file_path: &str, contents: String) -> Result<(), Error> {
let mut output_file = File::create(file_path)?;
write!(output_file, "{}", contents)?;
Ok(()) Ok(())
} }
fn main() { fn main() {
render_html("index.html", "index.tmpl", vec![1, 2, 3]); render_html("index.html", "index.tmpl");
} }