Skip to main content

two_devices_one_seat/
readback.rs

1//! The three files read back after every handle is dropped.
2//!
3//! Nothing is carried in but the directory. Each file is opened fresh, its log
4//! read whole and its read model read through a new connection, so what this
5//! reports came off disk and not out of a value the story was still holding.
6
7use std::collections::BTreeSet;
8use std::path::Path;
9
10use happenstance::{EventId, StoreId};
11
12use crate::domain::{Place, SALE_REFUNDED, ShowId};
13use crate::error::StoryError;
14use crate::scenario::{SHOW, Site};
15use crate::timeline::TimelineView;
16
17/// One place's file, read back.
18#[derive(Debug, Clone, PartialEq, Eq)]
19pub struct Replica {
20    /// The store's own identity, minted when its file was created.
21    pub store: StoreId,
22    /// Every event's origin identity. Positions are local to each store, so
23    /// the identity is what two logs can be compared by.
24    pub ids: BTreeSet<EventId>,
25    /// The refunds, by identity, in this log's order.
26    pub refunds: Vec<EventId>,
27    /// The read model.
28    pub view: TimelineView,
29}
30
31impl Replica {
32    async fn read(dir: &Path, at: Place, show: &ShowId) -> Result<Self, StoryError> {
33        let site = Site::open(dir, at)?;
34        let log = site.log(at).await?;
35        Ok(Self {
36            store: site.events.store_id(),
37            ids: log.iter().map(|event| event.id).collect(),
38            refunds: log
39                .iter()
40                .filter(|event| event.event_type() == &SALE_REFUNDED)
41                .map(|event| event.id)
42                .collect(),
43            view: site.view(show)?,
44        })
45    }
46}
47
48/// All three files, read back.
49#[derive(Debug, Clone, PartialEq, Eq)]
50pub struct Converged {
51    /// Central's file. Its [`store`](Replica::store) is the identity every
52    /// refund's id must name (SY-7).
53    pub central: Replica,
54    /// The tablet's file.
55    pub tablet: Replica,
56    /// The phone's file.
57    pub phone: Replica,
58}
59
60impl Converged {
61    /// Opens the three files under `dir` afresh and reads them, the read
62    /// model as it stands for the story's show.
63    ///
64    /// # Errors
65    ///
66    /// Any [`StoryError`] opening or reading a file, naming the file or place.
67    pub async fn read(dir: &Path) -> Result<Self, StoryError> {
68        let show = ShowId::new(SHOW).map_err(StoryError::Show)?;
69        Ok(Self {
70            central: Replica::read(dir, Place::Central, &show).await?,
71            tablet: Replica::read(dir, Place::Tablet, &show).await?,
72            phone: Replica::read(dir, Place::Phone, &show).await?,
73        })
74    }
75
76    /// Each place and its replica, central first.
77    #[must_use]
78    pub const fn replicas(&self) -> [(Place, &Replica); 3] {
79        [
80            (Place::Central, &self.central),
81            (Place::Tablet, &self.tablet),
82            (Place::Phone, &self.phone),
83        ]
84    }
85
86    /// Whether the three logs hold the same events and the three read models
87    /// are identical.
88    #[must_use]
89    pub fn agrees(&self) -> bool {
90        self.replicas().iter().all(|(_, replica)| {
91            replica.ids == self.central.ids && replica.view == self.central.view
92        })
93    }
94}