rustlings/src/watch/notify_event.rs

43 lines
1.4 KiB
Rust
Raw Normal View History

2024-04-10 17:02:12 +03:00
use notify_debouncer_mini::{DebounceEventResult, DebouncedEventKind};
2024-04-14 02:15:43 +03:00
use std::{path::Path, sync::mpsc::Sender};
2024-04-10 17:02:12 +03:00
use super::WatchEvent;
pub struct DebounceEventHandler {
pub tx: Sender<WatchEvent>,
2024-04-14 02:15:43 +03:00
pub exercise_paths: &'static [&'static Path],
2024-04-10 17:02:12 +03:00
}
impl notify_debouncer_mini::DebounceEventHandler for DebounceEventHandler {
fn handle_event(&mut self, event: DebounceEventResult) {
let event = match event {
Ok(event) => {
let Some(exercise_ind) = event
.iter()
.filter_map(|event| {
if event.kind != DebouncedEventKind::Any
|| !event.path.extension().is_some_and(|ext| ext == "rs")
{
return None;
}
2024-04-14 02:15:43 +03:00
self.exercise_paths
2024-04-10 17:02:12 +03:00
.iter()
2024-04-14 02:15:43 +03:00
.position(|path| event.path.ends_with(path))
2024-04-10 17:02:12 +03:00
})
.min()
else {
return;
};
WatchEvent::FileChange { exercise_ind }
}
Err(e) => WatchEvent::NotifyErr(e),
};
// An error occurs when the receiver is dropped.
// After dropping the receiver, the debouncer guard should also be dropped.
let _ = self.tx.send(event);
}
}