rust-ssg/src/links/mod.rs
2023-10-14 23:32:49 -04:00

49 lines
1.9 KiB
Rust

pub mod structs;
pub use structs::{LinksConfig, RustyLinks, MetaData};
use chrono::{DateTime, Utc};
use serde_yaml::{self};
use minijinja::Environment;
use std::path::PathBuf;
use crate::file_utils::{write_file, copy_recursively};
fn load_links(config: &LinksConfig) -> RustyLinks {
// TODO: Handle Panic
// thread 'main' panicked at 'Could not find data/links/links.yaml: Os { code: 2, kind: NotFound, message: "No such file or directory" }', src/links/mod.rs:11:58
let links_yaml = std::fs::File::open(&config.source).expect(&format!("Could not find {}", config.source));
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, config: &LinksConfig) -> String {
// TODO: Handle Panic
// thread 'main' panicked at 'Could not find templates/links/links.html: Os { code: 2, kind: NotFound, message: "No such file or directory" }', src/links/mod.rs:24:58
let main = std::fs::read_to_string(&config.template).expect(&format!("Could not find {}", config.template));
let mut env = Environment::new();
env.add_template("main", &*main).unwrap();
let tmpl = env.get_template("main").unwrap();
tmpl.render(rusty_links).unwrap()
}
pub fn create_links(output_dir: PathBuf, links_config: &LinksConfig) {
// Load the YAML file
let links_input = load_links(&links_config);
// Apply the Yaml file to the Template
let links_output = render_links(links_input, &links_config);
// Save to Disk
write_file(links_output);
// Copy Static Files
copy_recursively(
&links_config.static_dir,output_dir
).expect("Could not copy static directory");
}