refactor all the things

This commit is contained in:
Tyrel Souza 2023-10-14 14:38:07 -04:00
parent 1c702242d6
commit b1ef76e2de
No known key found for this signature in database
GPG Key ID: F3614B02ACBE438E
5 changed files with 84 additions and 78 deletions

14
Cargo.lock generated
View File

@ -759,13 +759,7 @@ dependencies = [
]
[[package]]
name = "rustc-demangle"
version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76"
[[package]]
name = "rustylinks"
name = "rust-ssg"
version = "0.1.0"
dependencies = [
"chrono",
@ -775,6 +769,12 @@ dependencies = [
"serde_yaml",
]
[[package]]
name = "rustc-demangle"
version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76"
[[package]]
name = "ryu"
version = "1.0.15"

View File

@ -1,5 +1,5 @@
[package]
name = "rustylinks"
name = "rust-ssg"
version = "0.1.0"
edition = "2021"

40
src/file_utils.rs Normal file
View File

@ -0,0 +1,40 @@
use std::path::Path;
use std::{fs, io};
pub fn copy_recursively(source: impl AsRef<Path>, destination: impl AsRef<Path>) -> io::Result<()> {
// Credit: https://nick.groenen.me/notes/recursively-copy-files-in-rust/
fs::create_dir_all(&destination)?;
for entry in fs::read_dir(source)? {
let entry = entry?;
let filetype = entry.file_type()?;
if filetype.is_dir() {
copy_recursively(entry.path(), destination.as_ref().join(entry.file_name()))?;
} else {
fs::copy(entry.path(), destination.as_ref().join(entry.file_name()))?;
}
}
Ok(())
}
pub fn write_file(html: String) {
// copy all files in static to public
if Path::new("./public/links").exists() {
copy_recursively("./static", "./public/links").expect("Could not copy static directory");
}
fs::write("./public/links/index.html", html).expect("Could not write to index.html");
}
pub fn create_dirs() {
if !Path::new("./public").exists() {
fs::create_dir("./public").expect("Could not create public directory");
}
if !Path::new("./public/links").exists() {
fs::create_dir("./public/links").expect("Could not create public directory");
}
}
pub fn delete_public_dir() {
if Path::new("./public").exists() {
fs::remove_dir_all("./public").expect("could not remove directory");
}
}

View File

@ -1,4 +1,7 @@
use serde::{Serialize, Deserialize};
use chrono::{DateTime, Utc};
use serde_yaml::{self};
use minijinja::Environment;
#[derive(Serialize, Deserialize, Debug)]
pub struct RustyLinks {
@ -36,3 +39,26 @@ pub struct Link {
copy: Option<String>,
rels: Option<String>,
}
pub fn load_links(file_name: &str) -> RustyLinks {
let links_yaml = std::fs::File::open(file_name).expect("Could not find file");
let mut rusty_links: RustyLinks = serde_yaml::from_reader(links_yaml).expect("Could not read values");
// Set last updated time to now.
let now: DateTime<Utc> = Utc::now();
let metadata: MetaData = MetaData { last_updated: now.to_rfc2822() };
rusty_links.metadata = Some(metadata);
rusty_links
}
pub fn render_links(rusty_links: RustyLinks) -> String {
let main = std::fs::read_to_string("templates/links/links.html").expect("Could not find links.html");
let mut env = Environment::new();
env.add_template("main", &*main).unwrap();
let tmpl = env.get_template("main").unwrap();
tmpl.render(rusty_links).unwrap()
}

View File

@ -1,75 +1,15 @@
mod links;
mod file_utils;
use crate::links::{load_links,render_links};
use links::{RustyLinks, MetaData};
use std::{fs, io};
use std::path::Path;
use chrono::{DateTime, Utc};
use serde_yaml::{self};
use minijinja::{Environment};
pub fn copy_recursively(source: impl AsRef<Path>, destination: impl AsRef<Path>) -> io::Result<()> {
// Credit: https://nick.groenen.me/notes/recursively-copy-files-in-rust/
fs::create_dir_all(&destination)?;
for entry in fs::read_dir(source)? {
let entry = entry?;
let filetype = entry.file_type()?;
if filetype.is_dir() {
copy_recursively(entry.path(), destination.as_ref().join(entry.file_name()))?;
} else {
fs::copy(entry.path(), destination.as_ref().join(entry.file_name()))?;
}
}
Ok(())
}
fn load_links(file_name: &str) -> RustyLinks {
let links_yaml = std::fs::File::open(file_name).expect("Could not find file");
let mut rusty_links: RustyLinks = serde_yaml::from_reader(links_yaml).expect("Could not read values");
// Set last updated time to now.
let now: DateTime<Utc> = Utc::now();
let metadata: MetaData = MetaData { last_updated: now.to_rfc2822() };
rusty_links.metadata = Some(metadata);
rusty_links
}
fn render_links(rusty_links: RustyLinks) -> String {
let main = std::fs::read_to_string("templates/links/links.html").expect("Could not find links.html");
let mut env = Environment::new();
env.add_template("main", &*main).unwrap();
let tmpl = env.get_template("main").unwrap();
tmpl.render(rusty_links).unwrap()
}
fn write_file(html: String) {
if !Path::new("./public").exists() {
fs::create_dir("./public").expect("Could not create public directory");
}
if !Path::new("./public/links").exists() {
fs::create_dir("./public/links").expect("Could not create public directory");
}
// copy all files in static to public
if Path::new("./public/links").exists() {
copy_recursively("./static", "./public/links").expect("Could not copy static directory");
}
fs::write("./public/links/index.html", html).expect("Could not write to index.html");
}
fn delete_public_dir() {
if Path::new("./public").exists() {
fs::remove_dir_all("./public").expect("could not remove directory");
}
}
fn main() {
// Write Links
delete_public_dir();
write_file(render_links(load_links("data/links/links.yaml")));
file_utils::delete_public_dir();
file_utils::create_dirs();
// Generate and Write Links
let links_input = load_links("data/links/links.yaml");
let links_output = render_links(links_input);
file_utils::write_file(links_output);
}