file utils refactor

This commit is contained in:
Tyrel Souza 2023-10-14 22:57:42 -04:00
parent ade3a569e0
commit 6d0dcb987f
No known key found for this signature in database
GPG Key ID: F3614B02ACBE438E
3 changed files with 30 additions and 24 deletions

View File

@ -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<Links>,
pub metadata: Option<MetaData>,
}
#[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<Link>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Link {
struct Link {
text: String,
icon: String,
href: Option<String>,
@ -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");
}

View File

@ -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");
}