notify_types/
debouncer_mini.rs1use std::path::PathBuf;
2
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5
6#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
8#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9#[non_exhaustive]
10pub enum DebouncedEventKind {
11 Any,
13 AnyContinuous,
15}
16
17#[derive(Clone, Debug, Eq, Hash, PartialEq)]
21#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
22pub struct DebouncedEvent {
23 pub path: PathBuf,
25 pub kind: DebouncedEventKind,
27}
28
29impl DebouncedEvent {
30 #[inline(always)]
31 #[must_use]
32 pub fn new(path: PathBuf, kind: DebouncedEventKind) -> Self {
33 Self { path, kind }
34 }
35}
36
37#[derive(Clone, Debug, Eq, Hash, PartialEq)]
41#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
42pub enum DebouncedEventBatch {
43 Events(Vec<DebouncedEvent>),
45 Rescan,
52}
53
54#[cfg(all(test, feature = "serde"))]
55mod tests {
56 use super::*;
57 use serde_json::json;
58
59 #[test]
60 fn serialize_and_deserialize_batches() {
61 for (batch, json) in [
62 (DebouncedEventBatch::Rescan, json!("Rescan")),
63 (
64 DebouncedEventBatch::Events(vec![
65 DebouncedEvent::new("file.txt".into(), DebouncedEventKind::Any),
66 DebouncedEvent::new("active.txt".into(), DebouncedEventKind::AnyContinuous),
67 ]),
68 json!({"Events": [
69 {"path": "file.txt", "kind": "Any"},
70 {"path": "active.txt", "kind": "AnyContinuous"},
71 ]}),
72 ),
73 ] {
74 assert_eq!(serde_json::to_value(&batch).unwrap(), json);
75 assert_eq!(
76 serde_json::from_value::<DebouncedEventBatch>(json).unwrap(),
77 batch,
78 );
79 }
80 }
81}