Skip to main content

continuity_engine/
replay.rs

1//! Pure replay of host-recorded change batches.
2
3use continuity_buffer::{Buffer, BufferId, Revision};
4use continuity_text::Selection;
5
6use crate::{ChangeBatch, Error};
7
8/// Reconstructed state after applying a host event log.
9#[derive(Clone, Debug, Eq, PartialEq)]
10pub struct ReplayedState {
11    /// Replayed buffer id.
12    pub buffer_id: BufferId,
13    /// Final UTF-8 text.
14    pub text: String,
15    /// Final revision.
16    pub revision: Revision,
17    /// Final selection set.
18    pub selections: Vec<Selection>,
19    /// Final running checksum.
20    pub checksum: u64,
21}
22
23/// Replay ordered batches without creating an engine host, database, or file.
24///
25/// This reconstructs text, selections, revisions, and checksums. Undo-tree
26/// restoration remains a host/storage capability rather than an engine load
27/// requirement.
28pub fn replay_change_batches(
29    buffer_id: BufferId,
30    initial_text: &str,
31    initial_revision: Revision,
32    initial_selections: Vec<Selection>,
33    batches: &[ChangeBatch],
34) -> Result<ReplayedState, Error> {
35    let mut buffer = Buffer::from_parts(buffer_id, initial_text, initial_revision);
36    buffer.set_selections(initial_selections);
37    for batch in batches {
38        if batch.buffer_id != buffer_id {
39            return Err(Error::InvalidChangeBatch("buffer id changed".into()));
40        }
41        if batch.revision_before != buffer.revision() {
42            return Err(Error::InvalidChangeBatch(
43                "batch revision chain has a gap".into(),
44            ));
45        }
46        if batch.selections_before != buffer.selections() {
47            return Err(Error::InvalidChangeBatch(
48                "batch selection chain has a gap".into(),
49            ));
50        }
51        for change in &batch.changes {
52            if change.revision_before != buffer.revision() {
53                return Err(Error::InvalidChangeBatch(
54                    "operation revision chain has a gap".into(),
55                ));
56            }
57            let revision = buffer.apply(&change.op)?;
58            if revision != change.revision_after
59                || buffer.running_checksum() != change.checksum_after
60            {
61                return Err(Error::InvalidChangeBatch(
62                    "operation result does not match recorded revision/checksum".into(),
63                ));
64            }
65        }
66        buffer.set_selections(batch.selections_after.clone());
67        if buffer.revision() != batch.revision_after
68            || buffer.running_checksum() != batch.checksum_after
69        {
70            return Err(Error::InvalidChangeBatch(
71                "batch result does not match recorded revision/checksum".into(),
72            ));
73        }
74    }
75    Ok(ReplayedState {
76        buffer_id,
77        text: buffer.rope().to_string(),
78        revision: buffer.revision(),
79        selections: buffer.selections().to_vec(),
80        checksum: buffer.running_checksum(),
81    })
82}