1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
/// An event and an optional tag. Typically not used direcly, but via [IntoTaggedEvt] or [EvtExt].
pub struct TaggedEvt<E> {
pub(crate) evt: E,
pub(crate) tag: Option<String>,
}
/// Used in an [EventSourced](super::EventSourced) command handler as impl trait in return position.
/// Together with its blanket implementation for any event allows for returning plain events without
/// boilerplate.
pub trait IntoTaggedEvt<E>: Send {
fn into_tagged_evt(self) -> TaggedEvt<E>;
}
impl<E> IntoTaggedEvt<E> for E
where
E: Send,
{
fn into_tagged_evt(self) -> TaggedEvt<E> {
TaggedEvt {
evt: self,
tag: None,
}
}
}
impl<E> IntoTaggedEvt<E> for TaggedEvt<E>
where
E: Send,
{
fn into_tagged_evt(self) -> TaggedEvt<E> {
self
}
}
/// Provide a `with_tag` extension method for events. Together with its blanket implementation for
/// any event allows for calling `with_tag` on any event type.
pub trait EvtExt: Sized {
/// Create a [TaggedEvt] with the given tag.
fn with_tag<T>(self, tag: T) -> TaggedEvt<Self>
where
T: Into<String>;
}
impl<E> EvtExt for E {
fn with_tag<T>(self, tag: T) -> TaggedEvt<E>
where
T: Into<String>,
{
TaggedEvt {
evt: self,
tag: Some(tag.into()),
}
}
}