From 6c926061233329b97f7d6650e9ef5c2d0c922afb Mon Sep 17 00:00:00 2001 From: Luke Carr Date: Mon, 2 Jan 2023 19:27:32 +0000 Subject: [PATCH] feat: refactored codebase and added more links --- Cargo.lock | 16 ++++----- Cargo.toml | 6 ++-- src/index.rs | 31 +++++++++++++++++ src/links.rs | 26 ++++++++++++++ src/main.rs | 82 +++++++++++++++----------------------------- src/robots.rs | 5 +++ templates/links.stpl | 4 ++- 7 files changed, 102 insertions(+), 68 deletions(-) create mode 100644 src/index.rs create mode 100644 src/links.rs create mode 100644 src/robots.rs diff --git a/Cargo.lock b/Cargo.lock index fd79ad2..e3de3d7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -43,9 +43,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chrono" -version = "0.4.22" +version = "0.4.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfd4d1b31faaa3a89d7934dbded3111da0d2ef28e3ebccdb4f0179f5929d1ef1" +checksum = "16b0a3d9ed01224b22057780a37bb8c5dbfe1be8ba48678e7bf57ec4b385411f" dependencies = [ "iana-time-zone", "js-sys", @@ -266,9 +266,9 @@ checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" [[package]] name = "sailfish" -version = "0.4.0" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "948a7edfc2f03d7c58a097dda25ed29440a72e8528894a6e182fe9171195fed1" +checksum = "79aef0b4612749106d372dfdeee715082f2f0fe24263be08e19db9b00b694bf9" dependencies = [ "itoap", "ryu", @@ -278,9 +278,9 @@ dependencies = [ [[package]] name = "sailfish-compiler" -version = "0.4.0" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f0a01133d6ce146020e6416ac6a823f813f1cbb30ff77548b4fa20749524947" +checksum = "787ef14715822299715d98d6eb6157f03a57a5258ffbd3321847f7450853dd64" dependencies = [ "filetime", "home", @@ -294,9 +294,9 @@ dependencies = [ [[package]] name = "sailfish-macros" -version = "0.4.0" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86326c1f1dce0b316e0a47071f683b185417dc64e1a704380b5c706b09e871b1" +checksum = "d0d39ce164c9e19147bcc4fa9ce9dcfc0a451e6cd0a996bb896fc7dee92887a4" dependencies = [ "proc-macro2", "sailfish-compiler", diff --git a/Cargo.toml b/Cargo.toml index 05c0ce7..d432b58 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,8 +3,6 @@ name = "www-carr-sh" version = "0.1.0" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] -chrono = "0.4.22" -sailfish = "0.4.0" +chrono = "0.4" +sailfish = "0.5" diff --git a/src/index.rs b/src/index.rs new file mode 100644 index 0000000..847fb7e --- /dev/null +++ b/src/index.rs @@ -0,0 +1,31 @@ +use std::fs; + +use chrono::{TimeZone, Utc}; +use sailfish::TemplateOnce; + +fn get_age() -> u8 { + let utc = Utc; + let dob = utc + .with_ymd_and_hms(2001, 7, 30, 0, 0, 0) + .single() + .expect("Failed to parse birthday"); + let age = Utc::now().signed_duration_since(dob).num_days() as f64 / 365.25; + age as u8 +} + +#[derive(TemplateOnce)] +#[template(path = "index.stpl")] +struct IndexPage { + age: u8, + title: &'static str, + build_time: String, +} + +pub fn build_index() -> std::io::Result<()> { + let ctx = IndexPage { + title: "Luke Carr", + build_time: Utc::now().to_rfc3339(), + age: get_age(), + }; + fs::write("out/index.html", ctx.render_once().unwrap()) +} diff --git a/src/links.rs b/src/links.rs new file mode 100644 index 0000000..5334c0d --- /dev/null +++ b/src/links.rs @@ -0,0 +1,26 @@ +use std::fs; + +use chrono::Utc; +use sailfish::TemplateOnce; + +#[derive(TemplateOnce)] +#[template(path = "links.stpl")] +struct LinksPage { + title: &'static str, + build_time: String, + links: Vec, +} + +pub struct Link { + pub href: &'static str, + pub text: &'static str, +} + +pub fn build_links(links: Vec) -> std::io::Result<()> { + let ctx = LinksPage { + title: "Links :: Luke Carr", + build_time: Utc::now().to_rfc3339(), + links, + }; + fs::write("out/links.html", ctx.render_once().unwrap()) +} diff --git a/src/main.rs b/src/main.rs index 74120b7..ec0b607 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,63 +1,35 @@ -use chrono::offset::Utc; use std::fs; -use std::time::Instant; -use chrono::TimeZone; -use sailfish::TemplateOnce; -fn main() { - let now = Instant::now(); - let build_time = Utc::now(); +mod index; +use index::build_index; +mod links; +use links::{build_links, Link}; +mod robots; +use robots::build_robots; +fn main() { fs::create_dir_all("out").expect("Failed to create ouput directory"); - let index = IndexPage { - age: get_age(), - title: "Luke Carr".to_owned(), - build_time: build_time.to_rfc3339(), - }; - build_index(index).expect("Failed to build index page"); - - let links = LinksPage { - title: "Links :: Luke Carr".to_owned(), - build_time: build_time.to_rfc3339(), - }; - build_links(links).expect("Failed to build links page"); - + build_index().expect("Failed to build index page"); build_robots().expect("Failed to build robots.txt"); - println!("Compiled site successfully in {:.1}ms!", (now.elapsed().as_nanos() as f64) / 1e6); -} - -fn get_age() -> u8 { - let utc = Utc; - let dob = utc.ymd(2001, 7, 30); - let age = Utc::today().signed_duration_since(dob).num_days() as f64 / 365.25; - return age as u8; -} - -#[derive(TemplateOnce)] -#[template(path = "index.stpl")] -struct IndexPage { - age: u8, - title: String, - build_time: String, -} - -fn build_index(ctx: IndexPage) -> std::io::Result<()> { - fs::write("out/index.html", ctx.render_once().unwrap()) -} - -#[derive(TemplateOnce)] -#[template(path = "links.stpl")] -struct LinksPage { - title: String, - build_time: String, -} - -fn build_links(ctx: LinksPage) -> std::io::Result<()> { - fs::write("out/links.html", ctx.render_once().unwrap()) -} - -fn build_robots() -> std::io::Result<()> { - fs::write("out/robots.txt", concat!("User-agent: *", "Allow: /")) + let links = vec![ + Link { + href: "https://sr.ht/~carr", + text: "Sourcehut (~carr)", + }, + Link { + href: "https://github.com/lukecarr", + text: "GitHub (lukecarr)", + }, + Link { + href: "https://gitlab.com/lukecarr", + text: "GitLab (lukecarr)", + }, + Link { + href: "https://linkedin.com/in/luke-j-carr", + text: "LinkedIn (luke-j-carr)", + }, + ]; + build_links(links).expect("Failed to build links page"); } diff --git a/src/robots.rs b/src/robots.rs new file mode 100644 index 0000000..be8140c --- /dev/null +++ b/src/robots.rs @@ -0,0 +1,5 @@ +use std::fs; + +pub fn build_robots() -> std::io::Result<()> { + fs::write("out/robots.txt", concat!("User-agent: *", "Allow: /")) +} diff --git a/templates/links.stpl b/templates/links.stpl index 1aefe6e..e1761b1 100644 --- a/templates/links.stpl +++ b/templates/links.stpl @@ -5,7 +5,9 @@ <% include!("./header.stpl"); %>

links

<% include!("./footer.stpl"); %>