use std::{fs::{write, read_dir, DirBuilder},path::Path}; mod handlers; mod structs; pub mod blog_entries; pub mod rand_quote; fn main() { let output_path = "output"; match DirBuilder::new() .recursive(true) .create(output_path) { Ok(()) => (), Err(err) => if err.kind() == std::io::ErrorKind::AlreadyExists { () // if folder already exists we can continue } else { panic!("Error detected: {err}") } } match copy_dir_all("assets", format!("{output_path}/assets")) { Ok(()) => (), Err(err) => println!("Couldnt copy assets directory, err: {err}"), } write(format!("{output_path}/index.html"), handlers::index().as_bytes()).expect("Couldnt write index file"); write(format!("{output_path}/about.html"), handlers::about().as_bytes()).expect("Couldnt write about file"); write(format!("{output_path}/404.html"), handlers::not_found().as_bytes()).expect("Couldnt write 404 file"); match DirBuilder::new() .create(format!("{output_path}/blog")) { Ok(()) => (), Err(err) => if err.kind() == std::io::ErrorKind::AlreadyExists { () // if folder already exists we can continue } else { panic!("Error detected: {err}") } } let post_dir_iter = match read_dir("content/posts/") { Ok(iter) => iter, Err(err) => panic!("couldnt ls files, err {err}") }; for entry in post_dir_iter { if let Ok(entry) = entry { let filename = entry.file_name().into_string().unwrap().split(".").collect::>()[0].to_string(); write(format!("{output_path}/blog/{filename}.html"), handlers::blog(filename).as_bytes()).expect("Couldnt write blog entry"); }; } } fn copy_dir_all(src: impl AsRef, dst: impl AsRef) -> std::io::Result<()> { std::fs::create_dir_all(&dst)?; for entry in std::fs::read_dir(src)? { let entry = entry?; let ty = entry.file_type()?; if ty.is_dir() { copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?; } else { std::fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?; } } Ok(()) }