use bitflags::bitflags;
bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EventFlag: u32 {
const None = objc2_core_services::kFSEventStreamEventFlagNone;
const MustScanSubDirs = objc2_core_services::kFSEventStreamEventFlagMustScanSubDirs;
const UserDropped = objc2_core_services::kFSEventStreamEventFlagUserDropped;
const KernelDropped = objc2_core_services::kFSEventStreamEventFlagKernelDropped;
const EventIdsWrapped = objc2_core_services::kFSEventStreamEventFlagEventIdsWrapped;
const HistoryDone = objc2_core_services::kFSEventStreamEventFlagHistoryDone;
const RootChanged = objc2_core_services::kFSEventStreamEventFlagRootChanged;
const Mount = objc2_core_services::kFSEventStreamEventFlagMount;
const Unmount = objc2_core_services::kFSEventStreamEventFlagUnmount;
const ItemCreated = objc2_core_services::kFSEventStreamEventFlagItemCreated;
const ItemRemoved = objc2_core_services::kFSEventStreamEventFlagItemRemoved;
const ItemInodeMetaMod = objc2_core_services::kFSEventStreamEventFlagItemInodeMetaMod;
const ItemRenamed = objc2_core_services::kFSEventStreamEventFlagItemRenamed;
const ItemModified = objc2_core_services::kFSEventStreamEventFlagItemModified;
const ItemFinderInfoMod = objc2_core_services::kFSEventStreamEventFlagItemFinderInfoMod;
const ItemChangeOwner = objc2_core_services::kFSEventStreamEventFlagItemChangeOwner;
const ItemXattrMod = objc2_core_services::kFSEventStreamEventFlagItemXattrMod;
const ItemIsFile = objc2_core_services::kFSEventStreamEventFlagItemIsFile;
const ItemIsDir = objc2_core_services::kFSEventStreamEventFlagItemIsDir;
const ItemIsSymlink = objc2_core_services::kFSEventStreamEventFlagItemIsSymlink;
const OwnEvent = objc2_core_services::kFSEventStreamEventFlagOwnEvent;
const IsHardlink = objc2_core_services::kFSEventStreamEventFlagItemIsHardlink;
const IsLastHardlink = objc2_core_services::kFSEventStreamEventFlagItemIsLastHardlink;
const Cloned = objc2_core_services::kFSEventStreamEventFlagItemCloned;
}
}
pub enum EventType {
Unknown,
File,
Dir,
Symlink,
Hardlink,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScanType {
SingleNode,
Folder,
ReScan,
Nop,
}
impl EventFlag {
pub fn event_type(&self) -> EventType {
if self.contains(EventFlag::IsHardlink) | self.contains(EventFlag::IsLastHardlink) {
EventType::Hardlink
} else if self.contains(EventFlag::ItemIsSymlink) {
EventType::Symlink
} else if self.contains(EventFlag::ItemIsDir) {
EventType::Dir
} else if self.contains(EventFlag::ItemIsFile) {
EventType::File
} else {
EventType::Unknown
}
}
pub fn scan_type(&self) -> ScanType {
let event_type = self.event_type();
let is_dir = matches!(event_type, EventType::Dir);
if self.contains(EventFlag::HistoryDone) | self.contains(EventFlag::EventIdsWrapped) {
ScanType::Nop
} else if self.contains(EventFlag::RootChanged) {
ScanType::ReScan
} else {
if is_dir {
ScanType::Folder
} else {
ScanType::SingleNode
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_event_type_deduction() {
assert!(matches!(
(EventFlag::ItemIsFile).event_type(),
EventType::File
));
assert!(matches!(
(EventFlag::ItemIsDir).event_type(),
EventType::Dir
));
assert!(matches!(
(EventFlag::ItemIsSymlink).event_type(),
EventType::Symlink
));
assert!(matches!(
(EventFlag::IsHardlink).event_type(),
EventType::Hardlink
));
assert!(matches!((EventFlag::None).event_type(), EventType::Unknown));
}
#[test]
fn test_scan_type_root_changed_and_history_done() {
assert_eq!(EventFlag::RootChanged.scan_type(), ScanType::ReScan);
assert_eq!(EventFlag::HistoryDone.scan_type(), ScanType::Nop);
}
#[test]
fn test_scan_type_created_removed_modified() {
assert!(matches!(
(EventFlag::ItemCreated | EventFlag::ItemIsFile).scan_type(),
ScanType::SingleNode
));
assert!(matches!(
(EventFlag::ItemRemoved | EventFlag::ItemIsDir).scan_type(),
ScanType::Folder
));
assert!(matches!(
(EventFlag::ItemRemoved | EventFlag::ItemIsFile).scan_type(),
ScanType::SingleNode
));
assert!(matches!(
(EventFlag::ItemModified | EventFlag::ItemIsFile).scan_type(),
ScanType::SingleNode
));
}
#[test]
fn test_scan_type_must_scan_subdirs() {
assert!(matches!(
(EventFlag::MustScanSubDirs | EventFlag::ItemIsDir).scan_type(),
ScanType::Folder
));
}
}