use crate::event::EventKind;
#[cfg(test)]
mod tests;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Interest {
created: bool,
modified: bool,
removed: bool,
moved: bool,
}
impl Interest {
#[inline]
pub const fn new() -> Self {
Self::all()
}
#[inline]
pub const fn all() -> Self {
Self {
created: true,
modified: true,
removed: true,
moved: true,
}
}
#[inline]
pub const fn none() -> Self {
Self {
created: false,
modified: false,
removed: false,
moved: false,
}
}
#[inline]
pub const fn created(&self) -> bool {
self.created
}
#[inline]
pub const fn modified(&self) -> bool {
self.modified
}
#[inline]
pub const fn removed(&self) -> bool {
self.removed
}
#[inline]
pub const fn moved(&self) -> bool {
self.moved
}
#[inline]
pub const fn is_all(&self) -> bool {
self.created && self.modified && self.removed && self.moved
}
#[inline]
pub const fn is_none(&self) -> bool {
!(self.created || self.modified || self.removed || self.moved)
}
pub fn admits<C>(&self, kind: &EventKind<C>) -> bool {
match kind {
EventKind::Created => self.created,
EventKind::Modified => self.modified,
EventKind::Removed => self.removed,
EventKind::Moved { .. } => self.moved,
EventKind::Rescan => true,
#[allow(unreachable_patterns)]
_ => true,
}
}
}
macro_rules! interest_flag {
($field:ident, $set:ident, $with:ident, $update:ident, $maybe:ident, $clear:ident) => {
#[doc = concat!("Subscribes to `", stringify!($field), "` deliveries (sets it true).")]
#[inline]
pub const fn $set(&mut self) -> &mut Self {
self.$field = true;
self
}
#[doc = concat!("Returns this interest subscribed to `", stringify!($field), "` deliveries.")]
#[inline]
#[must_use]
pub const fn $with(mut self) -> Self {
self.$field = true;
self
}
#[doc = concat!("Sets the `", stringify!($field), "` subscription to `value`.")]
#[inline]
pub const fn $update(&mut self, value: bool) -> &mut Self {
self.$field = value;
self
}
#[doc = concat!("Returns this interest with the `", stringify!($field), "` subscription set to `value`.")]
#[inline]
#[must_use]
pub const fn $maybe(mut self, value: bool) -> Self {
self.$field = value;
self
}
#[doc = concat!("Clears the `", stringify!($field), "` subscription (sets it false).")]
#[inline]
pub const fn $clear(&mut self) -> &mut Self {
self.$field = false;
self
}
};
}
impl Interest {
interest_flag!(
created,
set_created,
with_created,
update_created,
maybe_created,
clear_created
);
interest_flag!(
modified,
set_modified,
with_modified,
update_modified,
maybe_modified,
clear_modified
);
interest_flag!(
removed,
set_removed,
with_removed,
update_removed,
maybe_removed,
clear_removed
);
interest_flag!(
moved,
set_moved,
with_moved,
update_moved,
maybe_moved,
clear_moved
);
}
impl Default for Interest {
#[inline]
fn default() -> Self {
Self::new()
}
}