Static site generator + my presonnal website written in rust for some reason.
1use std::{fs::{write, 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(err) => if err.kind() == std::io::ErrorKind::AlreadyExists {
17 () // if folder already exists we can continue
18 } else {
19 panic!("Error detected: {err}")
20 }
21 }
22
23 match copy_dir_all("assets", format!("{output_path}/assets")) {
24 Ok(()) => (),
25 Err(err) => println!("Couldnt copy assets directory, err: {err}"),
26 }
27
28 write(format!("{output_path}/index.html"), handlers::index().as_bytes()).expect("Couldnt write index file");
29 write(format!("{output_path}/about.html"), handlers::about().as_bytes()).expect("Couldnt write about file");
30 write(format!("{output_path}/404.html"), handlers::not_found().as_bytes()).expect("Couldnt write 404 file");
31
32 match DirBuilder::new()
33 .create(format!("{output_path}/blog")) {
34 Ok(()) => (),
35 Err(err) => if err.kind() == std::io::ErrorKind::AlreadyExists {
36 () // if folder already exists we can continue
37 } else {
38 panic!("Error detected: {err}")
39 }
40 }
41
42
43 let post_dir_iter = match read_dir("content/posts/") {
44 Ok(iter) => iter,
45 Err(err) => panic!("couldnt ls files, err {err}")
46 };
47
48 for entry in post_dir_iter {
49 if let Ok(entry) = entry {
50
51 let filename = entry.file_name().into_string().unwrap().split(".").collect::<Vec<_>>()[0].to_string();
52
53 write(format!("{output_path}/blog/{filename}.html"), handlers::blog(filename).as_bytes()).expect("Couldnt write blog entry");
54 };
55
56 }
57
58}
59fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> std::io::Result<()> {
60 std::fs::create_dir_all(&dst)?;
61 for entry in std::fs::read_dir(src)? {
62 let entry = entry?;
63 let ty = entry.file_type()?;
64 if ty.is_dir() {
65 copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?;
66 } else {
67 std::fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
68 }
69 }
70 Ok(())
71}