From 6d0dcb987fa4436715e40340d6b4cfb0191fd748 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Sat, 14 Oct 2023 22:57:42 -0400 Subject: [PATCH] file utils refactor --- src/{file_utils.rs => file_utils/mod.rs} | 0 src/links/mod.rs | 32 ++++++++++++++++++------ src/main.rs | 22 ++++------------ 3 files changed, 30 insertions(+), 24 deletions(-) rename src/{file_utils.rs => file_utils/mod.rs} (100%) diff --git a/src/file_utils.rs b/src/file_utils/mod.rs similarity index 100% rename from src/file_utils.rs rename to src/file_utils/mod.rs diff --git a/src/links/mod.rs b/src/links/mod.rs index 9634588..2637256 100644 --- a/src/links/mod.rs +++ b/src/links/mod.rs @@ -2,6 +2,9 @@ use serde::{Serialize, Deserialize}; use chrono::{DateTime, Utc}; use serde_yaml::{self}; use minijinja::Environment; +use std::path::PathBuf; +use crate::file_utils::{write_file, copy_recursively}; + #[derive(Serialize, Deserialize, Debug, PartialEq)] @@ -11,19 +14,19 @@ pub struct LinksConfig { pub static_dir: String, } #[derive(Serialize, Deserialize, Debug)] -pub struct RustyLinks { +struct RustyLinks { config: Config, links: Vec, pub metadata: Option, } #[derive(Serialize, Deserialize, Debug)] -pub struct MetaData { +struct MetaData { pub last_updated: String, } #[derive(Serialize, Deserialize, Debug)] -pub struct Config { +struct Config { title: String, name: String, description: String, @@ -33,13 +36,13 @@ pub struct Config { } #[derive(Serialize, Deserialize, Debug)] -pub struct Links { +struct Links { title: String, links: Vec, } #[derive(Serialize, Deserialize, Debug)] -pub struct Link { +struct Link { text: String, icon: String, href: Option, @@ -48,7 +51,7 @@ pub struct Link { } -pub fn load_links(config: &LinksConfig) -> RustyLinks { +fn load_links(config: &LinksConfig) -> RustyLinks { 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"); @@ -61,7 +64,7 @@ pub fn load_links(config: &LinksConfig) -> RustyLinks { rusty_links } -pub fn render_links(rusty_links: RustyLinks, config: &LinksConfig) -> String { +fn render_links(rusty_links: RustyLinks, config: &LinksConfig) -> String { let main = std::fs::read_to_string(&config.template).expect(&format!("Could not find {}", config.template)); let mut env = Environment::new(); @@ -69,3 +72,18 @@ pub fn render_links(rusty_links: RustyLinks, config: &LinksConfig) -> String { 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"); +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index efa8329..e64b6d6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ mod links; mod file_utils; -use crate::links::{LinksConfig, load_links, render_links}; +use crate::links::{LinksConfig, create_links}; use serde::{Serialize, Deserialize}; use std::path::PathBuf; @@ -64,23 +64,11 @@ fn main() { // Generate and Write Links match config.links { Some(links_config) => { - create_links(links_dir, output_dir, &links_config); + create_links( + output_dir.join(links_dir), + &links_config + ); }, None => {} } } - -fn create_links(links_dir: PathBuf, 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 - file_utils::write_file(links_output); - - // Copy Static Files - file_utils::copy_recursively( - &links_config.static_dir, - PathBuf::from(output_dir).join(links_dir) - ).expect("Could not copy static directory"); -}