Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Result alias to allow for an easier use in lib users #72

Merged
merged 1 commit into from
Jun 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/fsevent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::convert::AsRef;
use std::thread;

use std::sync::mpsc::{channel, Sender, Receiver};
use super::{Error, Event, op, Watcher};
use super::{Error, Event, op, Result, Watcher};
use std::path::{Path, PathBuf};
use libc;

Expand Down Expand Up @@ -104,7 +104,7 @@ impl FsEventWatcher {
}
}

pub fn run(&mut self) -> Result<(), Error> {
pub fn run(&mut self) -> Result<()> {
if unsafe { cf::CFArrayGetCount(self.paths) } == 0 {
return Err(Error::PathNotFound);
}
Expand Down Expand Up @@ -206,7 +206,7 @@ pub unsafe extern "C" fn callback(


impl Watcher for FsEventWatcher {
fn new(tx: Sender<Event>) -> Result<FsEventWatcher, Error> {
fn new(tx: Sender<Event>) -> Result<FsEventWatcher> {
Ok(FsEventWatcher {
paths: unsafe {
cf::CFArrayCreateMutable(cf::kCFAllocatorDefault, 0, &cf::kCFTypeArrayCallBacks)
Expand All @@ -220,13 +220,13 @@ impl Watcher for FsEventWatcher {
})
}

fn watch<P: AsRef<Path>>(&mut self, path: P) -> Result<(), Error> {
fn watch<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
self.stop();
self.append_path(&path.as_ref().to_str().unwrap());
self.run()
}

fn unwatch<P: AsRef<Path>>(&mut self, path: P) -> Result<(), Error> {
fn unwatch<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
self.stop();
self.remove_path(&path.as_ref().to_str().unwrap());
// ignore return error: may be empty path list
Expand Down
18 changes: 9 additions & 9 deletions src/inotify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::path::{Path, PathBuf};
use std::sync::mpsc::{self, Sender};
use std::sync::{Arc, RwLock};
use std::thread::Builder as ThreadBuilder;
use super::{Error, Event, op, Op, Watcher};
use super::{Error, Event, op, Op, Result, Watcher};

mod flags;

Expand All @@ -27,8 +27,8 @@ struct INotifyHandler {
}

enum EventLoopMsg {
AddWatch(PathBuf, Sender<Result<(), Error>>),
RemoveWatch(PathBuf, Sender<Result<(), Error>>),
AddWatch(PathBuf, Sender<Result<()>>),
RemoveWatch(PathBuf, Sender<Result<()>>),
Shutdown,
}

Expand Down Expand Up @@ -85,7 +85,7 @@ impl mio::Handler for INotifyHandler {
}

impl INotifyHandler {
fn add_watch_recursively(&mut self, path: PathBuf) -> Result<(), Error> {
fn add_watch_recursively(&mut self, path: PathBuf) -> Result<()> {
match metadata(&path) {
Err(e) => return Err(Error::Io(e)),
Ok(m) => {
Expand All @@ -105,7 +105,7 @@ impl INotifyHandler {
Ok(())
}

fn add_watch(&mut self, path: PathBuf) -> Result<(), Error> {
fn add_watch(&mut self, path: PathBuf) -> Result<()> {
let mut watching = flags::IN_ATTRIB | flags::IN_CREATE | flags::IN_DELETE |
flags::IN_DELETE_SELF | flags::IN_MODIFY |
flags::IN_MOVED_FROM |
Expand All @@ -126,7 +126,7 @@ impl INotifyHandler {
}
}

fn remove_watch(&mut self, path: PathBuf) -> Result<(), Error> {
fn remove_watch(&mut self, path: PathBuf) -> Result<()> {
match self.watches.remove(&path) {
None => Err(Error::WatchNotFound),
Some(p) => {
Expand Down Expand Up @@ -182,7 +182,7 @@ fn handle_event(event: wrapper::Event,
}

impl Watcher for INotifyWatcher {
fn new(tx: Sender<Event>) -> Result<INotifyWatcher, Error> {
fn new(tx: Sender<Event>) -> Result<INotifyWatcher> {
INotify::init()
.and_then(|inotify| EventLoop::new().map(|l| (inotify, l)))
.and_then(|(inotify, mut event_loop)| {
Expand Down Expand Up @@ -215,7 +215,7 @@ impl Watcher for INotifyWatcher {
.map_err(Error::Io)
}

fn watch<P: AsRef<Path>>(&mut self, path: P) -> Result<(), Error> {
fn watch<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
let (tx, rx) = mpsc::channel();
let msg = EventLoopMsg::AddWatch(path.as_ref().to_owned(), tx);

Expand All @@ -224,7 +224,7 @@ impl Watcher for INotifyWatcher {
rx.recv().unwrap()
}

fn unwatch<P: AsRef<Path>>(&mut self, path: P) -> Result<(), Error> {
fn unwatch<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
let (tx, rx) = mpsc::channel();
let msg = EventLoopMsg::RemoveWatch(path.as_ref().to_owned(), tx);

Expand Down
13 changes: 8 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::sync::mpsc::Sender;
use std::convert::AsRef;
use std::fmt;
use std::error::Error as StdError;
use std::result::Result as StdResult;

#[cfg(target_os="macos")]
pub use self::fsevent::FsEventWatcher;
Expand Down Expand Up @@ -49,7 +50,7 @@ pub mod op {
#[derive(Debug)]
pub struct Event {
pub path: Option<PathBuf>,
pub op: Result<Op, Error>,
pub op: Result<Op>,
}

unsafe impl Send for Event {}
Expand Down Expand Up @@ -77,10 +78,12 @@ impl fmt::Display for Error {
}
}

pub type Result<T> = StdResult<T, Error>;

pub trait Watcher: Sized {
fn new(Sender<Event>) -> Result<Self, Error>;
fn watch<P: AsRef<Path>>(&mut self, P) -> Result<(), Error>;
fn unwatch<P: AsRef<Path>>(&mut self, P) -> Result<(), Error>;
fn new(Sender<Event>) -> Result<Self>;
fn watch<P: AsRef<Path>>(&mut self, P) -> Result<()>;
fn unwatch<P: AsRef<Path>>(&mut self, P) -> Result<()>;
}

#[cfg(target_os = "linux")]
Expand All @@ -92,7 +95,7 @@ pub type RecommendedWatcher = ReadDirectoryChangesWatcher;
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
pub type RecommendedWatcher = PollWatcher;

pub fn new(tx: Sender<Event>) -> Result<RecommendedWatcher, Error> {
pub fn new(tx: Sender<Event>) -> Result<RecommendedWatcher> {
Watcher::new(tx)
}

Expand Down
8 changes: 4 additions & 4 deletions src/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

use std::sync::mpsc::Sender;
use std::path::Path;
use super::{Error, Event, Watcher};
use super::{Event, Result, Watcher};

pub struct NullWatcher;

impl Watcher for NullWatcher {
fn new(tx: Sender<Event>) -> Result<NullWatcher, Error> {
fn new(tx: Sender<Event>) -> Result<NullWatcher> {
Ok(NullWatcher)
}

fn watch<P: AsRef<Path>>(&mut self, path: P) -> Result<(), Error> {
fn watch<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
Ok(())
}

fn unwatch<P: AsRef<Path>>(&mut self, path: P) -> Result<(), Error> {
fn unwatch<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
Ok(())
}
}
10 changes: 5 additions & 5 deletions src/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::{Arc, RwLock};
use std::sync::mpsc::Sender;
use std::fs;
use std::thread;
use super::{Error, Event, op, Watcher};
use super::{Error, Event, op, Result, Watcher};
use std::path::{Path, PathBuf};
use std::time::Duration;
use self::walkdir::WalkDir;
Expand All @@ -19,7 +19,7 @@ pub struct PollWatcher {
}

impl PollWatcher {
pub fn with_delay(tx: Sender<Event>, delay: u32) -> Result<PollWatcher, Error> {
pub fn with_delay(tx: Sender<Event>, delay: u32) -> Result<PollWatcher> {
let mut p = PollWatcher {
tx: tx,
watches: Arc::new(RwLock::new(HashSet::new())),
Expand Down Expand Up @@ -130,16 +130,16 @@ impl PollWatcher {
}

impl Watcher for PollWatcher {
fn new(tx: Sender<Event>) -> Result<PollWatcher, Error> {
fn new(tx: Sender<Event>) -> Result<PollWatcher> {
PollWatcher::with_delay(tx, 10)
}

fn watch<P: AsRef<Path>>(&mut self, path: P) -> Result<(), Error> {
fn watch<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
(*self.watches).write().unwrap().insert(path.as_ref().to_path_buf());
Ok(())
}

fn unwatch<P: AsRef<Path>>(&mut self, path: P) -> Result<(), Error> {
fn unwatch<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
if (*self.watches).write().unwrap().remove(path.as_ref()) {
Ok(())
} else {
Expand Down
20 changes: 10 additions & 10 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::os::windows::ffi::{OsStrExt, OsStringExt};
use std::thread;
use std::os::raw::c_void;

use super::{Event, Error, op, Op, Watcher};
use super::{Event, Error, op, Op, Result, Watcher};

const BUF_SIZE: u32 = 16384;

Expand Down Expand Up @@ -53,15 +53,15 @@ struct ReadDirectoryChangesServer {
rx: Receiver<Action>,
tx: Sender<Event>,
meta_tx: Sender<MetaEvent>,
cmd_tx: Sender<Result<PathBuf, Error>>,
cmd_tx: Sender<Result<PathBuf>>,
watches: HashMap<PathBuf, WatchState>,
wakeup_sem: HANDLE,
}

impl ReadDirectoryChangesServer {
fn start(event_tx: Sender<Event>,
meta_tx: Sender<MetaEvent>,
cmd_tx: Sender<Result<PathBuf, Error>>,
cmd_tx: Sender<Result<PathBuf>>,
wakeup_sem: HANDLE)
-> Sender<Action> {

Expand Down Expand Up @@ -124,7 +124,7 @@ impl ReadDirectoryChangesServer {
}
}

fn add_watch(&mut self, path: PathBuf) -> Result<PathBuf, Error> {
fn add_watch(&mut self, path: PathBuf) -> Result<PathBuf> {
// path must exist and be either a file or directory
if !path.is_dir() && !path.is_file() {
return Err(Error::Generic("Input watch path is neither a file nor a directory."
Expand Down Expand Up @@ -331,14 +331,14 @@ unsafe extern "system" fn handle_event(error_code: u32,

pub struct ReadDirectoryChangesWatcher {
tx: Sender<Action>,
cmd_rx: Receiver<Result<PathBuf, Error>>,
cmd_rx: Receiver<Result<PathBuf>>,
wakeup_sem: HANDLE,
}

impl ReadDirectoryChangesWatcher {
pub fn create(event_tx: Sender<Event>,
meta_tx: Sender<MetaEvent>)
-> Result<ReadDirectoryChangesWatcher, Error> {
-> Result<ReadDirectoryChangesWatcher> {
let (cmd_tx, cmd_rx) = channel();

let wakeup_sem = unsafe {
Expand Down Expand Up @@ -366,7 +366,7 @@ impl ReadDirectoryChangesWatcher {
}
}

fn send_action_require_ack(&mut self, action: Action, pb: &PathBuf) -> Result<(), Error> {
fn send_action_require_ack(&mut self, action: Action, pb: &PathBuf) -> Result<()> {
match self.tx.send(action) {
Err(_) => Err(Error::Generic("Error sending to internal channel".to_owned())),
Ok(_) => {
Expand Down Expand Up @@ -399,13 +399,13 @@ impl ReadDirectoryChangesWatcher {
}

impl Watcher for ReadDirectoryChangesWatcher {
fn new(event_tx: Sender<Event>) -> Result<ReadDirectoryChangesWatcher, Error> {
fn new(event_tx: Sender<Event>) -> Result<ReadDirectoryChangesWatcher> {
// create dummy channel for meta event
let (meta_tx, _) = channel();
ReadDirectoryChangesWatcher::create(event_tx, meta_tx)
}

fn watch<P: AsRef<Path>>(&mut self, path: P) -> Result<(), Error> {
fn watch<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
// path must exist and be either a file or directory
let pb = path.as_ref().to_path_buf();
if !pb.is_dir() && !pb.is_file() {
Expand All @@ -415,7 +415,7 @@ impl Watcher for ReadDirectoryChangesWatcher {
self.send_action_require_ack(Action::Watch(path.as_ref().to_path_buf()), &pb)
}

fn unwatch<P: AsRef<Path>>(&mut self, path: P) -> Result<(), Error> {
fn unwatch<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
let res = self.tx
.send(Action::Unwatch(path.as_ref().to_path_buf()))
.map_err(|_| Error::Generic("Error sending to internal channel".to_owned()));
Expand Down
16 changes: 8 additions & 8 deletions tests/notify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fn validate_recv(rx: Receiver<Event>, evs: Vec<(&Path, Op)>) -> Vec<Event> {
#[cfg(target_os = "windows")]
// Windows needs to test this differently since it can't watch files that don't exist yet.
fn validate_watch_single_file<F, W>(ctor: F) where
F: Fn(Sender<Event>) -> Result<W, Error>, W: Watcher {
F: Fn(Sender<Event>) -> Result<W>, W: Watcher {

let (tx, rx) = channel();
let mut w = ctor(tx).unwrap();
Expand Down Expand Up @@ -127,7 +127,7 @@ fn validate_watch_single_file<F, W>(ctor: F) where

#[cfg(not(target_os = "windows"))]
fn validate_watch_single_file<F, W>(ctor: F) where
F: Fn(Sender<Event>) -> Result<W, Error>, W: Watcher {
F: Fn(Sender<Event>) -> Result<W>, W: Watcher {
let mut file = NamedTempFile::new().unwrap();
let (tx, rx) = channel();
let mut w = ctor(tx).unwrap();
Expand All @@ -141,7 +141,7 @@ fn validate_watch_single_file<F, W>(ctor: F) where

#[cfg(not(target_os = "linux"))]
fn validate_watch_dir<F, W>(ctor: F) where
F: Fn(Sender<Event>) -> Result<W, Error>, W: Watcher {
F: Fn(Sender<Event>) -> Result<W>, W: Watcher {
let dir = TempDir::new("dir").unwrap();
let dir1 = TempDir::new_in(dir.path(), "dir1").unwrap();
let dir2 = TempDir::new_in(dir.path(), "dir2").unwrap();
Expand Down Expand Up @@ -191,7 +191,7 @@ fn watch_single_file_poll() {
#[test]
fn new_inotify() {
let (tx, _) = channel();
let w: Result<INotifyWatcher, Error> = Watcher::new(tx);
let w: Result<INotifyWatcher> = Watcher::new(tx);
match w {
Ok(_) => assert!(true),
Err(_) => assert!(false)
Expand All @@ -202,7 +202,7 @@ fn new_inotify() {
#[test]
fn new_fsevent() {
let (tx, _) = channel();
let w: Result<FsEventWatcher, Error> = Watcher::new(tx);
let w: Result<FsEventWatcher> = Watcher::new(tx);
match w {
Ok(_) => assert!(true),
Err(_) => assert!(false)
Expand All @@ -212,7 +212,7 @@ fn new_fsevent() {
#[test]
fn new_null() {
let (tx, _) = channel();
let w: Result<NullWatcher, Error> = Watcher::new(tx);
let w: Result<NullWatcher> = Watcher::new(tx);
match w {
Ok(_) => assert!(true),
Err(_) => assert!(false)
Expand All @@ -222,7 +222,7 @@ fn new_null() {
#[test]
fn new_poll() {
let (tx, _) = channel();
let w: Result<PollWatcher, Error> = Watcher::new(tx);
let w: Result<PollWatcher> = Watcher::new(tx);
match w {
Ok(_) => assert!(true),
Err(_) => assert!(false)
Expand All @@ -232,7 +232,7 @@ fn new_poll() {
#[test]
fn new_recommended() {
let (tx, _) = channel();
let w: Result<RecommendedWatcher, Error> = Watcher::new(tx);
let w: Result<RecommendedWatcher> = Watcher::new(tx);
match w {
Ok(_) => assert!(true),
Err(_) => assert!(false)
Expand Down