Make export own its playback and centralize session exits (#89)
Co-Authored-By: Codex <noreply@openai.com>
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
//! Presentation playback ownership and lifecycle transitions.
|
||||
|
||||
/// The one active presentation mode. Export owns its playback so it cannot outlive its film.
|
||||
pub enum State<P, E> {
|
||||
Live,
|
||||
Replay(P),
|
||||
Export { playback: P, export: E },
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum ExitCause {
|
||||
UserStop,
|
||||
WorldChange,
|
||||
Replaced,
|
||||
Finished,
|
||||
Failed,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct Cleanup {
|
||||
pub was_export: bool,
|
||||
pub cancel_capture: bool,
|
||||
}
|
||||
|
||||
pub struct Session<P, E> {
|
||||
state: State<P, E>,
|
||||
}
|
||||
|
||||
impl<P, E> Default for Session<P, E> {
|
||||
fn default() -> Self {
|
||||
Self { state: State::Live }
|
||||
}
|
||||
}
|
||||
|
||||
impl<P, E> Session<P, E> {
|
||||
pub fn is_live(&self) -> bool {
|
||||
matches!(self.state, State::Live)
|
||||
}
|
||||
|
||||
pub fn is_exporting(&self) -> bool {
|
||||
matches!(self.state, State::Export { .. })
|
||||
}
|
||||
|
||||
pub fn playback(&self) -> Option<&P> {
|
||||
match &self.state {
|
||||
State::Live => None,
|
||||
State::Replay(player)
|
||||
| State::Export {
|
||||
playback: player, ..
|
||||
} => Some(player),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn playback_mut(&mut self) -> Option<&mut P> {
|
||||
match &mut self.state {
|
||||
State::Live => None,
|
||||
State::Replay(player)
|
||||
| State::Export {
|
||||
playback: player, ..
|
||||
} => Some(player),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn replay(&mut self, playback: P) {
|
||||
assert!(
|
||||
self.is_live(),
|
||||
"end the current presentation before replacing it"
|
||||
);
|
||||
self.state = State::Replay(playback);
|
||||
}
|
||||
|
||||
pub fn export(&mut self, playback: P, export: E) {
|
||||
assert!(
|
||||
self.is_live(),
|
||||
"end the current presentation before starting export"
|
||||
);
|
||||
self.state = State::Export { playback, export };
|
||||
}
|
||||
|
||||
/// Temporarily move the active state out while a tick mutates the player and export together.
|
||||
pub fn take(&mut self) -> State<P, E> {
|
||||
std::mem::replace(&mut self.state, State::Live)
|
||||
}
|
||||
|
||||
pub fn restore(&mut self, state: State<P, E>) {
|
||||
assert!(self.is_live(), "restore only into a detached live session");
|
||||
self.state = state;
|
||||
}
|
||||
|
||||
/// Every exit converges here; a completed capture is already drained and must not be cancelled.
|
||||
pub fn end(&mut self, cause: ExitCause) -> Cleanup {
|
||||
let was_export = self.is_exporting();
|
||||
self.state = State::Live;
|
||||
Cleanup {
|
||||
was_export,
|
||||
cancel_capture: was_export && cause != ExitCause::Finished,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{ExitCause, Session, State};
|
||||
|
||||
#[test]
|
||||
fn export_always_owns_its_playback_and_user_stop_cleans_capture() {
|
||||
let mut session = Session::<u8, &str>::default();
|
||||
session.export(7, "capture");
|
||||
assert!(matches!(
|
||||
session.take(),
|
||||
State::Export {
|
||||
playback: 7,
|
||||
export: "capture"
|
||||
}
|
||||
));
|
||||
session.export(7, "capture");
|
||||
assert_eq!(session.end(ExitCause::UserStop).cancel_capture, true);
|
||||
assert!(session.is_live());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn world_change_and_failure_share_cleanup_while_success_drains() {
|
||||
for cause in [ExitCause::WorldChange, ExitCause::Failed] {
|
||||
let mut session = Session::<(), ()>::default();
|
||||
session.export((), ());
|
||||
assert_eq!(session.end(cause).cancel_capture, true);
|
||||
assert!(session.is_live());
|
||||
}
|
||||
let mut session = Session::<(), ()>::default();
|
||||
session.export((), ());
|
||||
assert_eq!(session.end(ExitCause::Finished).cancel_capture, false);
|
||||
assert!(session.is_live());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn replay_replacement_is_not_an_export_and_stop_is_idempotent() {
|
||||
let mut session = Session::<u8, ()>::default();
|
||||
session.replay(1);
|
||||
assert!(session.playback().is_some());
|
||||
assert_eq!(session.end(ExitCause::Replaced).was_export, false);
|
||||
assert_eq!(session.end(ExitCause::UserStop).was_export, false);
|
||||
assert!(session.is_live());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user