Static site generator + my presonnal website written in rust for some reason.

minor changes, comments, and formatting + better error handling

+13 -13
Cargo.lock
···
]
[[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"
source = "registry+https://github.com/rust-lang/crates.io-index"
···
dependencies = [
"deunicode",
"wasm-bindgen",
+
]
+
+
[[package]]
+
name = "staticrustator"
+
version = "0.1.0"
+
dependencies = [
+
"askama",
+
"comrak",
+
"markdown-parser",
+
"rand",
+
"serde",
+
"serde_yaml 0.9.34+deprecated",
+
"syntect",
]
[[package]]
+1 -1
Cargo.toml
···
[package]
-
name = "rusty_duck"
+
name = "staticrustator"
version = "0.1.0"
edition = "2021"
-1
src/blog_entries.rs
···
Ok(iter) => iter,
Err(err) => panic!("couldnt ls files, err {err}")
};
-
for entry in mr_dir_iter {
if let Ok(entry) = entry {
+4 -8
src/handlers.rs
···
}
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()
···
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(),
}
+13 -7
src/main.rs
···
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}")
+
}
}
+5
src/rand_quote.rs
···
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.",