restructure
This commit is contained in:
parent
d79924f70a
commit
ade3a569e0
908
Cargo.lock
generated
908
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -10,4 +10,3 @@ serde = { version = "1.0.144", features = ["derive"] }
|
||||
serde_yaml = "0.9.22"
|
||||
chrono = "0.4.26"
|
||||
minijinja = "1.0.8"
|
||||
rst="0.4.0"
|
@ -1,2 +1,5 @@
|
||||
output_dir: public/
|
||||
links:
|
||||
source: data/links.yaml
|
||||
source: data/links/links.yaml
|
||||
template: templates/links/links.html
|
||||
static_dir: static/
|
@ -24,7 +24,7 @@ pub fn write_file(html: String) {
|
||||
fs::write("./public/links/index.html", html).expect("Could not write to index.html");
|
||||
}
|
||||
|
||||
pub fn create_dirs(dirs: &[&str]) {
|
||||
pub fn create_dirs(dirs: Vec<&str>) {
|
||||
if !Path::new("./public").exists() {
|
||||
fs::create_dir("./public").expect("Could not create public directory");
|
||||
}
|
||||
|
@ -3,6 +3,13 @@ use chrono::{DateTime, Utc};
|
||||
use serde_yaml::{self};
|
||||
use minijinja::Environment;
|
||||
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
||||
pub struct LinksConfig {
|
||||
pub source: String,
|
||||
pub template: String,
|
||||
pub static_dir: String,
|
||||
}
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct RustyLinks {
|
||||
config: Config,
|
||||
@ -41,8 +48,8 @@ pub struct Link {
|
||||
}
|
||||
|
||||
|
||||
pub fn load_links(file_name: &str) -> RustyLinks {
|
||||
let links_yaml = std::fs::File::open(file_name).expect("Could not find file");
|
||||
pub 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");
|
||||
|
||||
@ -54,8 +61,8 @@ pub fn load_links(file_name: &str) -> RustyLinks {
|
||||
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");
|
||||
pub 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();
|
||||
env.add_template("main", &*main).unwrap();
|
||||
|
82
src/main.rs
82
src/main.rs
@ -1,46 +1,86 @@
|
||||
mod links;
|
||||
mod file_utils;
|
||||
use crate::links::{load_links,render_links};
|
||||
|
||||
use crate::links::{LinksConfig, load_links, render_links};
|
||||
use serde::{Serialize, Deserialize};
|
||||
use std::path::PathBuf;
|
||||
|
||||
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct LinksConfig {
|
||||
source: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
||||
pub struct BlogConfig {
|
||||
source: String,
|
||||
}
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Default)]
|
||||
pub struct Config {
|
||||
links: Option(LinksConfig),
|
||||
blog: Option(BlogConfig),
|
||||
output_dir: String,
|
||||
links: Option<LinksConfig>,
|
||||
blog: Option<BlogConfig>,
|
||||
}
|
||||
|
||||
fn load_config() {
|
||||
fn load_config() -> Config {
|
||||
let config_file = std::fs::File::open("./config.yaml").expect("Could not find config.yaml file");
|
||||
|
||||
let config: Config = serde_yaml::from_reader(config_file).expect("Could not read values");
|
||||
println!("{:?}", config)
|
||||
let config: Result<Config, serde_yaml::Error> = serde_yaml::from_reader(config_file);
|
||||
match config {
|
||||
Ok(parsed_data) => {
|
||||
return parsed_data;
|
||||
}
|
||||
Err(err) => {
|
||||
if let Some(last_backtick_index) = err.to_string().rfind('`') {
|
||||
let msg = &err.to_string()[..=last_backtick_index];
|
||||
eprintln!("Error with Configuration file: {}", msg);
|
||||
} else {
|
||||
// If there are no backticks, you may want to handle this differently
|
||||
eprintln!("Error with Configuration file: {}", &err.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Config::default()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let links_str = "links";
|
||||
let links_dir = PathBuf::from(links_str);
|
||||
let blog_str = "blog";
|
||||
// let blog_dir = PathBuf::from(blog_str);
|
||||
|
||||
let config: Config = load_config();
|
||||
let output_dir = PathBuf::from(&config.output_dir);
|
||||
|
||||
// absolutely destroy any /public data, so we have a clean slate to write to
|
||||
file_utils::delete_public_dir();
|
||||
file_utils::create_dirs(&[
|
||||
"links",
|
||||
"blog"
|
||||
]);
|
||||
|
||||
let mut dirs: Vec<&str> = Vec::new();
|
||||
if config.blog != None {
|
||||
dirs.push(blog_str);
|
||||
}
|
||||
if config.links != None {
|
||||
dirs.push(links_str);
|
||||
}
|
||||
file_utils::create_dirs(dirs);
|
||||
|
||||
// Generate and Write Links
|
||||
let links_input = load_links("data/links/links.yaml");
|
||||
let links_output = render_links(links_input);
|
||||
match config.links {
|
||||
Some(links_config) => {
|
||||
create_links(links_dir, output_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);
|
||||
|
||||
load_config();
|
||||
// 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