2024-04-10 10:02:12 -04:00
|
|
|
use notify_debouncer_mini::{DebounceEventResult, DebouncedEventKind};
|
2024-04-13 20:41:19 -04:00
|
|
|
use std::sync::mpsc::Sender;
|
2024-04-10 10:02:12 -04:00
|
|
|
|
|
|
|
use super::WatchEvent;
|
|
|
|
|
|
|
|
pub struct DebounceEventHandler {
|
|
|
|
pub tx: Sender<WatchEvent>,
|
2024-04-25 08:44:12 -04:00
|
|
|
pub exercise_names: &'static [&'static [u8]],
|
2024-04-10 10:02:12 -04: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| {
|
2024-04-25 08:44:12 -04:00
|
|
|
if event.kind != DebouncedEventKind::Any {
|
2024-04-10 10:02:12 -04:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2024-04-25 08:44:12 -04:00
|
|
|
let file_name = event.path.file_name()?.to_str()?.as_bytes();
|
|
|
|
|
|
|
|
if file_name.len() < 4 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let (file_name_without_ext, ext) = file_name.split_at(file_name.len() - 3);
|
|
|
|
|
|
|
|
if ext != b".rs" {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.exercise_names
|
2024-04-10 10:02:12 -04:00
|
|
|
.iter()
|
2024-04-25 08:44:12 -04:00
|
|
|
.position(|exercise_name| *exercise_name == file_name_without_ext)
|
2024-04-10 10:02:12 -04: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);
|
|
|
|
}
|
|
|
|
}
|