file utils refactor
This commit is contained in:
parent
ade3a569e0
commit
6d0dcb987f
@ -2,6 +2,9 @@ use serde::{Serialize, Deserialize};
|
|||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use serde_yaml::{self};
|
use serde_yaml::{self};
|
||||||
use minijinja::Environment;
|
use minijinja::Environment;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use crate::file_utils::{write_file, copy_recursively};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
||||||
@ -11,19 +14,19 @@ pub struct LinksConfig {
|
|||||||
pub static_dir: String,
|
pub static_dir: String,
|
||||||
}
|
}
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct RustyLinks {
|
struct RustyLinks {
|
||||||
config: Config,
|
config: Config,
|
||||||
links: Vec<Links>,
|
links: Vec<Links>,
|
||||||
pub metadata: Option<MetaData>,
|
pub metadata: Option<MetaData>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct MetaData {
|
struct MetaData {
|
||||||
pub last_updated: String,
|
pub last_updated: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct Config {
|
struct Config {
|
||||||
title: String,
|
title: String,
|
||||||
name: String,
|
name: String,
|
||||||
description: String,
|
description: String,
|
||||||
@ -33,13 +36,13 @@ pub struct Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct Links {
|
struct Links {
|
||||||
title: String,
|
title: String,
|
||||||
links: Vec<Link>,
|
links: Vec<Link>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct Link {
|
struct Link {
|
||||||
text: String,
|
text: String,
|
||||||
icon: String,
|
icon: String,
|
||||||
href: Option<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 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");
|
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
|
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 main = std::fs::read_to_string(&config.template).expect(&format!("Could not find {}", config.template));
|
||||||
|
|
||||||
let mut env = Environment::new();
|
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();
|
let tmpl = env.get_template("main").unwrap();
|
||||||
tmpl.render(rusty_links).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");
|
||||||
|
}
|
22
src/main.rs
22
src/main.rs
@ -1,7 +1,7 @@
|
|||||||
mod links;
|
mod links;
|
||||||
mod file_utils;
|
mod file_utils;
|
||||||
|
|
||||||
use crate::links::{LinksConfig, load_links, render_links};
|
use crate::links::{LinksConfig, create_links};
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
@ -64,23 +64,11 @@ fn main() {
|
|||||||
// Generate and Write Links
|
// Generate and Write Links
|
||||||
match config.links {
|
match config.links {
|
||||||
Some(links_config) => {
|
Some(links_config) => {
|
||||||
create_links(links_dir, output_dir, &links_config);
|
create_links(
|
||||||
|
output_dir.join(links_dir),
|
||||||
|
&links_config
|
||||||
|
);
|
||||||
},
|
},
|
||||||
None => {}
|
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");
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user