minor changes, comments, and formatting + better error handling

This commit is contained in:
Technoduck 2024-09-01 23:03:34 -04:00
parent f9bc5cdfa8
commit 7fd5232de7
6 changed files with 36 additions and 30 deletions

26
Cargo.lock generated
View file

@ -831,19 +831,6 @@ dependencies = [
"windows-sys 0.52.0", "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]] [[package]]
name = "ryu" name = "ryu"
version = "1.0.18" version = "1.0.18"
@ -938,6 +925,19 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "staticrustator"
version = "0.1.0"
dependencies = [
"askama",
"comrak",
"markdown-parser",
"rand",
"serde",
"serde_yaml 0.9.34+deprecated",
"syntect",
]
[[package]] [[package]]
name = "strsim" name = "strsim"
version = "0.11.1" version = "0.11.1"

View file

@ -1,5 +1,5 @@
[package] [package]
name = "rusty_duck" name = "staticrustator"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"

View file

@ -15,7 +15,6 @@ pub fn get_all_markdowns() -> Vec<IndexPostEntry> {
Ok(iter) => iter, Ok(iter) => iter,
Err(err) => panic!("couldnt ls files, err {err}") Err(err) => panic!("couldnt ls files, err {err}")
}; };
for entry in mr_dir_iter { for entry in mr_dir_iter {
if let Ok(entry) = entry { if let Ok(entry) = entry {

View file

@ -10,10 +10,7 @@ pub fn index() -> String {
} }
pub fn about() -> 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_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 about_content = parse_markdown(about_markdown.content());
let page_content = AboutTemplate{about_content: &about_content}; let page_content = AboutTemplate{about_content: &about_content};
page_content.to_string() page_content.to_string()
@ -21,12 +18,11 @@ pub fn about() -> String {
pub fn blog(blog_path:String) -> String { pub fn blog(blog_path:String) -> String {
match get_blog_entry_markdown(&blog_path){
match get_blog_entry_markdown(&blog_path){
Ok(markdown) => Ok(markdown) =>
BlogTemplate{ BlogTemplate{
blog_content: &parse_markdown(markdown.content()), blog_content: &parse_markdown(markdown.content()),
front_matter:&serde_yaml::from_str(markdown.front_matter()).unwrap()}.to_string() front_matter:&serde_yaml::from_str(markdown.front_matter()).unwrap()}.to_string()
, ,
Err(..) => not_found(), Err(..) => not_found(),
} }

View file

@ -12,25 +12,31 @@ fn main() {
match DirBuilder::new() match DirBuilder::new()
.recursive(true) .recursive(true)
.create(output_path) { .create(output_path) {
Ok(_) => (), Ok(()) => (),
Err(_) => (), 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")) { match copy_dir_all("assets", format!("{output_path}/assets")) {
Ok(_) => (), Ok(()) => (),
Err(err) => println!("Couldnt copy assets directory, err: {err}"), 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}/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}/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"); write(format!("{output_path}/404.html"), handlers::not_found().as_bytes()).expect("Couldnt write 404 file");
match DirBuilder::new() match DirBuilder::new()
.create(format!("{output_path}/blog")) { .create(format!("{output_path}/blog")) {
Ok(_) => (), Ok(()) => (),
Err(err) => println!("Error detected: {err}"), Err(err) => if err.kind() == std::io::ErrorKind::AlreadyExists {
() // if folder already exists we can continue
} else {
panic!("Error detected: {err}")
}
} }

View file

@ -1,6 +1,11 @@
use rand::Rng; use rand::Rng;
/// Generates a random quote from a vec of &str present in the function body.
pub fn get_quote() -> &'static str { 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![ let vs = vec![
"Silliness and tomfooler are afoot, and who am I to stop it.", "Silliness and tomfooler are afoot, and who am I to stop it.",
"Low entropy self replicating phenomenon that generates a binding force called compassion.", "Low entropy self replicating phenomenon that generates a binding force called compassion.",