use crate::{
commit::{CommitHash, CommitTree},
decode, encode, Result, UtcDateTime,
};
use binary_stream::futures::{Decodable, Encodable};
#[derive(Default, Debug, Clone, Eq, PartialEq)]
pub struct EventRecord(
pub(crate) UtcDateTime,
pub(crate) CommitHash,
pub(crate) CommitHash,
pub(crate) Vec<u8>,
);
impl EventRecord {
pub fn new(
time: UtcDateTime,
last_commit: CommitHash,
commit: CommitHash,
event: Vec<u8>,
) -> Self {
Self(time, last_commit, commit, event)
}
pub fn time(&self) -> &UtcDateTime {
&self.0
}
pub fn set_time(&mut self, time: UtcDateTime) {
self.0 = time;
}
pub fn last_commit(&self) -> &CommitHash {
&self.1
}
pub fn set_last_commit(&mut self, commit: Option<CommitHash>) {
self.1 = commit.unwrap_or_default();
}
pub fn commit(&self) -> &CommitHash {
&self.2
}
pub fn event_bytes(&self) -> &[u8] {
self.3.as_slice()
}
pub fn size(&self) -> usize {
self.3.len()
}
pub async fn decode_event<T: Default + Decodable>(&self) -> Result<T> {
Ok(decode(&self.3).await?)
}
pub async fn encode_event<T: Default + Encodable>(
event: &T,
) -> Result<Self> {
let bytes = encode(event).await?;
let commit = CommitHash(CommitTree::hash(&bytes));
Ok(EventRecord(
Default::default(),
Default::default(),
commit,
bytes,
))
}
}
impl From<EventRecord> for (UtcDateTime, CommitHash, CommitHash, Vec<u8>) {
fn from(value: EventRecord) -> Self {
(value.0, value.1, value.2, value.3)
}
}