Files
zesdex/src/app/state/snapshot.rs
T

34 lines
991 B
Rust
Raw Normal View History

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