-
Notifications
You must be signed in to change notification settings - Fork 229
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
use std::error::Error as StdError; | ||
|
||
#[cfg(target_os="macos")] pub use self::fsevent::FsEventWatcher; | ||
#[cfg(target_os="linux")] pub use self::inotify::INotifyWatcher; | ||
|
@@ -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>; | ||
|
@@ -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)) | ||
) | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} |
There was a problem hiding this comment.
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).