Skip to content

Commit

Permalink
style(inotify): simplify result checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Šťastný committed Jan 12, 2025
1 parent 3e5b21e commit 31a8079
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions src/file_watcher/inotify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ pub struct FileWatchImpl {

impl FileWatcherImpl {
pub fn init() -> Result<FileWatcherImpl> {
let ino = match Inotify::init() {
Ok(i) => i,
Err(msg) => return Result::Err(msg),
};
let ino = Inotify::init()?;

Result::Ok(FileWatcherImpl {
inotify: ino,
Expand All @@ -30,15 +27,11 @@ impl FileWatcherImpl {
pub fn add_watch(&mut self, file_path: &PathBuf) -> Result<&FileWatchImpl> {
let mask: inotify::WatchMask = inotify::WatchMask::MODIFY;

let watch = match self.inotify.watches().add(file_path, mask) {
Ok(w) => w,
Err(msg) => return Result::Err(msg),
};

let watch = self.inotify.watches().add(file_path, mask)?;
let fw = FileWatchImpl { descriptor: watch };

self.watches.push(fw);
return Result::Ok(self.watches.last().unwrap());
Result::Ok(self.watches.last().unwrap())
}

pub fn rm_watch(&mut self, fw: &FileWatchImpl) -> Result<()> {
Expand All @@ -62,10 +55,7 @@ impl FileWatcherImpl {

pub fn any_events(&mut self) -> Result<bool> {
let mut buffer = [0; 1024];
let events = match self.inotify.read_events(&mut buffer) {
Result::Ok(ev) => ev,
Result::Err(err) => return Result::Err(err),
};
let events = self.inotify.read_events(&mut buffer)?;

Result::Ok(events.count() > 0)
}
Expand Down

0 comments on commit 31a8079

Please sign in to comment.