hook events

master
Avril 4 years ago
parent d2bf263533
commit 370276fb56
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -126,3 +126,54 @@ where S: IntoIterator<Item=T>,
} }
} }
/// The ID type used for backing ID types;
pub type GenericID = uuid::Uuid;
/// Create a type that contains a (globally) unique ID.
#[macro_export] macro_rules! id_type {
($name:ident $(: $doc:literal)?) => ($crate::id_type!{pub(self) $name $(: $doc)?});
($vis:vis $name:ident $(: $doc:literal)?) => {
$(#[doc=$doc])?
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
$vis struct $name($crate::ext::GenericID);
impl $name
{
/// Create a new unique ID.
#[inline(always)] fn id_new() -> Self
{
Self($crate::ext::GenericID::new_v4())
}
/// The generic ID type backing this one
#[inline(always)] fn id_generic(&self) -> &$crate::ext::GenericID
{
&self.0
}
/// Consume into the generic ID
#[inline(always)] fn id_into_generic(self) -> $crate::ext::GenericID
{
self.0
}
/// Create from a generic ID
#[inline(always)] fn id_from_generic(gen: $crate::ext::GenericID) -> Self
{
Self(gen)
}
}
impl ::std::fmt::Display for $name
{
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result
{
use ::std::fmt::Write;
f.write_str(concat!(stringify!($name),"<"))?;
self.0.fmt(f)?;
f.write_str(">")
}
}
}
}

@ -112,6 +112,9 @@ pub struct Info
tags: Tags, tags: Tags,
/// Events to be emitted to `State` when this element does something.
///
/// Can be used for monitoring or logging and such.
hooks: event::Hooks, hooks: event::Hooks,
owner: Option<Vec<user::EntityID>>, //starts as the user that created (i.e. same as `created_by`), or `None` if ownership is disabled owner: Option<Vec<user::EntityID>>, //starts as the user that created (i.e. same as `created_by`), or `None` if ownership is disabled

@ -50,7 +50,7 @@ bitflags! {
/// Emmit delete events. /// Emmit delete events.
const DELETE = 1<<2; const DELETE = 1<<2;
/// Propagate events from children of `Map` datas /// Propagate events from children of `Map` or `List` datas
const CHILD = 1<<3; const CHILD = 1<<3;
/// Poke events are opaque events that are fired manually. /// Poke events are opaque events that are fired manually.
@ -68,28 +68,45 @@ impl Default for HookMask
} }
} }
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. /// Fire events when something happens to this value.
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Hooks pub struct Hooks
{ {
filter: HookMask, 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`. /// An event emitted from a matched `Hook`.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)] #[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub enum HookEvent pub enum HookEventKind
{ {
/// An opaque event type /// An opaque event type that can only be fired manually.
Poke, Poke,
Read, Read,
Write, Write,
Delete, Delete,
/// An event propagated from a child of this element.
///
/// This only can happen with `Map` or `List` data kinds.
Child(Box<HookEvent>), Child(Box<HookEvent>),
} }
impl Default for HookEvent impl Default for HookEventKind
{ {
#[inline] #[inline]
fn default() -> Self fn default() -> Self
@ -98,3 +115,11 @@ impl Default for HookEvent
} }
} }
/// 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,
}

@ -4,16 +4,9 @@ use std::collections::HashMap;
use std::borrow::Cow; use std::borrow::Cow;
use bitflags::bitflags; use bitflags::bitflags;
/// The ID type used for backing user/group IDs
type GenericID = uuid::Uuid;
/// A user group ID id_type!(pub UserID: "A unique user ID");
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] id_type!(pub GroupID: "A user group ID");
pub struct GroupID(GenericID);
/// A unique user ID
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct UserID(GenericID);
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct User pub struct User

Loading…
Cancel
Save