use core::{num::NonZeroUsize, time::Duration};
use crate::{filter::Filter, interest::Interest};
#[cfg(test)]
mod tests;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DebounceConfig {
quiet_window: Duration,
max_hold: Duration,
max_buffered: usize,
}
impl DebounceConfig {
pub const DEFAULT_QUIET_WINDOW: Duration = Duration::from_millis(50);
pub const DEFAULT_MAX_HOLD: Duration = Duration::from_millis(500);
pub const DEFAULT_MAX_BUFFERED: usize = 1024;
#[inline]
pub const fn new() -> Self {
Self {
quiet_window: Self::DEFAULT_QUIET_WINDOW,
max_hold: Self::DEFAULT_MAX_HOLD,
max_buffered: Self::DEFAULT_MAX_BUFFERED,
}
}
#[inline]
pub const fn max_buffered(&self) -> usize {
self.max_buffered
}
#[inline]
#[must_use]
pub const fn with_max_buffered(mut self, max_buffered: usize) -> Self {
self.max_buffered = if max_buffered == 0 { 1 } else { max_buffered };
self
}
#[inline]
pub const fn set_max_buffered(&mut self, max_buffered: usize) -> &mut Self {
self.max_buffered = if max_buffered == 0 { 1 } else { max_buffered };
self
}
#[inline]
pub const fn quiet_window(&self) -> Duration {
self.quiet_window
}
#[inline]
#[must_use]
pub const fn with_quiet_window(mut self, quiet_window: Duration) -> Self {
self.quiet_window = quiet_window;
self
}
#[inline]
pub const fn set_quiet_window(&mut self, quiet_window: Duration) -> &mut Self {
self.quiet_window = quiet_window;
self
}
#[inline]
pub const fn max_hold(&self) -> Duration {
self.max_hold
}
#[inline]
#[must_use]
pub const fn with_max_hold(mut self, max_hold: Duration) -> Self {
self.max_hold = max_hold;
self
}
#[inline]
pub const fn set_max_hold(&mut self, max_hold: Duration) -> &mut Self {
self.max_hold = max_hold;
self
}
}
impl Default for DebounceConfig {
#[inline]
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum Debounce {
#[default]
Inherit,
Off,
Custom(DebounceConfig),
}
impl Debounce {
#[inline]
pub const fn is_inherit(&self) -> bool {
matches!(self, Self::Inherit)
}
#[inline]
pub const fn is_off(&self) -> bool {
matches!(self, Self::Off)
}
#[inline]
pub const fn is_custom(&self) -> bool {
matches!(self, Self::Custom(_))
}
#[inline]
pub const fn as_custom(&self) -> Option<&DebounceConfig> {
match self {
Self::Custom(config) => Some(config),
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TributariesOptions {
event_capacity: NonZeroUsize,
command_capacity: NonZeroUsize,
debounce: Option<DebounceConfig>,
}
impl TributariesOptions {
pub const DEFAULT_DEBOUNCE: Option<DebounceConfig> = None;
pub const DEFAULT_EVENT_CAPACITY: NonZeroUsize = NonZeroUsize::new(1024).unwrap();
pub const DEFAULT_COMMAND_CAPACITY: NonZeroUsize = NonZeroUsize::new(64).unwrap();
#[inline]
pub const fn new() -> Self {
Self {
event_capacity: Self::DEFAULT_EVENT_CAPACITY,
command_capacity: Self::DEFAULT_COMMAND_CAPACITY,
debounce: Self::DEFAULT_DEBOUNCE,
}
}
#[inline]
pub const fn event_capacity(&self) -> NonZeroUsize {
self.event_capacity
}
#[inline]
#[must_use]
pub const fn with_event_capacity(mut self, event_capacity: NonZeroUsize) -> Self {
self.event_capacity = event_capacity;
self
}
#[inline]
pub const fn set_event_capacity(&mut self, event_capacity: NonZeroUsize) -> &mut Self {
self.event_capacity = event_capacity;
self
}
#[inline]
pub const fn command_capacity(&self) -> NonZeroUsize {
self.command_capacity
}
#[inline]
#[must_use]
pub const fn with_command_capacity(mut self, command_capacity: NonZeroUsize) -> Self {
self.command_capacity = command_capacity;
self
}
#[inline]
pub const fn set_command_capacity(&mut self, command_capacity: NonZeroUsize) -> &mut Self {
self.command_capacity = command_capacity;
self
}
#[inline]
pub const fn debounce_config(&self) -> Option<DebounceConfig> {
self.debounce
}
#[inline]
#[must_use]
pub const fn debounce(mut self, config: DebounceConfig) -> Self {
self.debounce = Some(config);
self
}
#[inline]
pub const fn set_debounce(&mut self, config: Option<DebounceConfig>) -> &mut Self {
self.debounce = config;
self
}
#[inline]
pub(crate) fn into_parts(self) -> (NonZeroUsize, NonZeroUsize, Option<DebounceConfig>) {
(self.event_capacity, self.command_capacity, self.debounce)
}
}
impl Default for TributariesOptions {
#[inline]
fn default() -> Self {
Self::new()
}
}
pub struct WatchOptions<C> {
interest: Interest,
filter: Filter<C>,
debounce: Debounce,
}
impl<C> WatchOptions<C> {
pub const DEFAULT_INTEREST: Interest = Interest::all();
pub const DEFAULT_DEBOUNCE: Debounce = Debounce::Inherit;
#[inline]
pub fn new() -> Self {
Self {
interest: Self::DEFAULT_INTEREST,
filter: Filter::all(),
debounce: Self::DEFAULT_DEBOUNCE,
}
}
#[inline]
pub const fn interest(&self) -> Interest {
self.interest
}
#[inline]
#[must_use]
pub const fn with_interest(mut self, interest: Interest) -> Self {
self.interest = interest;
self
}
#[inline]
pub const fn set_interest(&mut self, interest: Interest) -> &mut Self {
self.interest = interest;
self
}
#[inline]
pub const fn filter(&self) -> &Filter<C> {
&self.filter
}
#[inline]
#[must_use]
pub fn with_filter(mut self, filter: Filter<C>) -> Self {
self.filter = filter;
self
}
#[inline]
pub fn set_filter(&mut self, filter: Filter<C>) -> &mut Self {
self.filter = filter;
self
}
#[inline]
pub const fn debounce(&self) -> Debounce {
self.debounce
}
#[inline]
#[must_use]
pub const fn with_debounce(mut self, debounce: Debounce) -> Self {
self.debounce = debounce;
self
}
#[inline]
pub const fn set_debounce(&mut self, debounce: Debounce) -> &mut Self {
self.debounce = debounce;
self
}
#[inline]
pub(crate) fn into_parts(self) -> (Interest, Filter<C>, Debounce) {
(self.interest, self.filter, self.debounce)
}
}
impl<C> Default for WatchOptions<C> {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl<C> Clone for WatchOptions<C> {
#[inline]
fn clone(&self) -> Self {
Self {
interest: self.interest,
filter: self.filter.clone(),
debounce: self.debounce,
}
}
}
impl<C> core::fmt::Debug for WatchOptions<C> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("WatchOptions")
.field("interest", &self.interest)
.field("filter", &self.filter)
.field("debounce", &self.debounce)
.finish()
}
}