Skip to main content

notify_types/
event.rs

1//! The `Event` type and the hierarchical `EventKind` descriptor.
2
3use std::{
4    fmt,
5    hash::{Hash, Hasher},
6    path::PathBuf,
7};
8
9use bitflags::bitflags;
10
11#[cfg(feature = "serde")]
12use serde::{Deserialize, Serialize};
13
14/// An event describing open or close operations on files.
15#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
16#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
17#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
18pub enum AccessMode {
19    /// The catch-all case, to be used when the specific kind of event is unknown.
20    Any,
21
22    /// An event emitted when the file is executed, or the folder opened.
23    Execute,
24
25    /// An event emitted when the file is opened for reading.
26    Read,
27
28    /// An event emitted when the file is opened for writing.
29    Write,
30
31    /// An event which specific kind is known but cannot be represented otherwise.
32    Other,
33}
34
35/// An event describing non-mutating access operations on files.
36#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
37#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
38#[cfg_attr(feature = "serde", serde(tag = "kind", content = "mode"))]
39#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
40pub enum AccessKind {
41    /// The catch-all case, to be used when the specific kind of event is unknown.
42    Any,
43
44    /// An event emitted when the file is read.
45    Read,
46
47    /// An event emitted when the file, or a handle to the file, is opened.
48    Open(AccessMode),
49
50    /// An event emitted when the file, or a handle to the file, is closed.
51    Close(AccessMode),
52
53    /// An event which specific kind is known but cannot be represented otherwise.
54    Other,
55}
56
57/// An event describing creation operations on files.
58#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
59#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
60#[cfg_attr(feature = "serde", serde(tag = "kind"))]
61#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
62pub enum CreateKind {
63    /// The catch-all case, to be used when the specific kind of event is unknown.
64    Any,
65
66    /// An event which results in the creation of a file.
67    File,
68
69    /// An event which results in the creation of a folder.
70    Folder,
71
72    /// An event which specific kind is known but cannot be represented otherwise.
73    Other,
74}
75
76/// An event emitted when the data content of a file is changed.
77#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
78#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
79#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
80pub enum DataChange {
81    /// The catch-all case, to be used when the specific kind of event is unknown.
82    Any,
83
84    /// An event emitted when the size of the data is changed.
85    Size,
86
87    /// An event emitted when the content of the data is changed.
88    Content,
89
90    /// An event which specific kind is known but cannot be represented otherwise.
91    Other,
92}
93
94/// An event emitted when the metadata of a file or folder is changed.
95#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
96#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
97#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
98pub enum MetadataKind {
99    /// The catch-all case, to be used when the specific kind of event is unknown.
100    Any,
101
102    /// An event emitted when the access time of the file or folder is changed.
103    AccessTime,
104
105    /// An event emitted when the write or modify time of the file or folder is changed.
106    WriteTime,
107
108    /// An event emitted when the permissions of the file or folder are changed.
109    Permissions,
110
111    /// An event emitted when the ownership of the file or folder is changed.
112    Ownership,
113
114    /// An event emitted when an extended attribute of the file or folder is changed.
115    ///
116    /// If the extended attribute's name or type is known, it should be provided in the
117    /// `Info` event attribute.
118    Extended,
119
120    /// An event which specific kind is known but cannot be represented otherwise.
121    Other,
122}
123
124/// An event emitted when the name of a file or folder is changed.
125#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
126#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
127#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
128pub enum RenameMode {
129    /// The catch-all case, to be used when the specific kind of event is unknown.
130    Any,
131
132    /// An event emitted on the file or folder resulting from a rename.
133    To,
134
135    /// An event emitted on the file or folder that was renamed.
136    From,
137
138    /// A single event emitted with both the `From` and `To` paths.
139    ///
140    /// This event should be emitted when both source and target are known. The paths should be
141    /// provided in this exact order (from, to).
142    Both,
143
144    /// An event which specific kind is known but cannot be represented otherwise.
145    Other,
146}
147
148/// An event describing mutation of content, name, or metadata.
149#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
150#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
151#[cfg_attr(feature = "serde", serde(tag = "kind", content = "mode"))]
152#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
153pub enum ModifyKind {
154    /// The catch-all case, to be used when the specific kind of event is unknown.
155    Any,
156
157    /// An event emitted when the data content of a file is changed.
158    Data(DataChange),
159
160    /// An event emitted when the metadata of a file or folder is changed.
161    Metadata(MetadataKind),
162
163    /// An event emitted when the name of a file or folder is changed.
164    #[cfg_attr(feature = "serde", serde(rename = "rename"))]
165    Name(RenameMode),
166
167    /// An event which specific kind is known but cannot be represented otherwise.
168    Other,
169}
170
171/// An event describing removal operations on files.
172#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
173#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
174#[cfg_attr(feature = "serde", serde(tag = "kind"))]
175#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
176pub enum RemoveKind {
177    /// The catch-all case, to be used when the specific kind of event is unknown.
178    Any,
179
180    /// An event emitted when a file is removed.
181    File,
182
183    /// An event emitted when a folder is removed.
184    Folder,
185
186    /// An event which specific kind is known but cannot be represented otherwise.
187    Other,
188}
189
190/// Top-level event kind.
191///
192/// This is arguably the most important classification for events. All subkinds below this one
193/// represent details that may or may not be available for any particular backend, but most tools
194/// and Notify systems will only care about which of these four general kinds an event is about.
195#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
196#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
197#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
198#[cfg_attr(
199    all(feature = "serde", not(feature = "serialization-compat-6")),
200    serde(tag = "type")
201)]
202pub enum EventKind {
203    /// The catch-all event kind, for unsupported/unknown events.
204    ///
205    /// This variant should be used as the "else" case when mapping native kernel bitmasks or
206    /// bitmaps, such that if the mask is ever extended with new event types the backend will not
207    /// gain bugs due to not matching new unknown event types.
208    ///
209    /// This variant is also the default variant used when Notify is in "imprecise" mode.
210    #[default]
211    Any,
212
213    /// An event describing non-mutating access operations on files.
214    ///
215    /// This event is about opening and closing file handles, as well as executing files, and any
216    /// other such event that is about accessing files, folders, or other structures rather than
217    /// mutating them.
218    ///
219    /// Only some platforms are capable of generating these.
220    Access(AccessKind),
221
222    /// An event describing creation operations on files.
223    ///
224    /// This event is about the creation of files, folders, or other structures but not about e.g.
225    /// writing new content into them.
226    Create(CreateKind),
227
228    /// An event describing mutation of content, name, or metadata.
229    ///
230    /// This event is about the mutation of files', folders', or other structures' content, name
231    /// (path), or associated metadata (attributes).
232    Modify(ModifyKind),
233
234    /// An event describing removal operations on files.
235    ///
236    /// This event is about the removal of files, folders, or other structures but not e.g. erasing
237    /// content from them. This may also be triggered for renames/moves that move files _out of the
238    /// watched subpath_.
239    ///
240    /// Some editors also trigger Remove events when saving files as they may opt for removing (or
241    /// renaming) the original then creating a new file in-place.
242    Remove(RemoveKind),
243
244    /// An event not fitting in any of the above four categories.
245    ///
246    /// This may be used for meta-events about the watch itself.
247    Other,
248}
249
250impl EventKind {
251    /// Indicates whether an event is an Access variant.
252    #[must_use]
253    pub fn is_access(&self) -> bool {
254        matches!(self, EventKind::Access(_))
255    }
256
257    /// Indicates whether an event is a Create variant.
258    #[must_use]
259    pub fn is_create(&self) -> bool {
260        matches!(self, EventKind::Create(_))
261    }
262
263    /// Indicates whether an event is a Modify variant.
264    #[must_use]
265    pub fn is_modify(&self) -> bool {
266        matches!(self, EventKind::Modify(_))
267    }
268
269    /// Indicates whether an event is a Remove variant.
270    #[must_use]
271    pub fn is_remove(&self) -> bool {
272        matches!(self, EventKind::Remove(_))
273    }
274
275    /// Indicates whether an event is an Other variant.
276    #[must_use]
277    pub fn is_other(&self) -> bool {
278        matches!(self, EventKind::Other)
279    }
280}
281
282bitflags! {
283    /// A bitmask specifying which event kinds to monitor.
284    ///
285    /// This type allows fine-grained control over which filesystem events are reported.
286    /// On backends that support kernel-level filtering (inotify), the mask is
287    /// translated to native flags for optimal performance. On other backends (kqueue,
288    /// Windows, FSEvents, PollWatcher), filtering is applied in userspace.
289    ///
290    /// # Examples
291    ///
292    /// ```
293    /// use notify_types::event::EventKindMask;
294    ///
295    /// // Monitor only file creations and deletions
296    /// let mask = EventKindMask::CREATE | EventKindMask::REMOVE;
297    ///
298    /// // Monitor everything including access events
299    /// let all = EventKindMask::ALL;
300    ///
301    /// // Default: includes all events (matches Config::default())
302    /// let default = EventKindMask::default();
303    /// assert_eq!(default, EventKindMask::ALL);
304    /// ```
305    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
306    #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
307    pub struct EventKindMask: u32 {
308        /// Monitor file/folder creation events.
309        const CREATE = 0b0000_0001;
310
311        /// Monitor file/folder removal events.
312        const REMOVE = 0b0000_0010;
313
314        /// Monitor data modification events (content/size changes).
315        const MODIFY_DATA = 0b0000_0100;
316
317        /// Monitor metadata modification events (permissions, timestamps, etc).
318        const MODIFY_META = 0b0000_1000;
319
320        /// Monitor name/rename events.
321        const MODIFY_NAME = 0b0001_0000;
322
323        /// Monitor file open events.
324        const ACCESS_OPEN = 0b0010_0000;
325
326        /// Monitor file close events after writing.
327        /// This fires when a file opened for writing is closed.
328        const ACCESS_CLOSE = 0b0100_0000;
329
330        /// Monitor file close events after read-only access.
331        /// This fires when a file opened for reading (not writing) is closed.
332        /// Note: This can be very noisy and may cause queue overflow on busy systems.
333        const ACCESS_CLOSE_NOWRITE = 0b1000_0000;
334
335        /// All modify events (data, metadata, and name changes).
336        const ALL_MODIFY = Self::MODIFY_DATA.bits() | Self::MODIFY_META.bits() | Self::MODIFY_NAME.bits();
337
338        /// All access events (open, close-write, and close-nowrite).
339        const ALL_ACCESS = Self::ACCESS_OPEN.bits() | Self::ACCESS_CLOSE.bits() | Self::ACCESS_CLOSE_NOWRITE.bits();
340
341        /// Core events: create, remove, and all modify events.
342        const CORE = Self::CREATE.bits() | Self::REMOVE.bits() | Self::ALL_MODIFY.bits();
343
344        /// All events including access events.
345        /// This is the default.
346        const ALL = Self::CORE.bits() | Self::ALL_ACCESS.bits();
347    }
348}
349
350impl Default for EventKindMask {
351    fn default() -> Self {
352        EventKindMask::ALL
353    }
354}
355
356impl EventKindMask {
357    /// Returns whether the given event kind matches this mask.
358    ///
359    /// `EventKind::Any` and `EventKind::Other` always pass regardless of the mask,
360    /// as they represent meta-events that should not be filtered.
361    ///
362    /// # Examples
363    ///
364    /// ```
365    /// use notify_types::event::{EventKindMask, EventKind, CreateKind, AccessKind, AccessMode};
366    ///
367    /// let mask = EventKindMask::CREATE;
368    /// assert!(mask.matches(&EventKind::Create(CreateKind::File)));
369    /// assert!(!mask.matches(&EventKind::Access(AccessKind::Open(AccessMode::Read))));
370    ///
371    /// // Any and Other always pass
372    /// let empty = EventKindMask::empty();
373    /// assert!(empty.matches(&EventKind::Any));
374    /// assert!(empty.matches(&EventKind::Other));
375    /// ```
376    #[must_use]
377    pub fn matches(&self, kind: &EventKind) -> bool {
378        match kind {
379            // Meta-events always pass
380            EventKind::Any | EventKind::Other => true,
381
382            // Create events
383            EventKind::Create(_) => self.intersects(EventKindMask::CREATE),
384
385            // Remove events
386            EventKind::Remove(_) => self.intersects(EventKindMask::REMOVE),
387
388            // Modify events - check subkind
389            EventKind::Modify(modify_kind) => match modify_kind {
390                ModifyKind::Data(_) => self.intersects(EventKindMask::MODIFY_DATA),
391                ModifyKind::Metadata(_) => self.intersects(EventKindMask::MODIFY_META),
392                ModifyKind::Name(_) => self.intersects(EventKindMask::MODIFY_NAME),
393                // ModifyKind::Any and ModifyKind::Other pass if any modify flag is set
394                ModifyKind::Any | ModifyKind::Other => self.intersects(EventKindMask::ALL_MODIFY),
395            },
396
397            // Access events - check subkind
398            EventKind::Access(access_kind) => match access_kind {
399                AccessKind::Open(_) => self.intersects(EventKindMask::ACCESS_OPEN),
400                // Close after write
401                AccessKind::Close(AccessMode::Write) => {
402                    self.intersects(EventKindMask::ACCESS_CLOSE)
403                }
404                // Close after read-only (no write)
405                AccessKind::Close(AccessMode::Read) => {
406                    self.intersects(EventKindMask::ACCESS_CLOSE_NOWRITE)
407                }
408                // Close with unknown mode - match if either close flag is set
409                AccessKind::Close(_) => self
410                    .intersects(EventKindMask::ACCESS_CLOSE | EventKindMask::ACCESS_CLOSE_NOWRITE),
411                // AccessKind::Read, Any, and Other pass if any access flag is set
412                AccessKind::Read | AccessKind::Any | AccessKind::Other => {
413                    self.intersects(EventKindMask::ALL_ACCESS)
414                }
415            },
416        }
417    }
418}
419
420/// Notify event.
421///
422/// You might want to check [`Event::need_rescan`] to make sure no event was missed before you
423/// received this one.
424#[derive(Clone)]
425#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
426pub struct Event {
427    /// Kind or type of the event.
428    ///
429    /// This is a hierarchy of enums describing the event as precisely as possible. All enums in
430    /// the hierarchy have two variants always present, `Any` and `Other`, accompanied by one or
431    /// more specific variants.
432    ///
433    /// `Any` should be used when more detail about the event is not known beyond the variant
434    /// already selected. For example, `AccessMode::Any` means a file has been accessed, but that's
435    /// all we know.
436    ///
437    /// `Other` should be used when more detail _is_ available, but cannot be encoded as one of the
438    /// defined variants. When specifying `Other`, the [`info`](EventAttributes::info) attribute
439    /// should contain a short string identifying this detail.
440    ///
441    /// For example, `CreateKind::Other` with `info()` returning `Some("mount")` may indicate the
442    /// binding of a mount. The documentation of the particular backend should indicate if any
443    /// `Other` events are generated, and what their description means.
444    ///
445    /// The `EventKind::Any` variant should be used as the "else" case when mapping native kernel
446    /// bitmasks or bitmaps, such that if the mask is ever extended with new event types the
447    /// backend will not gain bugs due to not matching new unknown event types.
448    #[cfg_attr(
449        all(feature = "serde", not(feature = "serialization-compat-6")),
450        serde(flatten)
451    )]
452    #[cfg_attr(
453        all(feature = "serde", feature = "serialization-compat-6"),
454        serde(rename = "type")
455    )]
456    pub kind: EventKind,
457
458    /// Paths the event is about, if known.
459    ///
460    /// Notify backends report paths using the same root representation that was passed to
461    /// `Watcher::watch`. For example, watching `src` reports paths like `src/lib.rs`, while
462    /// watching an absolute path reports absolute event paths.
463    ///
464    /// If an event concerns two or more paths, and the paths are known at the time of event
465    /// creation, they should all go in this `Vec`. Otherwise, using the
466    /// [`tracker`](EventAttributes::tracker) attribute may be more appropriate.
467    ///
468    /// The order of the paths is likely to be significant! For example, renames where both ends of
469    /// the name change are known will have the "source" path first, and the "target" path last.
470    pub paths: Vec<PathBuf>,
471
472    // "What should be in the struct" and "what can go in the attrs" is an interesting question.
473    //
474    // Technically, the paths could go in the attrs. That would reduce the type size to 4 pointer
475    // widths, instead of 7 like it is now. Anything 8 and below is probably good — on x64 that's
476    // the size of an L1 cache line. The entire kind classification fits in 3 bytes, and an AnyMap
477    // is 3 pointers. A Vec<PathBuf> is another 3 pointers.
478    //
479    // Type size aside, what's behind these structures? A Vec and a PathBuf is stored on the heap.
480    // An AnyMap is stored on the heap. But a Vec is directly there, requiring about one access to
481    // get, while retrieving anything in the AnyMap requires some accesses as overhead.
482    //
483    // So things that are used often should be on the struct, and things that are used more rarely
484    // should go in the attrs. Additionally, arbitrary data can _only_ go in the attrs.
485    //
486    // The kind and the paths vie for first place on this scale, depending on how downstream wishes
487    // to use the information. Everything else is secondary. So far, that's why paths live here.
488    //
489    // In the future, it might be possible to have more data and to benchmark things properly, so
490    // the performance can be actually quantified. Also, it might turn out that I have no idea what
491    // I was talking about, so the above may be discarded or reviewed. We'll see!
492    //
493    /// Additional attributes of the event.
494    ///
495    /// Arbitrary data may be added to this field, without restriction beyond the `Sync` and
496    /// `Clone` properties. Some data added here is considered for comparing and hashing, but not
497    /// all: at this writing this is `Tracker`, `Flag`, `Info`, `Source`, and `UserData`.
498    #[cfg_attr(feature = "serde", serde(default))]
499    pub attrs: EventAttributes,
500}
501
502/// Additional attributes of the event.
503///
504/// Attributes are optional and their availability depends on the backend and event. See
505/// [`tracker`](Self::tracker), [`info`](Self::info), and [`source`](Self::source) for details.
506#[derive(Clone, Default, Debug)]
507#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
508pub struct EventAttributes {
509    #[cfg_attr(feature = "serde", serde(flatten))]
510    inner: Option<Box<EventAttributesInner>>,
511
512    // Keep this inline so attaching a watch's value does not require an allocation.
513    #[cfg_attr(
514        feature = "serde",
515        serde(default, skip_serializing_if = "Option::is_none")
516    )]
517    user_data: Option<u64>,
518}
519
520#[derive(Clone, Default, Debug)]
521#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
522struct EventAttributesInner {
523    /// See [`EventAttributes::tracker`].
524    #[cfg_attr(
525        feature = "serde",
526        serde(default, skip_serializing_if = "Option::is_none")
527    )]
528    tracker: Option<usize>,
529
530    /// Special Notify flag on the event.
531    #[cfg_attr(
532        feature = "serde",
533        serde(default, skip_serializing_if = "Option::is_none")
534    )]
535    flag: Option<Flag>,
536
537    /// See [`EventAttributes::info`].
538    #[cfg_attr(
539        feature = "serde",
540        serde(default, skip_serializing_if = "Option::is_none")
541    )]
542    info: Option<String>,
543
544    /// See [`EventAttributes::source`].
545    #[cfg_attr(
546        feature = "serde",
547        serde(default, skip_serializing_if = "Option::is_none")
548    )]
549    source: Option<String>,
550
551    /// The process ID of the originator of the event.
552    ///
553    /// This attribute is experimental and, while included in Notify itself, is not considered
554    /// stable or standard enough to be part of the serde, eq, hash, and debug representations.
555    #[cfg_attr(
556        feature = "serde",
557        serde(default, skip_serializing, skip_deserializing)
558    )]
559    process_id: Option<u32>,
560}
561
562impl EventAttributes {
563    /// Creates a new `EventAttributes`.
564    #[must_use]
565    pub fn new() -> Self {
566        Self {
567            inner: None,
568            user_data: None,
569        }
570    }
571
572    /// Returns the application-defined value of the watch that reported this event.
573    ///
574    /// The value is set when registering the watch. It participates in [`Event`]'s equality
575    /// and hashing, so otherwise identical events with different values remain distinct.
576    /// Values need not be unique. `None` means this attribute does not identify the reporting
577    /// watch, either because no value was configured or because the event has no specific watch.
578    ///
579    /// For renames, [`RenameMode::From`] carries the source watch's value, while
580    /// [`RenameMode::To`] and [`RenameMode::Both`] carry the destination watch's value.
581    ///
582    /// On a [`Flag::Rescan`] event, `Some(value)` identifies the reporting watch by its
583    /// configured value. It does not guarantee that other watches are unaffected; the recovery
584    /// requirements of [`Flag::Rescan`] still apply.
585    #[must_use]
586    pub fn user_data(&self) -> Option<u64> {
587        self.user_data
588    }
589
590    /// Sets [`EventAttributes::user_data`].
591    pub fn set_user_data(&mut self, user_data: u64) {
592        self.user_data = Some(user_data);
593    }
594
595    /// Returns a tracking ID (also called a cookie) for related events, if present.
596    ///
597    /// A backend may assign the same ID to multiple events that describe a single operation.
598    /// For example, the inotify backend uses a rename cookie to associate [`RenameMode::From`]
599    /// and [`RenameMode::To`] events for the same rename. If it also emits a [`RenameMode::Both`]
600    /// event, that event has the same ID.
601    /// When several watches reach the renamed file under different paths, for example through a
602    /// symbolic link, inotify reports the rename once for each of those watches, with the same ID.
603    /// Pair a [`RenameMode::From`] event with the [`RenameMode::To`] event below the same watch,
604    /// if there is one.
605    ///
606    /// Use this value to match related events from the same watcher. Treat it as an opaque value:
607    /// its meaning and lifetime depend on the backend, and it is not a persistent file identifier
608    /// or a globally unique event ID.
609    ///
610    /// Returns `None` when the backend or event does not provide a tracking ID.
611    #[must_use]
612    pub fn tracker(&self) -> Option<usize> {
613        self.inner.as_ref().and_then(|inner| inner.tracker)
614    }
615
616    /// Retrieves the Notify flag for an event directly, if present.
617    #[must_use]
618    pub fn flag(&self) -> Option<Flag> {
619        self.inner.as_ref().and_then(|inner| inner.flag)
620    }
621
622    /// Returns additional, backend-provided information about this event, if present.
623    ///
624    /// This attribute is primarily used to disambiguate the various `Other` variants in the
625    /// [`EventKind`] hierarchy (for example [`EventKind::Other`], [`CreateKind::Other`],
626    /// [`ModifyKind::Other`], [`RemoveKind::Other`], [`MetadataKind::Other`], ...).
627    ///
628    /// When a backend emits an `Other` kind, it should also set this attribute to a short,
629    /// human-readable string explaining what happened. If an event kind indicates `Other` but
630    /// this value is missing, treat it as a backend bug.
631    ///
632    /// Backends may also set this attribute for non-`Other` kinds, if doing so provides useful
633    /// precision. For example, the `Modify(Metadata(Extended))` kind suggests using this
634    /// attribute when information about _what_ extended metadata changed is available.
635    ///
636    /// # Stability
637    ///
638    /// The exact string values are backend-defined. They are intended as a hint for consumers and
639    /// may change between notify versions.
640    ///
641    /// # Examples
642    ///
643    /// `notify` backends and debouncers currently use values such as:
644    ///
645    /// - `unmount` (inotify unmount events)
646    /// - `rescan: user dropped` / `rescan: kernel dropped` (FSEvents queue overflow hints)
647    /// - `root changed` (FSEvents watched root renamed or removed)
648    /// - `mount` (FSEvents mount or unmount events)
649    /// - `is: symlink`, `is: hardlink`, `is: clone` (FSEvents item type hints)
650    /// - `meta: finder info` (FSEvents Finder metadata changes)
651    /// - `override` (a synthetic removal from `notify-debouncer-full` when a rename replaces an
652    ///   existing target)
653    #[must_use]
654    pub fn info(&self) -> Option<&str> {
655        self.inner.as_ref().and_then(|inner| inner.info.as_deref())
656    }
657
658    /// Returns a string identifying the backend that produced the event, if present.
659    ///
660    /// This is intended to be a short, unambiguous backend name. A dynamically generated value
661    /// should include a prefix that identifies the backend.
662    ///
663    /// The built-in `notify` backends currently leave this attribute unset, so their events return
664    /// `None`. A value can be present in events deserialized with the `serde` feature.
665    #[must_use]
666    pub fn source(&self) -> Option<&str> {
667        self.inner
668            .as_ref()
669            .and_then(|inner| inner.source.as_deref())
670    }
671
672    /// The process ID of the originator of the event.
673    ///
674    /// This attribute is experimental and, while included in Notify itself, is not considered
675    /// stable or standard enough to be part of the serde, eq, hash, and debug representations.
676    #[must_use]
677    pub fn process_id(&self) -> Option<u32> {
678        self.inner.as_ref().and_then(|inner| inner.process_id)
679    }
680
681    /// Sets [`EventAttributes::tracker`].
682    pub fn set_tracker(&mut self, tracker: usize) {
683        self.inner_mut().tracker = Some(tracker);
684    }
685
686    /// Sets the Notify flag onto the event.
687    pub fn set_flag(&mut self, flag: Flag) {
688        self.inner_mut().flag = Some(flag);
689    }
690
691    /// Sets [`EventAttributes::info`].
692    ///
693    /// This should be a short, human-readable string.
694    pub fn set_info(&mut self, info: &str) {
695        self.inner_mut().info = Some(info.to_string());
696    }
697
698    /// Sets the process id onto the event.
699    pub fn set_process_id(&mut self, process_id: u32) {
700        self.inner_mut().process_id = Some(process_id)
701    }
702
703    fn inner_mut(&mut self) -> &mut EventAttributesInner {
704        self.inner.get_or_insert_with(Box::default)
705    }
706}
707
708/// Special Notify flag on the event.
709///
710/// This attribute is used to flag certain kinds of events that Notify either marks or generates in
711/// particular ways.
712#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
713#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
714#[cfg_attr(
715    all(feature = "serde", not(feature = "serialization-compat-6")),
716    serde(rename_all = "camelCase")
717)]
718pub enum Flag {
719    /// Rescan notices are emitted by some platforms (and may also be emitted by Notify itself).
720    /// They indicate either a lapse in the events or a change in the filesystem such that events
721    /// received so far can no longer be relied on to represent the state of the filesystem now.
722    ///
723    /// An application that simply reacts to file changes may not care about this. An application
724    /// that keeps an in-memory representation of the filesystem will need to care, and will need
725    /// to refresh that representation directly from the filesystem.
726    Rescan,
727}
728
729impl Event {
730    /// Returns whether some events may have been missed. If true, you should assume any file or
731    /// folder might have been modified.
732    ///
733    /// See [`Flag::Rescan`] for more information.
734    #[must_use]
735    pub fn need_rescan(&self) -> bool {
736        matches!(self.flag(), Some(Flag::Rescan))
737    }
738    /// Returns [`EventAttributes::tracker`].
739    #[must_use]
740    pub fn tracker(&self) -> Option<usize> {
741        self.attrs.tracker()
742    }
743
744    /// Returns [`EventAttributes::user_data`].
745    #[must_use]
746    pub fn user_data(&self) -> Option<u64> {
747        self.attrs.user_data()
748    }
749
750    /// Retrieves the Notify flag for an event directly, if present.
751    #[must_use]
752    pub fn flag(&self) -> Option<Flag> {
753        self.attrs.flag()
754    }
755
756    /// Returns [`EventAttributes::info`].
757    #[must_use]
758    pub fn info(&self) -> Option<&str> {
759        self.attrs.info()
760    }
761
762    /// Returns [`EventAttributes::source`].
763    #[must_use]
764    pub fn source(&self) -> Option<&str> {
765        self.attrs.source()
766    }
767
768    /// Creates a new `Event` given a kind.
769    #[must_use]
770    pub fn new(kind: EventKind) -> Self {
771        Self {
772            kind,
773            paths: Vec::new(),
774            attrs: EventAttributes::new(),
775        }
776    }
777
778    /// Sets the kind.
779    #[must_use]
780    pub fn set_kind(mut self, kind: EventKind) -> Self {
781        self.kind = kind;
782        self
783    }
784
785    /// Adds a path to the event.
786    #[must_use]
787    pub fn add_path(mut self, path: PathBuf) -> Self {
788        self.paths.push(path);
789        self
790    }
791
792    /// Adds a path to the event if the argument is Some.
793    #[must_use]
794    pub fn add_some_path(self, path: Option<PathBuf>) -> Self {
795        if let Some(path) = path {
796            self.add_path(path)
797        } else {
798            self
799        }
800    }
801
802    /// Sets [`EventAttributes::tracker`].
803    #[must_use]
804    pub fn set_tracker(mut self, tracker: usize) -> Self {
805        self.attrs.set_tracker(tracker);
806        self
807    }
808
809    /// Sets [`EventAttributes::user_data`].
810    #[must_use]
811    pub fn set_user_data(mut self, user_data: u64) -> Self {
812        self.attrs.set_user_data(user_data);
813        self
814    }
815
816    /// Sets [`EventAttributes::info`].
817    #[must_use]
818    pub fn set_info(mut self, info: &str) -> Self {
819        self.attrs.set_info(info);
820        self
821    }
822
823    /// Sets the Notify flag onto the event.
824    #[must_use]
825    pub fn set_flag(mut self, flag: Flag) -> Self {
826        self.attrs.set_flag(flag);
827        self
828    }
829
830    /// Sets the process id onto the event.
831    #[must_use]
832    pub fn set_process_id(mut self, process_id: u32) -> Self {
833        self.attrs.set_process_id(process_id);
834        self
835    }
836}
837
838impl fmt::Debug for Event {
839    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
840        f.debug_struct("Event")
841            .field("kind", &self.kind)
842            .field("paths", &self.paths)
843            .field("attr:tracker", &self.tracker())
844            .field("attr:flag", &self.flag())
845            .field("attr:info", &self.info())
846            .field("attr:source", &self.source())
847            .field("attr:user_data", &self.user_data())
848            .finish()
849    }
850}
851impl Default for Event {
852    fn default() -> Self {
853        Self {
854            kind: EventKind::default(),
855            paths: Vec::new(),
856            attrs: EventAttributes::new(),
857        }
858    }
859}
860
861impl Eq for Event {}
862impl PartialEq for Event {
863    fn eq(&self, other: &Self) -> bool {
864        self.kind.eq(&other.kind)
865            && self.paths.eq(&other.paths)
866            && self.tracker().eq(&other.tracker())
867            && self.flag().eq(&other.flag())
868            && self.info().eq(&other.info())
869            && self.source().eq(&other.source())
870            && self.user_data().eq(&other.user_data())
871    }
872}
873
874impl Hash for Event {
875    fn hash<H: Hasher>(&self, state: &mut H) {
876        self.kind.hash(state);
877        self.paths.hash(state);
878        self.tracker().hash(state);
879        self.flag().hash(state);
880        self.info().hash(state);
881        self.source().hash(state);
882        self.user_data().hash(state);
883    }
884}
885
886#[cfg(test)]
887mod event_kind_mask_tests {
888    use super::*;
889
890    #[test]
891    fn default_is_all() {
892        assert_eq!(EventKindMask::default(), EventKindMask::ALL);
893    }
894
895    #[test]
896    fn matches_create_events() {
897        let mask = EventKindMask::CREATE;
898        assert!(mask.matches(&EventKind::Create(CreateKind::File)));
899        assert!(mask.matches(&EventKind::Create(CreateKind::Folder)));
900        assert!(mask.matches(&EventKind::Create(CreateKind::Any)));
901        assert!(mask.matches(&EventKind::Create(CreateKind::Other)));
902        assert!(!mask.matches(&EventKind::Remove(RemoveKind::File)));
903    }
904
905    #[test]
906    fn matches_remove_events() {
907        let mask = EventKindMask::REMOVE;
908        assert!(mask.matches(&EventKind::Remove(RemoveKind::File)));
909        assert!(mask.matches(&EventKind::Remove(RemoveKind::Folder)));
910        assert!(mask.matches(&EventKind::Remove(RemoveKind::Any)));
911        assert!(!mask.matches(&EventKind::Create(CreateKind::File)));
912    }
913
914    #[test]
915    fn matches_access_open_events() {
916        let mask = EventKindMask::ACCESS_OPEN;
917        assert!(mask.matches(&EventKind::Access(AccessKind::Open(AccessMode::Any))));
918        assert!(mask.matches(&EventKind::Access(AccessKind::Open(AccessMode::Read))));
919        assert!(mask.matches(&EventKind::Access(AccessKind::Open(AccessMode::Write))));
920        assert!(!mask.matches(&EventKind::Access(AccessKind::Close(AccessMode::Write))));
921        assert!(!mask.matches(&EventKind::Create(CreateKind::File)));
922    }
923
924    #[test]
925    fn matches_access_close_events() {
926        // ACCESS_CLOSE only matches Close(Write), not Close(Read)
927        let mask = EventKindMask::ACCESS_CLOSE;
928        assert!(mask.matches(&EventKind::Access(AccessKind::Close(AccessMode::Write))));
929        assert!(mask.matches(&EventKind::Access(AccessKind::Close(AccessMode::Any)))); // Any could be write
930        assert!(!mask.matches(&EventKind::Access(AccessKind::Close(AccessMode::Read)))); // Read goes to NOWRITE
931        assert!(!mask.matches(&EventKind::Access(AccessKind::Open(AccessMode::Any))));
932    }
933
934    #[test]
935    fn matches_access_close_nowrite_events() {
936        // ACCESS_CLOSE_NOWRITE matches Close(Read) - files opened read-only
937        let mask = EventKindMask::ACCESS_CLOSE_NOWRITE;
938        assert!(mask.matches(&EventKind::Access(AccessKind::Close(AccessMode::Read))));
939        assert!(mask.matches(&EventKind::Access(AccessKind::Close(AccessMode::Any)))); // Any could be read
940        assert!(!mask.matches(&EventKind::Access(AccessKind::Close(AccessMode::Write)))); // Write goes to ACCESS_CLOSE
941        assert!(!mask.matches(&EventKind::Access(AccessKind::Open(AccessMode::Any))));
942    }
943
944    #[test]
945    fn combined_close_masks_match_both() {
946        // When both ACCESS_CLOSE and ACCESS_CLOSE_NOWRITE are set, match both
947        let mask = EventKindMask::ACCESS_CLOSE | EventKindMask::ACCESS_CLOSE_NOWRITE;
948        assert!(mask.matches(&EventKind::Access(AccessKind::Close(AccessMode::Write))));
949        assert!(mask.matches(&EventKind::Access(AccessKind::Close(AccessMode::Read))));
950        assert!(mask.matches(&EventKind::Access(AccessKind::Close(AccessMode::Any))));
951    }
952
953    #[test]
954    fn all_access_matches_open_close_read_any_other() {
955        let mask = EventKindMask::ALL_ACCESS;
956        assert!(mask.matches(&EventKind::Access(AccessKind::Open(AccessMode::Read))));
957        assert!(mask.matches(&EventKind::Access(AccessKind::Close(AccessMode::Write))));
958        assert!(mask.matches(&EventKind::Access(AccessKind::Read)));
959        assert!(mask.matches(&EventKind::Access(AccessKind::Any)));
960        assert!(mask.matches(&EventKind::Access(AccessKind::Other)));
961    }
962
963    #[test]
964    fn matches_modify_data_events() {
965        let mask = EventKindMask::MODIFY_DATA;
966        assert!(mask.matches(&EventKind::Modify(ModifyKind::Data(DataChange::Any))));
967        assert!(mask.matches(&EventKind::Modify(ModifyKind::Data(DataChange::Size))));
968        assert!(mask.matches(&EventKind::Modify(ModifyKind::Data(DataChange::Content))));
969        assert!(!mask.matches(&EventKind::Modify(ModifyKind::Metadata(MetadataKind::Any))));
970        assert!(!mask.matches(&EventKind::Modify(ModifyKind::Name(RenameMode::From))));
971    }
972
973    #[test]
974    fn matches_modify_metadata_events() {
975        let mask = EventKindMask::MODIFY_META;
976        assert!(mask.matches(&EventKind::Modify(ModifyKind::Metadata(MetadataKind::Any))));
977        assert!(mask.matches(&EventKind::Modify(ModifyKind::Metadata(
978            MetadataKind::Permissions
979        ))));
980        assert!(!mask.matches(&EventKind::Modify(ModifyKind::Data(DataChange::Any))));
981    }
982
983    #[test]
984    fn matches_modify_name_events() {
985        let mask = EventKindMask::MODIFY_NAME;
986        assert!(mask.matches(&EventKind::Modify(ModifyKind::Name(RenameMode::From))));
987        assert!(mask.matches(&EventKind::Modify(ModifyKind::Name(RenameMode::To))));
988        assert!(mask.matches(&EventKind::Modify(ModifyKind::Name(RenameMode::Both))));
989        assert!(!mask.matches(&EventKind::Modify(ModifyKind::Data(DataChange::Any))));
990    }
991
992    #[test]
993    fn all_modify_matches_data_meta_name() {
994        let mask = EventKindMask::ALL_MODIFY;
995        assert!(mask.matches(&EventKind::Modify(ModifyKind::Data(DataChange::Any))));
996        assert!(mask.matches(&EventKind::Modify(ModifyKind::Metadata(MetadataKind::Any))));
997        assert!(mask.matches(&EventKind::Modify(ModifyKind::Name(RenameMode::From))));
998        assert!(mask.matches(&EventKind::Modify(ModifyKind::Any)));
999        assert!(mask.matches(&EventKind::Modify(ModifyKind::Other)));
1000    }
1001
1002    #[test]
1003    fn any_and_other_always_pass() {
1004        let empty = EventKindMask::empty();
1005        assert!(empty.matches(&EventKind::Any));
1006        assert!(empty.matches(&EventKind::Other));
1007    }
1008
1009    #[test]
1010    fn core_excludes_access() {
1011        let core = EventKindMask::CORE;
1012        assert!(core.matches(&EventKind::Create(CreateKind::File)));
1013        assert!(core.matches(&EventKind::Remove(RemoveKind::File)));
1014        assert!(core.matches(&EventKind::Modify(ModifyKind::Data(DataChange::Any))));
1015        assert!(core.matches(&EventKind::Modify(ModifyKind::Metadata(MetadataKind::Any))));
1016        assert!(core.matches(&EventKind::Modify(ModifyKind::Name(RenameMode::From))));
1017        assert!(!core.matches(&EventKind::Access(AccessKind::Open(AccessMode::Any))));
1018        assert!(!core.matches(&EventKind::Access(AccessKind::Close(AccessMode::Write))));
1019    }
1020
1021    #[test]
1022    fn empty_mask_only_passes_any_other() {
1023        let empty = EventKindMask::empty();
1024        assert!(empty.matches(&EventKind::Any));
1025        assert!(empty.matches(&EventKind::Other));
1026        assert!(!empty.matches(&EventKind::Create(CreateKind::File)));
1027        assert!(!empty.matches(&EventKind::Remove(RemoveKind::File)));
1028        assert!(!empty.matches(&EventKind::Modify(ModifyKind::Data(DataChange::Any))));
1029        assert!(!empty.matches(&EventKind::Access(AccessKind::Open(AccessMode::Any))));
1030    }
1031
1032    #[test]
1033    fn bitwise_or_combines_masks() {
1034        let mask = EventKindMask::CREATE | EventKindMask::REMOVE;
1035        assert!(mask.matches(&EventKind::Create(CreateKind::File)));
1036        assert!(mask.matches(&EventKind::Remove(RemoveKind::Folder)));
1037        assert!(!mask.matches(&EventKind::Modify(ModifyKind::Data(DataChange::Any))));
1038        assert!(!mask.matches(&EventKind::Access(AccessKind::Open(AccessMode::Any))));
1039    }
1040
1041    #[test]
1042    fn all_matches_everything() {
1043        let all = EventKindMask::ALL;
1044        assert!(all.matches(&EventKind::Any));
1045        assert!(all.matches(&EventKind::Other));
1046        assert!(all.matches(&EventKind::Create(CreateKind::File)));
1047        assert!(all.matches(&EventKind::Remove(RemoveKind::File)));
1048        assert!(all.matches(&EventKind::Modify(ModifyKind::Data(DataChange::Any))));
1049        assert!(all.matches(&EventKind::Access(AccessKind::Open(AccessMode::Any))));
1050        assert!(all.matches(&EventKind::Access(AccessKind::Close(AccessMode::Write))));
1051    }
1052
1053    #[test]
1054    fn access_read_and_any_match_with_all_access() {
1055        // Edge case: AccessKind::Read and AccessKind::Any should match if ALL_ACCESS is set
1056        let mask = EventKindMask::ALL_ACCESS;
1057        assert!(mask.matches(&EventKind::Access(AccessKind::Read)));
1058        assert!(mask.matches(&EventKind::Access(AccessKind::Any)));
1059        assert!(mask.matches(&EventKind::Access(AccessKind::Other)));
1060    }
1061}
1062
1063#[cfg(test)]
1064mod user_data_tests {
1065    use super::*;
1066    use std::collections::HashSet;
1067
1068    #[test]
1069    fn user_data_distinguishes_otherwise_identical_events() {
1070        let event = Event::new(EventKind::Create(CreateKind::File)).add_path("file".into());
1071        let zero = event.clone().set_user_data(0);
1072        let max = event.clone().set_user_data(u64::MAX);
1073        assert_eq!(zero.user_data(), Some(0));
1074        assert_eq!(zero.attrs.user_data(), Some(0));
1075        assert!(
1076            zero.attrs.inner.is_none(),
1077            "user data should not allocate attributes"
1078        );
1079        assert_eq!(zero, zero.clone());
1080        assert_ne!(zero, max);
1081        assert_ne!(zero, event);
1082        assert_eq!(HashSet::from([event, zero.clone(), zero, max]).len(), 3);
1083        assert!(
1084            format!("{:?}", Event::default().set_user_data(42))
1085                .contains("attr:user_data: Some(42)")
1086        );
1087    }
1088
1089    #[cfg(feature = "serde")]
1090    #[test]
1091    fn user_data_serialization_preserves_existing_attributes() {
1092        let base = Event::default().set_tracker(123).set_info("test");
1093        let old = serde_json::to_value(&base).unwrap();
1094        assert!(old["attrs"].get("user_data").is_none());
1095        assert_eq!(serde_json::from_value::<Event>(old.clone()).unwrap(), base);
1096        for value in [0, u64::MAX] {
1097            let event = base.clone().set_user_data(value);
1098            let mut expected = old.clone();
1099            expected["attrs"]["user_data"] = value.into();
1100            assert_eq!(serde_json::to_value(&event).unwrap(), expected);
1101            assert_eq!(serde_json::from_value::<Event>(expected).unwrap(), event);
1102            let event = Event::default().set_user_data(value);
1103            assert_eq!(
1104                serde_json::from_value::<Event>(serde_json::to_value(&event).unwrap()).unwrap(),
1105                event
1106            );
1107        }
1108    }
1109}
1110
1111#[cfg(all(test, feature = "serde", not(feature = "serialization-compat-6")))]
1112mod tests {
1113    use super::*;
1114
1115    use insta::assert_snapshot;
1116    use rstest::rstest;
1117
1118    #[rustfmt::skip]
1119    #[rstest]
1120    #[case("any", EventKind::Any)]
1121    #[case("access-any", EventKind::Access(AccessKind::Any))]
1122    #[case("access-read", EventKind::Access(AccessKind::Read))]
1123    #[case("access-open-any", EventKind::Access(AccessKind::Open(AccessMode::Any)))]
1124    #[case("access-open-execute", EventKind::Access(AccessKind::Open(AccessMode::Execute)))]
1125    #[case("access-open-read", EventKind::Access(AccessKind::Open(AccessMode::Read)))]
1126    #[case("access-open-write", EventKind::Access(AccessKind::Open(AccessMode::Write)))]
1127    #[case("access-open-other", EventKind::Access(AccessKind::Open(AccessMode::Other)))]
1128    #[case("access-close-any", EventKind::Access(AccessKind::Close(AccessMode::Any)))]
1129    #[case("access-close-execute", EventKind::Access(AccessKind::Close(AccessMode::Execute)))]
1130    #[case("access-close-read", EventKind::Access(AccessKind::Close(AccessMode::Read)))]
1131    #[case("access-close-write", EventKind::Access(AccessKind::Close(AccessMode::Write)))]
1132    #[case("access-close-other", EventKind::Access(AccessKind::Close(AccessMode::Other)))]
1133    #[case("access-other", EventKind::Access(AccessKind::Other))]
1134    #[case("create-any", EventKind::Create(CreateKind::Any))]
1135    #[case("create-file", EventKind::Create(CreateKind::File))]
1136    #[case("create-folder", EventKind::Create(CreateKind::Folder))]
1137    #[case("create-other", EventKind::Create(CreateKind::Other))]
1138    #[case("modify-any", EventKind::Modify(ModifyKind::Any))]
1139    #[case("modify-data-any", EventKind::Modify(ModifyKind::Data(DataChange::Any)))]
1140    #[case("modify-data-size", EventKind::Modify(ModifyKind::Data(DataChange::Size)))]
1141    #[case("modify-data-content", EventKind::Modify(ModifyKind::Data(DataChange::Content)))]
1142    #[case("modify-data-other", EventKind::Modify(ModifyKind::Data(DataChange::Other)))]
1143    #[case("modify-metadata-any", EventKind::Modify(ModifyKind::Metadata(MetadataKind::Any)))]
1144    #[case("modify-metadata-accesstime", EventKind::Modify(ModifyKind::Metadata(MetadataKind::AccessTime)))]
1145    #[case("modify-metadata-writetime", EventKind::Modify(ModifyKind::Metadata(MetadataKind::WriteTime)))]
1146    #[case("modify-metadata-permissions", EventKind::Modify(ModifyKind::Metadata(MetadataKind::Permissions)))]
1147    #[case("modify-metadata-ownership", EventKind::Modify(ModifyKind::Metadata(MetadataKind::Ownership)))]
1148    #[case("modify-metadata-extended", EventKind::Modify(ModifyKind::Metadata(MetadataKind::Extended)))]
1149    #[case("modify-metadata-other", EventKind::Modify(ModifyKind::Metadata(MetadataKind::Other)))]
1150    #[case("modify-name-any", EventKind::Modify(ModifyKind::Name(RenameMode::Any)))]
1151    #[case("modify-name-to", EventKind::Modify(ModifyKind::Name(RenameMode::To)))]
1152    #[case("modify-name-from", EventKind::Modify(ModifyKind::Name(RenameMode::From)))]
1153    #[case("modify-name-both", EventKind::Modify(ModifyKind::Name(RenameMode::Both)))]
1154    #[case("modify-name-other", EventKind::Modify(ModifyKind::Name(RenameMode::Other)))]
1155    #[case("modify-other", EventKind::Modify(ModifyKind::Other))]
1156    #[case("remove-any", EventKind::Remove(RemoveKind::Any))]
1157    #[case("remove-file", EventKind::Remove(RemoveKind::File))]
1158    #[case("remove-folder", EventKind::Remove(RemoveKind::Folder))]
1159    #[case("remove-other", EventKind::Remove(RemoveKind::Other))]
1160    #[case("other", EventKind::Other)]
1161    fn serialize_event_kind(
1162        #[case] name: &str,
1163        #[case] event_kind: EventKind,
1164    ) {
1165        let event = Event::new(event_kind);
1166        let json = serde_json::to_string(&event).unwrap();
1167        assert_snapshot!(name, json);
1168    }
1169
1170    #[test]
1171    fn serialize_event_with_attrs() {
1172        let event = Event::new(EventKind::Any)
1173            .set_tracker(123)
1174            .set_flag(Flag::Rescan)
1175            .set_info("test event")
1176            .set_process_id(0);
1177        let json = serde_json::to_string(&event).unwrap();
1178        assert_snapshot!(json);
1179    }
1180}