diff --git a/src/main.rs b/src/main.rs index 547bcf3..426c764 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use serde_json::Value; use std::env; use std::error::Error; use std::fs::{create_dir_all, read_dir, read_to_string, write}; +use std::path::{Path, PathBuf}; use std::process; use tera::{Context, Error as TeraError, Tera}; @@ -44,10 +45,12 @@ fn run() -> Result<(), Box> { .map(String::from) .collect(); + let template_dir = PathBuf::from(template_dir); + output(&tera, &context, &output_dir, &outputs, &template_dir)?; if matches.is_present("watch") { - watch_mode(tera, context, output_dir, outputs, template_dir)?; + watch_mode(tera, context, output_dir, outputs, &template_dir)?; } Ok(()) @@ -72,16 +75,16 @@ fn get_args() -> clap::ArgMatches<'static> { /// 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>( +fn watch_mode( mut engine: Tera, context: Context, dir: String, outputs: Vec, - template_dir: String, + template_dir: &Path, ) -> Result<(), Box> { let (tx, rx) = unbounded(); - let mut watcher: RecommendedWatcher = Watcher::new_immediate(move |res| tx.send(res).unwrap())?; - watcher.watch(&template_dir, RecursiveMode::Recursive)?; + let mut watcher: RecommendedWatcher = Watcher::new(move |res| tx.send(res).unwrap())?; + watcher.watch(template_dir, RecursiveMode::Recursive)?; for res in rx { match res { @@ -89,7 +92,7 @@ fn watch_mode<'a>( Ok(event) => { println!("got event {:?}", event); engine.full_reload().expect("Failed to perform full reload"); - output(&engine, &context, &dir, &outputs, &template_dir) + output(&engine, &context, &dir, &outputs, template_dir) .expect("Failed to call output"); } } @@ -100,12 +103,12 @@ fn watch_mode<'a>( /// Parses the output values and generates a file for each format specified or /// found, if told to generate all outputs. -fn output<'a>( +fn output( engine: &Tera, context: &Context, dir: &str, outputs: &[String], - template_dir: &'a str, + template_dir: &Path, ) -> Result<(), Box> { if outputs.contains(&String::from("all")) { for output in read_dir(template_dir)? {