sim_lib_journal/
snapshot.rs1use std::collections::BTreeMap;
2
3use sim_kernel::{ContentId, Datum};
4
5use crate::{JournalEntry, JournalHead, StoredState, Verification, verify::verify_state};
6
7#[derive(Clone, Debug, PartialEq, Eq)]
13pub struct VerifiedSnapshot {
14 head: Option<JournalHead>,
15 entries: Vec<JournalEntry>,
16 datums: BTreeMap<ContentId, Datum>,
17}
18
19impl VerifiedSnapshot {
20 pub(crate) fn from_state(state: StoredState) -> Result<Self, crate::JournalError> {
21 let Verification {
22 head,
23 entries,
24 object_ids,
25 } = verify_state(&state)?;
26 let datums = object_ids
27 .into_iter()
28 .map(|id| {
29 let datum = state
30 .datums
31 .get(&id)
32 .cloned()
33 .ok_or_else(|| crate::JournalError::MissingSemanticObject(id.clone()))?;
34 Ok((id, datum))
35 })
36 .collect::<Result<_, crate::JournalError>>()?;
37 Ok(Self {
38 head,
39 entries,
40 datums,
41 })
42 }
43
44 pub const fn head(&self) -> Option<&JournalHead> {
46 self.head.as_ref()
47 }
48
49 pub fn entries(&self) -> &[JournalEntry] {
51 &self.entries
52 }
53
54 pub fn datum(&self, id: &ContentId) -> Option<&Datum> {
56 self.datums.get(id)
57 }
58
59 pub const fn datums(&self) -> &BTreeMap<ContentId, Datum> {
61 &self.datums
62 }
63}