Static site generator + my presonnal website written in rust for some reason.
1use std::{fs::{self, read_dir, DirBuilder},path::Path};
2
3mod handlers;
4mod structs;
5pub mod blog_entries;
6pub mod rand_quote;
7
8
9
10fn main() {
11 let output_path = "output";
12 match DirBuilder::new()
13 .recursive(true)
14 .create(output_path) {
15 Ok(_) => (),
16 Err(_) => (),
17 }
18
19 match copy_dir_all("assets", format!("{output_path}/assets")) {
20 Ok(_) => (),
21 Err(err) => println!("Couldnt copy assets directory, err: {err}"),
22 }
23
24
25
26 fs::write(format!("{output_path}/index.html"), handlers::index().as_bytes()).expect("Couldnt write index file");
27 fs::write(format!("{output_path}/about.html"), handlers::about().as_bytes()).expect("Couldnt write about file");
28 fs::write(format!("{output_path}/404.html"), handlers::not_found().as_bytes()).expect("Couldnt write 404 file");
29
30 match DirBuilder::new()
31 .create(format!("{output_path}/blog")) {
32 Ok(_) => (),
33 Err(err) => println!("Error detected: {err}"),
34 }
35
36
37 let post_dir_iter = match read_dir("posts/") {
38 Ok(iter) => iter,
39 Err(err) => panic!("could ls files, err {err}")
40 };
41
42 for entry in post_dir_iter {
43 if let Ok(entry) = entry {
44
45 let filename = entry.file_name().into_string().unwrap().split(".").collect::<Vec<_>>()[0].to_string();
46
47 fs::write(format!("{output_path}/blog/{filename}.html"), handlers::blog(filename).as_bytes()).expect("Couldnt write blog entry");
48 };
49
50 }
51
52}
53fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> std::io::Result<()> {
54 std::fs::create_dir_all(&dst)?;
55 for entry in std::fs::read_dir(src)? {
56 let entry = entry?;
57 let ty = entry.file_type()?;
58 if ty.is_dir() {
59 copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?;
60 } else {
61 std::fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
62 }
63 }
64 Ok(())
65}