From 416085e17413c8bfd7537d9ae4ddb488aa86a05e Mon Sep 17 00:00:00 2001 From: Daniel Faust Date: Fri, 26 Jan 2024 12:23:29 +0100 Subject: [PATCH] Move DebouncedEvent types into notify-types crate --- notify-debouncer-full/Cargo.toml | 6 ++- notify-debouncer-full/src/lib.rs | 7 ++-- notify-debouncer-full/src/testing.rs | 2 +- notify-debouncer-mini/Cargo.toml | 3 +- notify-debouncer-mini/src/lib.rs | 38 ++----------------- notify-types/Cargo.toml | 1 + .../src/debouncer_full.rs | 6 +-- notify-types/src/debouncer_mini.rs | 34 +++++++++++++++++ notify-types/src/lib.rs | 5 +++ 9 files changed, 58 insertions(+), 44 deletions(-) rename notify-debouncer-full/src/debounced_event.rs => notify-types/src/debouncer_full.rs (91%) create mode 100644 notify-types/src/debouncer_mini.rs diff --git a/notify-debouncer-full/Cargo.toml b/notify-debouncer-full/Cargo.toml index 2449b846..0a5a76c8 100644 --- a/notify-debouncer-full/Cargo.toml +++ b/notify-debouncer-full/Cargo.toml @@ -20,6 +20,8 @@ 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"] @@ -27,14 +29,16 @@ 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" diff --git a/notify-debouncer-full/src/lib.rs b/notify-debouncer-full/src/lib.rs index 7312197b..fe2ccf9d 100644 --- a/notify-debouncer-full/src/lib.rs +++ b/notify-debouncer-full/src/lib.rs @@ -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; @@ -74,10 +73,10 @@ 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::{ @@ -85,10 +84,10 @@ use notify::{ 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. diff --git a/notify-debouncer-full/src/testing.rs b/notify-debouncer-full/src/testing.rs index 2b7d6c85..55ae706e 100644 --- a/notify-debouncer-full/src/testing.rs +++ b/notify-debouncer-full/src/testing.rs @@ -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); } } } diff --git a/notify-debouncer-mini/Cargo.toml b/notify-debouncer-mini/Cargo.toml index c2094b63..da0ab460 100644 --- a/notify-debouncer-mini/Cargo.toml +++ b/notify-debouncer-mini/Cargo.toml @@ -20,6 +20,7 @@ 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"] @@ -27,6 +28,6 @@ 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" diff --git a/notify-debouncer-mini/src/lib.rs b/notify-debouncer-mini/src/lib.rs index 8965b047..82fff9d9 100644 --- a/notify-debouncer-mini/src/lib.rs +++ b/notify-debouncer-mini/src/lib.rs @@ -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). //! @@ -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, @@ -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. @@ -188,36 +188,6 @@ impl EventData { /// Comes with either a vec of events or an immediate error. pub type DebounceEventResult = Result, 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), Shutdown, @@ -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, diff --git a/notify-types/Cargo.toml b/notify-types/Cargo.toml index 57dfe5a0..8fc315a3 100644 --- a/notify-types/Cargo.toml +++ b/notify-types/Cargo.toml @@ -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" diff --git a/notify-debouncer-full/src/debounced_event.rs b/notify-types/src/debouncer_full.rs similarity index 91% rename from notify-debouncer-full/src/debounced_event.rs rename to notify-types/src/debouncer_full.rs index ef33135e..5c3c9d85 100644 --- a/notify-debouncer-full/src/debounced_event.rs +++ b/notify-types/src/debouncer_full.rs @@ -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)] diff --git a/notify-types/src/debouncer_mini.rs b/notify-types/src/debouncer_mini.rs new file mode 100644 index 00000000..99397f51 --- /dev/null +++ b/notify-types/src/debouncer_mini.rs @@ -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 } + } +} diff --git a/notify-types/src/lib.rs b/notify-types/src/lib.rs index f2133292..a7f14e3d 100644 --- a/notify-types/src/lib.rs +++ b/notify-types/src/lib.rs @@ -1,3 +1,5 @@ +pub mod debouncer_full; +pub mod debouncer_mini; pub mod event; #[cfg(test)] @@ -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); } }