telltale_vm/
persistence.rs1use serde::{Deserialize, Serialize};
4
5use crate::session::SessionId;
6
7pub trait PersistenceModel {
9 type PState: Clone;
11 type Delta: Clone;
13
14 fn apply(state: &mut Self::PState, delta: &Self::Delta) -> Result<(), String>;
20
21 fn derive(before: &Self::PState, after: &Self::PState) -> Result<Self::Delta, String>;
27
28 #[must_use]
30 fn open_delta(session: SessionId) -> Self::Delta;
31
32 #[must_use]
34 fn close_delta(session: SessionId) -> Self::Delta;
35
36 #[must_use]
38 fn invoke_delta(_session: SessionId, _action: &str) -> Option<Self::Delta> {
39 None
40 }
41
42 #[must_use]
44 #[allow(non_snake_case)]
45 fn openDelta(session: SessionId) -> Self::Delta {
46 Self::open_delta(session)
47 }
48
49 #[must_use]
51 #[allow(non_snake_case)]
52 fn closeDelta(session: SessionId) -> Self::Delta {
53 Self::close_delta(session)
54 }
55
56 #[must_use]
58 #[allow(non_snake_case)]
59 fn invokeDelta(session: SessionId, action: &str) -> Option<Self::Delta> {
60 Self::invoke_delta(session, action)
61 }
62}
63
64#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
66pub struct NoopPersistence;
67
68impl PersistenceModel for NoopPersistence {
69 type PState = ();
70 type Delta = ();
71
72 fn apply(_state: &mut Self::PState, _delta: &Self::Delta) -> Result<(), String> {
73 Ok(())
74 }
75
76 fn derive(_before: &Self::PState, _after: &Self::PState) -> Result<Self::Delta, String> {
77 Ok(())
78 }
79
80 fn open_delta(_session: SessionId) -> Self::Delta {}
81
82 fn close_delta(_session: SessionId) -> Self::Delta {}
83}