Skip to content

Commit

Permalink
Move DebouncedEvent types into notify-types crate
Browse files Browse the repository at this point in the history
  • Loading branch information
dfaust authored and 0xpr03 committed Mar 28, 2024
1 parent f195bd6 commit 416085e
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 44 deletions.
6 changes: 5 additions & 1 deletion notify-debouncer-full/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,25 @@ path = "src/lib.rs"

[features]
default = ["crossbeam","macos_fsevent"]
serde = ["notify-types/serde"]
mock_instant = ["dep:mock_instant","notify-types/mock_instant"]
# can't use dep:crossbeam-channel and feature name crossbeam-channel below rust 1.60
crossbeam = ["crossbeam-channel","notify/crossbeam-channel"]
macos_fsevent = ["notify/macos_fsevent"]
macos_kqueue = ["notify/macos_kqueue"]

[dependencies]
notify = { version = "6.1.1", path = "../notify", default-features = false }
notify-types = { version = "1.0.0", path = "../notify-types" }
crossbeam-channel = { version = "0.5", optional = true }
file-id = { version = "0.2.1", path = "../file-id" }
walkdir = "2.2.2"
log = "0.4.17"
mock_instant = { version = "0.3.0", optional = true }

[dev-dependencies]
notify-debouncer-full = { path = ".", features = ["mock_instant"] }
pretty_assertions = "1.3.0"
mock_instant = "0.3.0"
rstest = "0.18"
serde = { version = "1.0.89", features = ["derive"] }
deser-hjson = "1.1.1"
Expand Down
7 changes: 3 additions & 4 deletions notify-debouncer-full/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
//! As all file events are sourced from notify, the [known problems](https://docs.rs/notify/latest/notify/#known-problems) section applies here too.

mod cache;
mod debounced_event;

#[cfg(test)]
mod testing;
Expand All @@ -74,21 +73,21 @@ use std::{
};

pub use cache::{FileIdCache, FileIdMap, NoCache, RecommendedCache};
pub use debounced_event::DebouncedEvent;

pub use file_id;
pub use notify;
pub use notify_types::debouncer_full::DebouncedEvent;

use file_id::FileId;
use notify::{
event::{ModifyKind, RemoveKind, RenameMode},
Error, ErrorKind, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher, WatcherKind,
};

#[cfg(test)]
#[cfg(feature = "mock_instant")]
use mock_instant::Instant;

#[cfg(not(test))]
#[cfg(not(feature = "mock_instant"))]
use std::time::Instant;

/// The set of requirements for watcher debounce event handling functions.
Expand Down
2 changes: 1 addition & 1 deletion notify-debouncer-full/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ impl FileIdCache for TestCache {
if file_path == path
|| (file_path.starts_with(path) && recursive_mode == RecursiveMode::Recursive)
{
self.paths.insert(file_path.clone(), file_id.clone());
self.paths.insert(file_path.clone(), *file_id);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion notify-debouncer-mini/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ path = "src/lib.rs"

[features]
default = ["crossbeam","macos_fsevent"]
serde = ["notify-types/serde"]
# can't use dep:crossbeam-channel and feature name crossbeam-channel below rust 1.60
crossbeam = ["crossbeam-channel","notify/crossbeam-channel"]
macos_fsevent = ["notify/macos_fsevent"]
macos_kqueue = ["notify/macos_kqueue"]

[dependencies]
notify = { version = "6.1.1", path = "../notify", default-features = false }
notify-types = { version = "1.0.0", path = "../notify-types" }
crossbeam-channel = { version = "0.5", optional = true }
serde = { version = "1.0.89", features = ["derive"], optional = true }
log = "0.4.17"
38 changes: 4 additions & 34 deletions notify-debouncer-mini/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//! notify-debouncer-mini = "0.4.1"
//! notify = { version = "..", features = [".."] }
//! ```
//!
//!
//! # Examples
//! See also the full configuration example [here](https://github.com/notify-rs/notify/blob/main/examples/debouncer_mini_custom.rs).
//!
Expand Down Expand Up @@ -52,8 +52,6 @@
//! # Caveats
//!
//! As all file events are sourced from notify, the [known problems](https://docs.rs/notify/latest/notify/#known-problems) section applies here too.
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
path::PathBuf,
Expand All @@ -62,6 +60,8 @@ use std::{
};

pub use notify;
pub use notify_types::debouncer_mini::{DebouncedEvent, DebouncedEventKind};

use notify::{Error, Event, RecommendedWatcher, Watcher};

/// The set of requirements for watcher debounce event handling functions.
Expand Down Expand Up @@ -188,36 +188,6 @@ impl EventData {
/// Comes with either a vec of events or an immediate error.
pub type DebounceEventResult = Result<Vec<DebouncedEvent>, Error>;

/// A debounced event kind.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[non_exhaustive]
pub enum DebouncedEventKind {
/// No precise events
Any,
/// Event but debounce timed out (for example continuous writes)
AnyContinuous,
}

/// A debounced event.
///
/// Does not emit any specific event type on purpose, only distinguishes between an any event and a continuous any event.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct DebouncedEvent {
/// Event path
pub path: PathBuf,
/// Event kind
pub kind: DebouncedEventKind,
}

impl DebouncedEvent {
#[inline(always)]
fn new(path: PathBuf, kind: DebouncedEventKind) -> Self {
Self { path, kind }
}
}

enum InnerEvent {
NotifyEvent(Result<Event, Error>),
Shutdown,
Expand Down Expand Up @@ -293,7 +263,7 @@ impl DebounceDataInner {

/// Updates the deadline if none is set or when batch mode is disabled and the current deadline would miss the next event.
/// The new deadline is calculated based on the last event update time and the debounce timeout.
///
///
/// can't sub-function this due to event_map.drain() holding &mut self
fn check_deadline(
batch_mode: bool,
Expand Down
1 change: 1 addition & 0 deletions notify-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ edition = "2021"

[dependencies]
serde = { version = "1.0.89", features = ["derive"], optional = true }
mock_instant = { version = "0.3.0", optional = true }

[dev-dependencies]
serde_json = "1.0.39"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::ops::{Deref, DerefMut};

#[cfg(test)]
#[cfg(feature = "mock_instant")]
use mock_instant::Instant;

#[cfg(not(test))]
#[cfg(not(feature = "mock_instant"))]
use std::time::Instant;

use notify::Event;
use crate::event::Event;

/// A debounced event is emitted after a short delay.
#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down
34 changes: 34 additions & 0 deletions notify-types/src/debouncer_mini.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::path::PathBuf;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// A debounced event kind.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[non_exhaustive]
pub enum DebouncedEventKind {
/// No precise events
Any,
/// Event but debounce timed out (for example continuous writes)
AnyContinuous,
}

/// A debounced event.
///
/// Does not emit any specific event type on purpose, only distinguishes between an any event and a continuous any event.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct DebouncedEvent {
/// Event path
pub path: PathBuf,
/// Event kind
pub kind: DebouncedEventKind,
}

impl DebouncedEvent {
#[inline(always)]
pub fn new(path: PathBuf, kind: DebouncedEventKind) -> Self {
Self { path, kind }
}
}
5 changes: 5 additions & 0 deletions notify-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod debouncer_full;
pub mod debouncer_mini;
pub mod event;

#[cfg(test)]
Expand Down Expand Up @@ -25,5 +27,8 @@ mod tests {
assert_debug_impl!(event::RenameMode);
assert_debug_impl!(event::Event);
assert_debug_impl!(event::EventKind);
assert_debug_impl!(debouncer_mini::DebouncedEvent);
assert_debug_impl!(debouncer_mini::DebouncedEventKind);
assert_debug_impl!(debouncer_full::DebouncedEvent);
}
}

0 comments on commit 416085e

Please sign in to comment.