panres/src/main.rs

133 lines
4.0 KiB
Rust
Raw Normal View History

2019-05-06 03:27:01 +00:00
#![forbid(unsafe_code)]
2019-08-30 21:50:10 +00:00
use clap::{crate_authors, crate_version, App, AppSettings, Arg};
use crossbeam::crossbeam_channel::unbounded;
2020-07-24 02:04:24 +00:00
use json5::from_str;
2019-05-11 15:44:26 +00:00
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
2020-07-24 02:04:24 +00:00
use serde_json::Value;
2020-07-23 23:19:38 +00:00
use std::env;
use std::error::Error;
use std::fs::{create_dir_all, read_dir, read_to_string, write};
use std::process;
2019-08-30 21:50:10 +00:00
use tera::{Context, Error as TeraError, Tera};
2019-05-06 03:27:01 +00:00
2019-12-04 00:35:36 +00:00
const DEFAULT_TEMPLATE_DIR: &str = "templates";
const DEFAULT_OUTPUT_DIR: &str = "output";
2019-05-06 03:27:01 +00:00
2019-08-30 21:50:10 +00:00
fn main() -> Result<(), Box<dyn Error>> {
process::exit(match run() {
Ok(_) => 0,
Err(e) => {
eprintln!("{}", e);
1
}
})
}
fn env_or_default(env_name: &str, default: &str) -> String {
env::var(env_name).unwrap_or_else(|_| String::from(default))
}
fn run() -> Result<(), Box<dyn Error>> {
2020-07-24 02:04:24 +00:00
let config: Value = from_str(&read_to_string("config.json5")?)?;
2020-07-23 23:19:38 +00:00
let template_dir = env_or_default("PANRES_TEMPLATE_DIR", DEFAULT_TEMPLATE_DIR);
let output_dir = env_or_default("PANRES_OUTPUT_DIR", DEFAULT_OUTPUT_DIR);
2019-08-30 21:50:10 +00:00
let matches = get_args();
2020-07-23 23:19:38 +00:00
let tera = Tera::new(&format!("{}/**/*", template_dir))?;
let mut context = Context::new();
2019-08-30 21:50:10 +00:00
context.insert("config", &config);
let outputs: Vec<String> = matches
.values_of("output-format")
.unwrap_or_default()
2019-12-04 00:35:36 +00:00
.map(String::from)
2019-08-30 21:50:10 +00:00
.collect();
2020-07-23 23:19:38 +00:00
output(&tera, &context, &output_dir, &outputs, &template_dir)?;
2019-08-30 21:50:10 +00:00
if matches.is_present("watch") {
2020-07-23 23:19:38 +00:00
watch_mode(tera, context, output_dir, outputs, template_dir)?;
2019-08-30 21:50:10 +00:00
}
2020-07-23 23:19:38 +00:00
Ok(())
2019-08-30 21:50:10 +00:00
}
2019-05-06 03:27:01 +00:00
2019-08-30 21:50:10 +00:00
/// Returns the args passed by the user.
fn get_args() -> clap::ArgMatches<'static> {
App::new("panres")
2019-05-06 03:27:01 +00:00
.version(crate_version!())
.author(crate_authors!())
.about("Universal resume formatter")
2019-05-11 15:44:26 +00:00
.arg(Arg::with_name("watch").short("w").help("Watch for changes"))
2019-05-06 03:27:01 +00:00
.arg(
Arg::with_name("output-format")
.help("Specifies which output format you want")
2019-05-11 15:44:26 +00:00
.required(true)
2019-05-06 03:27:01 +00:00
.multiple(true),
)
.settings(&[AppSettings::ArgRequiredElseHelp])
2019-08-30 21:50:10 +00:00
.get_matches()
}
2019-05-11 15:44:26 +00:00
2019-08-30 21:50:10 +00:00
/// Usually never returns, unless there was an error initializing the watcher.
/// Handles watching for file changes, and reloads tera if there's a change.
fn watch_mode<'a>(
2020-07-23 23:19:38 +00:00
mut engine: Tera,
context: Context,
dir: String,
outputs: Vec<String>,
template_dir: String,
2019-08-30 21:50:10 +00:00
) -> Result<(), Box<dyn Error>> {
let (tx, rx) = unbounded();
2020-07-23 23:19:38 +00:00
let mut watcher: RecommendedWatcher = Watcher::new_immediate(move |res| tx.send(res).unwrap())?;
watcher.watch(&template_dir, RecursiveMode::Recursive)?;
2019-05-11 15:44:26 +00:00
2020-07-23 23:19:38 +00:00
for res in rx {
match res {
2019-08-30 21:50:10 +00:00
Err(e) => println!("{}", e),
Ok(event) => {
println!("got event {:?}", event);
2020-07-23 23:19:38 +00:00
engine.full_reload().expect("Failed to perform full reload");
output(&engine, &context, &dir, &outputs, &template_dir)
.expect("Failed to call output");
2019-05-11 15:44:26 +00:00
}
}
}
2020-07-23 23:19:38 +00:00
Ok(())
2019-05-11 15:44:26 +00:00
}
2019-05-06 03:27:01 +00:00
2019-08-30 21:50:10 +00:00
/// Parses the output values and generates a file for each format specified or
/// found, if told to generate all outputs.
2019-05-11 15:44:26 +00:00
fn output<'a>(
engine: &Tera,
context: &Context,
2019-05-11 15:48:47 +00:00
dir: &str,
2019-12-04 00:35:36 +00:00
outputs: &[String],
2019-05-11 15:44:26 +00:00
template_dir: &'a str,
2019-08-30 21:50:10 +00:00
) -> Result<(), Box<dyn Error>> {
if outputs.contains(&String::from("all")) {
2019-05-11 15:44:26 +00:00
for output in read_dir(template_dir)? {
2019-08-30 21:50:10 +00:00
write_file(engine, context, dir, &output?.file_name().to_str().unwrap())?;
2019-05-06 03:27:01 +00:00
}
} else {
for output in outputs {
2019-08-30 21:50:10 +00:00
write_file(engine, context, dir, &output)?;
2019-05-06 03:27:01 +00:00
}
}
Ok(())
}
2019-08-30 21:50:10 +00:00
/// Write out the post-template file to the output dir.
fn write_file(engine: &Tera, context: &Context, dir: &str, format: &str) -> Result<(), TeraError> {
2019-05-11 15:48:47 +00:00
create_dir_all(dir).expect("Could not create output dir");
2019-05-11 15:44:26 +00:00
write(
2019-05-11 15:48:47 +00:00
format!("{}/{}", dir, format),
2020-07-23 23:19:38 +00:00
engine.render(format, context)?,
2019-05-11 15:44:26 +00:00
)
.expect("to be able to write to output folder");
2019-05-06 03:27:01 +00:00
Ok(())
}