two_devices_one_seat/
readback.rs1use 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#[derive(Debug, Clone, PartialEq, Eq)]
19pub struct Replica {
20 pub store: StoreId,
22 pub ids: BTreeSet<EventId>,
25 pub refunds: Vec<EventId>,
27 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#[derive(Debug, Clone, PartialEq, Eq)]
50pub struct Converged {
51 pub central: Replica,
54 pub tablet: Replica,
56 pub phone: Replica,
58}
59
60impl Converged {
61 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 #[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 #[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}