Don't panic on error in watch
This commit is contained in:
parent
89805cd351
commit
8be3cb4a88
27
src/lib.rs
27
src/lib.rs
|
@ -1,7 +1,7 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate log;
|
extern crate log;
|
||||||
|
|
||||||
use std::{io, thread};
|
use std::{io, thread, fmt};
|
||||||
use std::env::current_dir;
|
use std::env::current_dir;
|
||||||
use std::process::{Command, ExitStatus};
|
use std::process::{Command, ExitStatus};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
@ -34,6 +34,16 @@ impl From<io::Error> for Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Error {
|
||||||
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
Error::NoBuilderFound => write!(fmt, "no builder found"),
|
||||||
|
Error::IoError(e) => write!(fmt, "i/o error occured: {}", e),
|
||||||
|
Error::CommandError(command, _, status) => write!(fmt, "command {} exited with status code {}", command, status),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Tests if a file is in a directory.
|
/// Tests if a file is in a directory.
|
||||||
pub fn contains_file(path: &str, file: &str) -> bool {
|
pub fn contains_file(path: &str, file: &str) -> bool {
|
||||||
let mut path = PathBuf::from(path);
|
let mut path = PathBuf::from(path);
|
||||||
|
@ -118,8 +128,19 @@ pub fn watch<P: AsRef<Path>>(p: P) -> Result<(), Error> {
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
let mut builder = GeneralBuilder::new();
|
let mut builder = GeneralBuilder::new();
|
||||||
let (tx, rx) = channel();
|
let (tx, rx) = channel();
|
||||||
let mut watcher = watcher(tx, Duration::from_millis(200)).unwrap();
|
|
||||||
watcher.watch(&path, RecursiveMode::Recursive).unwrap();
|
let mut watcher = match watcher(tx, Duration::from_millis(200)) {
|
||||||
|
Ok(w) => w,
|
||||||
|
Err(e) => {
|
||||||
|
warn!("couldn't watch directory {}: {}", path.display(), e);
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Err(e) = watcher.watch(&path, RecursiveMode::Recursive) {
|
||||||
|
warn!("couldn't watch directory {}: {}", path.display(), e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
info!("watching {}", path.display());
|
info!("watching {}", path.display());
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@ fn main() {
|
||||||
|
|
||||||
if args.len() == 3 && args[1] == "-w" {
|
if args.len() == 3 && args[1] == "-w" {
|
||||||
if let Err(e) = watch(&args[2]) {
|
if let Err(e) = watch(&args[2]) {
|
||||||
error!("{:?}", e);
|
error!("{}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue