From 7fd5232de7a3c91d255bc6e793fce1387760ecce Mon Sep 17 00:00:00 2001 From: Technoduck Date: Sun, 1 Sep 2024 23:03:34 -0400 Subject: [PATCH] minor changes, comments, and formatting + better error handling --- Cargo.lock | 26 +++++++++++++------------- Cargo.toml | 2 +- src/blog_entries.rs | 1 - src/handlers.rs | 12 ++++-------- src/main.rs | 20 +++++++++++++------- src/rand_quote.rs | 5 +++++ 6 files changed, 36 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ab98448..a52d2fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -831,19 +831,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "rusty_duck" -version = "0.1.0" -dependencies = [ - "askama", - "comrak", - "markdown-parser", - "rand", - "serde", - "serde_yaml 0.9.34+deprecated", - "syntect", -] - [[package]] name = "ryu" version = "1.0.18" @@ -938,6 +925,19 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "staticrustator" +version = "0.1.0" +dependencies = [ + "askama", + "comrak", + "markdown-parser", + "rand", + "serde", + "serde_yaml 0.9.34+deprecated", + "syntect", +] + [[package]] name = "strsim" version = "0.11.1" diff --git a/Cargo.toml b/Cargo.toml index 2a3c8cb..b7d77c6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "rusty_duck" +name = "staticrustator" version = "0.1.0" edition = "2021" diff --git a/src/blog_entries.rs b/src/blog_entries.rs index 7c24f2e..0bc7280 100644 --- a/src/blog_entries.rs +++ b/src/blog_entries.rs @@ -15,7 +15,6 @@ pub fn get_all_markdowns() -> Vec { Ok(iter) => iter, Err(err) => panic!("couldnt ls files, err {err}") }; - for entry in mr_dir_iter { if let Ok(entry) = entry { diff --git a/src/handlers.rs b/src/handlers.rs index c4b89f6..a05f436 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -10,10 +10,7 @@ pub fn index() -> String { } pub fn about() -> String { - - let about_markdown = markdown_parser::read_file("content/pages/about.md").expect("Could no find about.md file in content/pages/ folder"); - let about_content = parse_markdown(about_markdown.content()); let page_content = AboutTemplate{about_content: &about_content}; page_content.to_string() @@ -21,12 +18,11 @@ pub fn about() -> String { pub fn blog(blog_path:String) -> String { - - match get_blog_entry_markdown(&blog_path){ + match get_blog_entry_markdown(&blog_path){ Ok(markdown) => - BlogTemplate{ - blog_content: &parse_markdown(markdown.content()), - front_matter:&serde_yaml::from_str(markdown.front_matter()).unwrap()}.to_string() + BlogTemplate{ + blog_content: &parse_markdown(markdown.content()), + front_matter:&serde_yaml::from_str(markdown.front_matter()).unwrap()}.to_string() , Err(..) => not_found(), } diff --git a/src/main.rs b/src/main.rs index 5d28920..1b3573c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,25 +12,31 @@ fn main() { match DirBuilder::new() .recursive(true) .create(output_path) { - Ok(_) => (), - Err(_) => (), + 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(_) => (), + 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) => println!("Error detected: {err}"), + Ok(()) => (), + Err(err) => if err.kind() == std::io::ErrorKind::AlreadyExists { + () // if folder already exists we can continue + } else { + panic!("Error detected: {err}") + } } diff --git a/src/rand_quote.rs b/src/rand_quote.rs index 1a7ac5c..0ea33bb 100644 --- a/src/rand_quote.rs +++ b/src/rand_quote.rs @@ -1,6 +1,11 @@ use rand::Rng; +/// Generates a random quote from a vec of &str present in the function body. pub fn get_quote() -> &'static str { + // originally while dynamically server it would generate a new quote every time index would be + // reloaded, but with static serve nature a new quote is written every new build. + // Maybe not worth it. + // feel free to remove let vs = vec![ "Silliness and tomfooler are afoot, and who am I to stop it.", "Low entropy self replicating phenomenon that generates a binding force called compassion.",