//! When events happen to datamaps use super::*; use bitflags::bitflags; /// What happened in the in-band event #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] pub enum InBandEventKind { Created, Modified, Deleted, Renamed(String), // Previous name Cloned(data::AbsoluteIndex), // Index of the new item Moved, Comment(String), } /// An event that happened to a data entry that is then stored in its metadata. /// /// # Note /// Not to be confused with `HookEvent`s #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct InBandEvent { who: Option, what: InBandEventKind, when: u64, signed: Option, } bitflags! { /// Kind of events that should emmit for a hook #[derive(Serialize, Deserialize)] struct HookMask: u16 { /// Filter all events. None are emiited. const NONE = 0; /// Mask any event, any kind is emitted. const ANY = !0; /// Emmit read events. const READ = 1<<0; /// Emmit write events. const WRITE = 1<<1; /// Emmit delete events. const DELETE = 1<<2; /// Propagate events from children of `Map` or `List` datas const CHILD = 1<<3; /// Poke events are opaque events that are fired manually. const POKE = 1<<4; } } impl Default for HookMask { #[inline(always)] fn default() -> Self { Self::NONE } } id_type!(pub HookID: "The ID of a hook, passed with the event when the hook is fired"); /// Fire events when something happens to this value. #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] pub struct Hooks { filter: HookMask, id: HookID, } impl Default for Hooks { #[inline] fn default() -> Self { Self { id: HookID::id_new(), // generate an id now, in case hooks are added later filter: Default::default(), //default is to emit nothing ever } } } /// An event emitted from a matched `Hook`. #[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)] pub enum HookEventKind { /// An opaque event type that can only be fired manually. Poke, Read, Write, Delete, /// An event propagated from a child of this element. /// /// This only can happen with `Map` or `List` data kinds. Child(Box), } impl Default for HookEventKind { #[inline] fn default() -> Self { Self::Poke } } /// An event emitted from a element value's `Hooks` object. #[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)] pub struct HookEvent { kind: HookEventKind, /// Corresponds to the `id` field of the `Hooks` object this event was emitted from. id: HookID, }