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

feat: display formatted errors. #56

Merged
merged 3 commits into from
Mar 27, 2016
Merged
Changes from 1 commit
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
31 changes: 31 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ use std::io;
use std::path::{Path, PathBuf};
use std::sync::mpsc::Sender;
use std::convert::AsRef;
use std::fmt;
// Permits retrieve err.description()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove that comment, it's fine (if someone removes the line in error it won't compile anyway).

use std::error::Error as StdError;

#[cfg(target_os="macos")] pub use self::fsevent::FsEventWatcher;
#[cfg(target_os="linux")] pub use self::inotify::INotifyWatcher;
Expand Down Expand Up @@ -53,6 +56,20 @@ pub enum Error {
WatchNotFound,
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let error = String::from(match *self {
Error::PathNotFound => "No path was found.",
Error::WatchNotFound => "No watch was found.",
Error::NotImplemented => "Not implemented.",
Error::Generic(ref err) => err.as_ref(),
Error::Io(ref err) => err.description(),
});

write!(f, "{}", error)
}
}

pub trait Watcher: Sized {
fn new(Sender<Event>) -> Result<Self, Error>;
fn watch<P: AsRef<Path>>(&mut self, P) -> Result<(), Error>;
Expand All @@ -67,3 +84,17 @@ pub trait Watcher: Sized {
pub fn new(tx: Sender<Event>) -> Result<RecommendedWatcher, Error> {
Watcher::new(tx)
}


#[test]
fn display_formatted_errors() {
let expected = "Some error";
assert_eq!(expected,
format!("{}", Error::Generic(String::from(expected))));
assert_eq!(
expected,
format!("{}",
Error::Io(io::Error::new(io::ErrorKind::Other, expected))
)
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's some kind of indentation mistake here. Also, the rest of the code is indented 2 chars, not 4.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooops I am used to ident that way by the (still under development) style guide. Let me fix it. :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I want to change rsnotify to the rust style some day, but for now 'll just keep it consistent.

}