sos_core/events/
record.rs1use crate::{
2 commit::{CommitHash, CommitTree},
3 decode, encode, Result, UtcDateTime,
4};
5use binary_stream::futures::{Decodable, Encodable};
6
7#[derive(Default, Debug, Clone, Eq, PartialEq)]
9pub struct EventRecord(
10 pub(crate) UtcDateTime,
11 pub(crate) CommitHash,
12 pub(crate) CommitHash,
13 pub(crate) Vec<u8>,
14);
15
16impl EventRecord {
17 pub fn new(
19 time: UtcDateTime,
20 last_commit: CommitHash,
21 commit: CommitHash,
22 event: Vec<u8>,
23 ) -> Self {
24 Self(time, last_commit, commit, event)
25 }
26
27 pub fn time(&self) -> &UtcDateTime {
29 &self.0
30 }
31
32 pub fn set_time(&mut self, time: UtcDateTime) {
34 self.0 = time;
35 }
36
37 pub fn last_commit(&self) -> &CommitHash {
39 &self.1
40 }
41
42 pub fn set_last_commit(&mut self, commit: Option<CommitHash>) {
44 self.1 = commit.unwrap_or_default();
45 }
46
47 pub fn commit(&self) -> &CommitHash {
49 &self.2
50 }
51
52 pub fn event_bytes(&self) -> &[u8] {
54 self.3.as_slice()
55 }
56
57 pub fn size(&self) -> usize {
59 self.3.len()
60 }
61
62 pub async fn decode_event<T: Default + Decodable>(&self) -> Result<T> {
64 Ok(decode(&self.3).await?)
65 }
66
67 pub async fn encode_event<T: Default + Encodable>(
72 event: &T,
73 ) -> Result<Self> {
74 let bytes = encode(event).await?;
75 let commit = CommitHash(CommitTree::hash(&bytes));
76 Ok(EventRecord(
77 Default::default(),
78 Default::default(),
79 commit,
80 bytes,
81 ))
82 }
83}
84
85impl From<EventRecord> for (UtcDateTime, CommitHash, CommitHash, Vec<u8>) {
86 fn from(value: EventRecord) -> Self {
87 (value.0, value.1, value.2, value.3)
88 }
89}