2026-07-12 11:28:39 +07:00
|
|
|
//! Opaque, serializable snapshot of application state used for
|
|
|
|
|
//! attach/daemon IPC transfer.
|
|
|
|
|
|
2026-07-11 13:16:10 +07:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
2026-07-12 11:28:39 +07:00
|
|
|
/// A JSON-boxed snapshot of app state, opaque to the transport layer.
|
2026-07-11 13:16:10 +07:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
|
pub struct StateSnapshot {
|
|
|
|
|
pub snapshot: serde_json::Value,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl StateSnapshot {
|
2026-07-12 11:28:39 +07:00
|
|
|
/// Create an empty snapshot (`{}`).
|
2026-07-11 13:16:10 +07:00
|
|
|
pub fn new() -> Self {
|
|
|
|
|
StateSnapshot {
|
|
|
|
|
snapshot: serde_json::json!({}),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 11:28:39 +07:00
|
|
|
/// Serialize a snapshot to bytes for transport over the daemon socket.
|
|
|
|
|
///
|
|
|
|
|
/// Return: JSON-encoded bytes, or a serde error.
|
2026-07-11 13:16:10 +07:00
|
|
|
pub fn serialize_snapshot(snapshot: &StateSnapshot) -> anyhow::Result<Vec<u8>> {
|
|
|
|
|
Ok(serde_json::to_vec(snapshot)?)
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 11:28:39 +07:00
|
|
|
/// Parse a snapshot previously produced by `serialize_snapshot`.
|
|
|
|
|
///
|
|
|
|
|
/// Return: the decoded `StateSnapshot`, or a serde error.
|
2026-07-11 13:16:10 +07:00
|
|
|
pub fn deserialize_snapshot(data: &[u8]) -> anyhow::Result<StateSnapshot> {
|
|
|
|
|
Ok(serde_json::from_slice(data)?)
|
|
|
|
|
}
|