Skip to content

Commit

Permalink
feat: make msg type generic (#3)
Browse files Browse the repository at this point in the history
* feat: make event msg generic

* feat: add test for minimal custom event msg
  • Loading branch information
mhatzl authored Jul 1, 2023
1 parent ab60b1f commit e6615be
Show file tree
Hide file tree
Showing 34 changed files with 412 additions and 193 deletions.
41 changes: 28 additions & 13 deletions src/creation_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
/// ```ignore
/// evident::create_static_publisher!(
/// <visibility specifier> <Name for the publisher>,
/// id_type = <Struct implementing `evident::publisher::Id`>,
/// id_type = <Struct implementing `evident::event::Id`>,
/// msg_type = <Struct implementing `evident::event::Msg`>,
/// entry_type = <Struct implementing `evident::event::entry::EventEntry`>,
/// interm_event_type = <Struct implementing `evident::event::intermediary::IntermediaryEvent`>,
/// filter_type = <Optional Struct implementing `evident::event::filter::Filter`>,
Expand All @@ -26,6 +27,7 @@
/// evident::create_static_publisher!(
/// pub MY_PUBLISHER,
/// id_type = MyId,
/// msg_type = String,
/// entry_type = MyEventEntry,
/// interm_event_type = MyIntermEvent,
/// capture_channel_bound = 100,
Expand All @@ -41,6 +43,7 @@
/// evident::create_static_publisher!(
/// pub MY_PUBLISHER,
/// id_type = MyId,
/// msg_type = String,
/// entry_type = MyEventEntry,
/// interm_event_type = MyIntermEvent,
/// filter_type = MyFilter,
Expand All @@ -56,6 +59,7 @@
macro_rules! create_static_publisher {
($publisher_name:ident,
id_type = $id_t:ty,
msg_type = $msg_t:ty,
entry_type = $entry_t:ty,
interm_event_type = $interm_event_t:ty,
$(filter_type=$filter_t:ty,)?
Expand All @@ -68,6 +72,7 @@ macro_rules! create_static_publisher {
$crate::z__setup_static_publisher!(
$publisher_name,
$id_t,
$msg_t,
$entry_t,
$interm_event_t,
$cap_channel_bound,
Expand All @@ -80,6 +85,7 @@ macro_rules! create_static_publisher {
};
($visibility:vis $publisher_name:ident,
id_type = $id_t:ty,
msg_type = $msg_t:ty,
entry_type = $entry_t:ty,
interm_event_type = $interm_event_t:ty,
$(filter_type=$filter_t:ty,)?
Expand All @@ -92,6 +98,7 @@ macro_rules! create_static_publisher {
$crate::z__setup_static_publisher!(
$publisher_name,
$id_t,
$msg_t,
$entry_t,
$interm_event_t,
$cap_channel_bound,
Expand All @@ -109,6 +116,7 @@ macro_rules! create_static_publisher {
macro_rules! z__setup_static_publisher {
($publisher_name:ident,
$id_t:ty,
$msg_t:ty,
$entry_t:ty,
$interm_event_t:ty,
$cap_channel_bound:expr,
Expand All @@ -123,6 +131,7 @@ macro_rules! z__setup_static_publisher {
$crate::z__create_static_publisher!(
$publisher_name,
$id_t,
$msg_t,
$entry_t,
$interm_event_t,
$(filter_type=$filter_t,)?
Expand All @@ -143,20 +152,20 @@ macro_rules! z__setup_static_publisher {
// Note: Re-impl `finalize()` for better IntelliSense.
impl $interm_event_t {
pub fn finalize(self) -> $crate::event::finalized::FinalizedEvent<$id_t> {
$crate::event::intermediary::IntermediaryEvent::<$id_t, $entry_t>::finalize(self)
$crate::event::intermediary::IntermediaryEvent::<$id_t, $msg_t, $entry_t>::finalize(self)
}
}

impl From<$interm_event_t> for $id_t {
fn from(intermed_event: $interm_event_t) -> Self {
$crate::event::intermediary::IntermediaryEvent::<$id_t, $entry_t>::finalize(intermed_event).into_event_id()
$crate::event::intermediary::IntermediaryEvent::<$id_t, $msg_t, $entry_t>::finalize(intermed_event).into_event_id()
}
}

impl PartialEq for $entry_t {
fn eq(&self, other: &Self) -> bool {
$crate::event::entry::EventEntry::<$id_t>::get_event_id(self) == $crate::event::entry::EventEntry::<$id_t>::get_event_id(other)
&& $crate::event::entry::EventEntry::<$id_t>::get_entry_id(self) == $crate::event::entry::EventEntry::<$id_t>::get_entry_id(other)
$crate::event::entry::EventEntry::<$id_t, $msg_t>::get_event_id(self) == $crate::event::entry::EventEntry::<$id_t, $msg_t>::get_event_id(other)
&& $crate::event::entry::EventEntry::<$id_t, $msg_t>::get_entry_id(self) == $crate::event::entry::EventEntry::<$id_t, $msg_t>::get_entry_id(other)
}
}

Expand All @@ -165,7 +174,7 @@ macro_rules! z__setup_static_publisher {
impl std::hash::Hash for $entry_t
{
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
$crate::event::entry::EventEntry::<$id_t>::get_entry_id(self).hash(state);
$crate::event::entry::EventEntry::<$id_t, $msg_t>::get_entry_id(self).hash(state);
}
}
};
Expand All @@ -175,6 +184,7 @@ macro_rules! z__setup_static_publisher {
macro_rules! z__create_static_publisher {
($publisher_name:ident,
$id_t:ty,
$msg_t:ty,
$entry_t:ty,
$interm_event_t:ty,
filter_type=$filter_t:ty,
Expand All @@ -186,10 +196,11 @@ macro_rules! z__create_static_publisher {
$(, scope=$visibility:vis)?
) => {
$($visibility)? static $publisher_name: $crate::once_cell::sync::Lazy<
$crate::publisher::EvidentPublisher<$id_t, $entry_t, $filter_t>,
$crate::publisher::EvidentPublisher<$id_t, $msg_t, $entry_t, $filter_t>,
> = $crate::once_cell::sync::Lazy::new(|| {
$crate::publisher::EvidentPublisher::<
$id_t,
$msg_t,
$entry_t,
$filter_t
>::with(|event| {
Expand All @@ -199,6 +210,7 @@ macro_rules! z__create_static_publisher {
};
($publisher_name:ident,
$id_t:ty,
$msg_t:ty,
$entry_t:ty,
$interm_event_t:ty,
$cap_channel_bound:expr,
Expand All @@ -207,13 +219,14 @@ macro_rules! z__create_static_publisher {
$timestamp_kind:expr
$(, scope=$visibility:vis)?
) => {
type DummyFilter = $crate::event::filter::DummyFilter<$id_t>;
type DummyFilter = $crate::event::filter::DummyFilter<$id_t, $msg_t>;

$($visibility)? static $publisher_name: $crate::once_cell::sync::Lazy<
$crate::publisher::EvidentPublisher<$id_t, $entry_t, DummyFilter>,
$crate::publisher::EvidentPublisher<$id_t, $msg_t, $entry_t, DummyFilter>,
> = $crate::once_cell::sync::Lazy::new(|| {
$crate::publisher::EvidentPublisher::<
$id_t,
$msg_t,
$entry_t,
DummyFilter
>::new(|event| {
Expand Down Expand Up @@ -263,19 +276,20 @@ macro_rules! z__create_static_publisher {
#[macro_export]
macro_rules! create_set_event_macro {
(id_type = $id_t:ty,
msg_type = $msg_t:ty,
entry_type = $entry_t:ty,
interm_event_type = $interm_event_t:ty
) => {
#[macro_export]
macro_rules! set_event {
($id:expr) => {
$crate::event::set_event::<$id_t, $entry_t, $interm_event_t>(
$crate::event::set_event::<$id_t, $msg_t, $entry_t, $interm_event_t>(
$id,
$crate::this_origin!(),
)
};
($id:expr, $msg:expr) => {
$crate::event::set_event_with_msg::<$id_t, $entry_t, $interm_event_t>(
$crate::event::set_event_with_msg::<$id_t, $msg_t, $entry_t, $interm_event_t>(
$id,
$msg,
$crate::this_origin!(),
Expand All @@ -285,18 +299,19 @@ macro_rules! create_set_event_macro {
};
(no_export,
id_type = $id_t:ty,
msg_type = $msg_t:ty,
entry_type = $entry_t:ty,
interm_event_type = $interm_event_t:ty
) => {
macro_rules! set_event {
($id:expr) => {
$crate::event::set_event::<$id_t, $entry_t, $interm_event_t>(
$crate::event::set_event::<$id_t, $msg_t, $entry_t, $interm_event_t>(
$id,
$crate::this_origin!(),
)
};
($id:expr, $msg:expr) => {
$crate::event::set_event_with_msg::<$id_t, $entry_t, $interm_event_t>(
$crate::event::set_event_with_msg::<$id_t, $msg_t, $entry_t, $interm_event_t>(
$id,
$msg,
$crate::this_origin!(),
Expand Down
10 changes: 4 additions & 6 deletions src/event/entry.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use std::hash::Hash;

use crate::publisher::Id;
use super::{origin::Origin, Id, Msg};

use super::origin::Origin;

pub trait EventEntry<K: Id>: Default + Clone + Hash + Send + Sync + 'static {
fn new(event_id: K, msg: &str, origin: Origin) -> Self;
pub trait EventEntry<K: Id, M: Msg>: Default + Clone + Hash + Send + Sync + 'static {
fn new(event_id: K, msg: Option<impl Into<M>>, origin: Origin) -> Self;

fn get_event_id(&self) -> &K;

Expand All @@ -14,7 +12,7 @@ pub trait EventEntry<K: Id>: Default + Clone + Hash + Send + Sync + 'static {
fn get_entry_id(&self) -> crate::uuid::Uuid;

/// Get the main message that was set when the event entry was created.
fn get_msg(&self) -> &str;
fn get_msg(&self) -> Option<&M>;

fn get_origin(&self) -> &Origin;
}
20 changes: 12 additions & 8 deletions src/event/filter.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
use std::marker::PhantomData;

use crate::publisher::{CaptureControl, Id};
use crate::publisher::CaptureControl;

use super::entry::EventEntry;
use super::{entry::EventEntry, Id, Msg};

pub trait Filter<K>
pub trait Filter<K, M>
where
K: Id + CaptureControl,
M: Msg,
{
/// Return `true` if the entry is allowed to be captured.
fn allow_entry(&self, entry: &impl EventEntry<K>) -> bool;
fn allow_entry(&self, entry: &impl EventEntry<K, M>) -> bool;
}

#[derive(Default, Debug)]
pub struct DummyFilter<K>
pub struct DummyFilter<K, M>
where
K: Id + CaptureControl,
M: Msg,
{
v: PhantomData<K>,
v1: PhantomData<K>,
v2: PhantomData<M>,
}

impl<K> Filter<K> for DummyFilter<K>
impl<K, M> Filter<K, M> for DummyFilter<K, M>
where
K: Id + CaptureControl,
M: Msg,
{
fn allow_entry(&self, _entry: &impl EventEntry<K>) -> bool {
fn allow_entry(&self, _entry: &impl EventEntry<K, M>) -> bool {
true
}
}
2 changes: 1 addition & 1 deletion src/event/finalized.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::publisher::Id;
use super::Id;

#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct FinalizedEvent<K: Id> {
Expand Down
11 changes: 5 additions & 6 deletions src/event/intermediary.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::publisher::Id;
use super::{entry::EventEntry, finalized::FinalizedEvent, origin::Origin, Id, Msg};

use super::{entry::EventEntry, finalized::FinalizedEvent, origin::Origin};

pub trait IntermediaryEvent<K, T>
pub trait IntermediaryEvent<K, M, T>
where
Self: std::marker::Sized,
K: Id,
T: EventEntry<K>,
M: Msg,
T: EventEntry<K, M>,
{
fn new(event_id: K, msg: &str, origin: Origin) -> Self;
fn new(event_id: K, msg: Option<impl Into<M>>, origin: Origin) -> Self;

fn get_entry(&self) -> &T;

Expand Down
Loading

0 comments on commit e6615be

Please sign in to comment.