pub mod decode;
pub mod hash;
pub mod header;
pub mod kind;
pub mod payload;
pub mod sourcing;
pub use decode::{DecodeSource, DecodeTyped, TypedDecodeError};
pub use hash::HashChain;
pub use header::EventHeader;
pub use kind::{EventKind, EventKindError};
pub use payload::{
revalidate_event_payload_registry, validate_event_payload_registry, EventPayload,
EventPayloadKindCollision, EventPayloadRegistryError, EventPayloadValidation,
};
pub use sourcing::{
EventSourced, JsonValueInput, MultiDispatchError, MultiReactive, ProjectionEvent,
ProjectionInput, ProjectionPayload, RawMsgpackInput, Reactive, ReplayLane, TypedReactive,
};
use crate::coordinate::Coordinate;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Event<P> {
pub header: EventHeader,
pub payload: P,
pub hash_chain: Option<HashChain>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct StoredEvent<P> {
pub coordinate: Coordinate,
pub event: Event<P>,
}
impl<P> Event<P> {
pub fn new(header: EventHeader, payload: P) -> Self {
Self {
header,
payload,
hash_chain: None,
}
}
pub fn with_hash_chain(mut self, chain: HashChain) -> Self {
self.hash_chain = Some(chain);
self
}
pub fn event_id(&self) -> u128 {
self.header.event_id
}
pub fn event_kind(&self) -> EventKind {
self.header.event_kind
}
pub fn position(&self) -> &crate::coordinate::DagPosition {
&self.header.position
}
pub fn is_genesis(&self) -> bool {
self.hash_chain
.as_ref()
.is_none_or(|c| c.prev_hash == [0u8; 32])
}
pub fn map_payload<U, F: FnOnce(P) -> U>(self, f: F) -> Event<U> {
Event {
header: self.header,
payload: f(self.payload),
hash_chain: self.hash_chain,
}
}
}