summaryrefslogtreecommitdiff
path: root/src/watch/debounce_event.rs
blob: 1dc92cb49b75dd8b77a06b3f061387ed75269b1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use notify_debouncer_mini::{DebounceEventResult, DebouncedEventKind};
use std::sync::mpsc::Sender;

use crate::exercise::Exercise;

use super::WatchEvent;

pub struct DebounceEventHandler {
    pub tx: Sender<WatchEvent>,
    pub exercises: &'static [Exercise],
}

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.exercises
                            .iter()
                            .position(|exercise| event.path.ends_with(&exercise.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);
    }
}