rust-ssg/src/main.rs
2023-10-15 00:45:11 -04:00

70 lines
2.1 KiB
Rust

mod links;
mod file_utils;
mod blog;
use crate::links::{LinksConfig, create_links};
use crate::blog::{BlogConfig};
use serde::{Serialize, Deserialize};
use std::path::PathBuf;
use std::env;
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct Config {
output_dir: String,
links: Option<LinksConfig>,
blog: Option<BlogConfig>,
}
fn load_config(filename: &str) -> Config {
let config_file = std::fs::File::open(filename).expect("Could not find config.yaml file");
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 {
let msg = &err.to_string();
eprintln!("Error with Configuration file: {}", msg);
}
}
}
Config::default()
}
fn main() {
let pwd = env::current_dir().expect("Can not generate PWD");
// TODO: Handle Panic:
// thread 'main' panicked at 'Could not find config.yaml file: Os { code: 2, kind: NotFound, message: "No such file or directory" }', src/main.rs:19:53
let config: Config = load_config("./config.yaml");
let base_path = pwd.join(PathBuf::from(&config.output_dir));
if let Some(base_dir) = file_utils::resolve_and_handle_path(&pwd, &base_path) {
// absolutely destroy any /public data, so we have a clean slate to write to
// Generate and Write Links
match config.links {
Some(links_config) => {
let links_path = base_dir.join(PathBuf::from("links"));
file_utils::delete_output_dir(&links_path);
if let Some(links_dir) = file_utils::resolve_and_handle_path(&base_dir, &links_path) {
create_links(links_dir, &links_config);
}
},
None => {}
}
}
}