From 27287a50c3718ead96d5eb3cf73910fe892797f9 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Mon, 1 May 2017 18:11:18 +0900 Subject: [PATCH] Do not unwrap on watchers in serve cmd --- src/cmd/serve.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index dfcc4bb2..9bf2a8af 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -11,7 +11,7 @@ use staticfile::Static; use notify::{Watcher, RecursiveMode, watcher}; use ws::{WebSocket, Sender}; use gutenberg::Site; -use gutenberg::errors::{Result}; +use gutenberg::errors::{Result, ResultExt}; use ::{report_elapsed_time, unravel_errors}; @@ -71,6 +71,16 @@ pub fn serve(interface: &str, port: &str, config_file: &str) -> Result<()> { site.build()?; report_elapsed_time(start); + // Setup watchers + let (tx, rx) = channel(); + let mut watcher = watcher(tx, Duration::from_secs(2)).unwrap(); + watcher.watch("content/", RecursiveMode::Recursive) + .chain_err(|| format!("Can't watch the `content` folder. Does it exist?"))?; + watcher.watch("static/", RecursiveMode::Recursive) + .chain_err(|| format!("Can't watch the `static` folder. Does it exist?"))?; + watcher.watch("templates/", RecursiveMode::Recursive) + .chain_err(|| format!("Can't watch the `templates` folder. Does it exist?"))?; + let ws_address = format!("{}:{}", interface, "1112"); // Start a webserver that serves the `public` directory @@ -92,12 +102,6 @@ pub fn serve(interface: &str, port: &str, config_file: &str) -> Result<()> { ws_server.listen(&*ws_address).unwrap(); }); - // And finally watching/reacting on file changes - let (tx, rx) = channel(); - let mut watcher = watcher(tx, Duration::from_secs(2)).unwrap(); - watcher.watch("content/", RecursiveMode::Recursive).unwrap(); - watcher.watch("static/", RecursiveMode::Recursive).unwrap(); - watcher.watch("templates/", RecursiveMode::Recursive).unwrap(); let pwd = format!("{}", env::current_dir().unwrap().display()); println!("Listening for changes in {}/{{content, static, templates}}", pwd);