use itertools::Itertools as _; use super::{ event::{Created, Deleted, Event}, Channel, Id, }; use crate::event::{Instant, ResumePoint, Sequence}; #[derive(Clone, Debug, Eq, PartialEq)] pub struct History { pub channel: Channel, pub created: Instant, pub deleted: Option, } // State interface impl History { pub fn id(&self) -> &Id { &self.channel.id } // Snapshot of this channel as it was when created. (Note to the future: it's // okay if this returns a redacted or modified version of the channel. If we // implement renames by redacting the original name, then this should return the // renamed channel, not the original, even if that's not how it was "as // created.") pub fn as_created(&self) -> Channel { self.channel.clone() } pub fn as_of(&self, resume_point: impl Into) -> Option { self.events() .filter(Sequence::up_to(resume_point.into())) .collect() } // Snapshot of this channel as of all events recorded in this history. pub fn as_snapshot(&self) -> Option { self.events().collect() } } // Event factories impl History { pub fn events(&self) -> impl Iterator { [self.created()] .into_iter() .merge_by(self.deleted(), Sequence::merge) } fn created(&self) -> Event { Created { instant: self.created, channel: self.channel.clone(), } .into() } fn deleted(&self) -> Option { self.deleted.map(|instant| { Deleted { instant, id: self.channel.id.clone(), } .into() }) } }