summaryrefslogtreecommitdiff
path: root/src/watch/notify_event.rs
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-07-05 13:39:50 +0200
committermo8it <mo8it@proton.me>2024-07-05 13:39:50 +0200
commit7123c7ae3a9605fbe962e4ef0a0f1424cd16fef8 (patch)
treec67f7e62bb9a179ae4fdbab492501cb6847e64c7 /src/watch/notify_event.rs
parent77b687d501771c24bd83294d97b8e6f9ffa92d6b (diff)
parent4d9c346a173bb722b929f3ea3c00f84954483e24 (diff)
Merge remote-tracking branch 'upstream/main' into fix-enum-variant-inconsistency
Diffstat (limited to 'src/watch/notify_event.rs')
-rw-r--r--src/watch/notify_event.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/watch/notify_event.rs b/src/watch/notify_event.rs
new file mode 100644
index 0000000..7471640
--- /dev/null
+++ b/src/watch/notify_event.rs
@@ -0,0 +1,52 @@
+use notify_debouncer_mini::{DebounceEventResult, DebouncedEventKind};
+use std::sync::mpsc::Sender;
+
+use super::WatchEvent;
+
+pub struct NotifyEventHandler {
+ pub tx: Sender<WatchEvent>,
+ /// Used to report which exercise was modified.
+ pub exercise_names: &'static [&'static [u8]],
+}
+
+impl notify_debouncer_mini::DebounceEventHandler for NotifyEventHandler {
+ fn handle_event(&mut self, input_event: DebounceEventResult) {
+ let output_event = match input_event {
+ Ok(input_event) => {
+ let Some(exercise_ind) = input_event
+ .iter()
+ .filter_map(|input_event| {
+ if input_event.kind != DebouncedEventKind::Any {
+ return None;
+ }
+
+ let file_name = input_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
+ .iter()
+ .position(|exercise_name| *exercise_name == file_name_without_ext)
+ })
+ .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(output_event);
+ }
+}