StateManager

Struct StateManager 

Source
pub struct StateManager { /* private fields */ }
Expand description

StateManager manages immutable state with patch history.

§Design

  • State is immutable - all changes go through commits
  • Full history is maintained for replay
  • Supports batch commits and preview operations

§API Philosophy

  • commit/commit_batch: Mutating operations that modify state and record history
  • preview_patch: Pure operation that computes result without modifying state
  • replay_to: Pure operation that reconstructs historical state

§Example

use tirea_state::{StateManager, StateContext};
use serde_json::json;

let manager = StateManager::new(json!({}));

// Get snapshot and create state context
let snapshot = manager.snapshot().await;
let ctx = StateContext::new(&snapshot);

// ... modify state through ctx ...

// Commit patch (modifies state and records history)
manager.commit(ctx.take_tracked_patch("tool:example")).await?;

// Replay to a specific point
let old_state = manager.replay_to(5).await?;

Implementations§

Source§

impl StateManager

Source

pub fn new(initial: Value) -> Self

Create a new StateManager with initial state.

Source

pub async fn register_lattice<T>(&self, path: impl Into<Path>)
where T: Lattice + Serialize + DeserializeOwned + Send + Sync + 'static,

Register a lattice type at the given path for automatic merge during apply.

Source

pub async fn snapshot(&self) -> Value

Get a snapshot of the current state.

Source

pub async fn commit( &self, patch: TrackedPatch, ) -> Result<ApplyResult, StateError>

Commit a single patch, modifying state and recording to history.

This is a mutating operation that:

  1. Applies the patch to current state
  2. Updates the internal state
  3. Records the patch in history for replay
Source

pub async fn commit_batch( &self, patches: Vec<TrackedPatch>, ) -> Result<ApplyResult, StateError>

Commit multiple patches in batch.

Patches are applied in order atomically. If any patch fails, no changes are persisted to state or history.

Source

pub async fn preview_patch(&self, patch: &Patch) -> Result<Value, StateError>

Preview applying a patch without modifying state (pure operation).

This computes what the state would look like after applying the patch, but does not modify the actual state or record to history.

Useful for validation, dry-runs, or showing users what changes would occur.

Source

pub async fn apply( &self, patch: TrackedPatch, ) -> Result<ApplyResult, StateError>

👎Deprecated since 0.2.0: Use commit instead

Deprecated: Use commit instead.

Source

pub async fn apply_batch( &self, patches: Vec<TrackedPatch>, ) -> Result<ApplyResult, StateError>

👎Deprecated since 0.2.0: Use commit_batch instead

Deprecated: Use commit_batch instead.

Source

pub async fn replay_to(&self, index: usize) -> Result<Value, StateError>

Replay state from the beginning up to (and including) the specified index.

Returns the state as it was after applying patches [0..=index] to the initial state.

Source

pub async fn history(&self) -> Vec<TrackedPatch>

Get the full patch history.

Source

pub async fn history_len(&self) -> usize

Get the number of patches in history.

Source

pub async fn clear_history(&self)

Clear history (keeps current state).

Use with caution - this removes the ability to replay.

Source

pub async fn prune_history(&self, keep_last: usize) -> Result<usize, StateError>

Prune history, keeping only the last keep_last patches.

This is useful for long-running systems to prevent unbounded memory growth. The initial state is updated to the state before the remaining patches, so replay_to will continue to work correctly with the remaining patches.

§Arguments
  • keep_last: Number of recent patches to keep. If 0, all patches are removed.
§Returns

The number of patches that were removed.

Source

pub async fn initial(&self) -> Value

Get a snapshot of the initial state.

Trait Implementations§

Source§

impl Clone for StateManager

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.