summaryrefslogtreecommitdiff
path: root/src/watch/notify_event.rs
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-04-14 01:15:43 +0200
committermo8it <mo8it@proton.me>2024-04-14 01:15:43 +0200
commit5c0073a9485c4226e58b657cb49628919a28a942 (patch)
tree00cd8cb2ca8926e5e6cf68e44d51d14f77cc4695 /src/watch/notify_event.rs
parent2a26dfcb005d2a9ee24e920462b37dfb6d235c32 (diff)
Tolerate changes in the state file
Diffstat (limited to 'src/watch/notify_event.rs')
-rw-r--r--src/watch/notify_event.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/watch/notify_event.rs b/src/watch/notify_event.rs
new file mode 100644
index 0000000..0c8d669
--- /dev/null
+++ b/src/watch/notify_event.rs
@@ -0,0 +1,42 @@
+use notify_debouncer_mini::{DebounceEventResult, DebouncedEventKind};
+use std::{path::Path, sync::mpsc::Sender};
+
+use super::WatchEvent;
+
+pub struct DebounceEventHandler {
+ pub tx: Sender<WatchEvent>,
+ pub exercise_paths: &'static [&'static Path],
+}
+
+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;
+ }
+
+ self.exercise_paths
+ .iter()
+ .position(|path| event.path.ends_with(path))
+ })
+ .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);
+ }
+}