From bb5edb941c3a617369a995254351c134882e2cce Mon Sep 17 00:00:00 2001 From: Vassiliy Yegorov Date: Mon, 15 Jun 2026 15:30:16 +0700 Subject: [PATCH] feat(core): Snapshot derives Deserialize + PartialEq for disk persistence Co-Authored-By: Claude Sonnet 4.6 --- crates/spacesh-core/Cargo.toml | 3 +++ crates/spacesh-core/src/snapshot.rs | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/crates/spacesh-core/Cargo.toml b/crates/spacesh-core/Cargo.toml index afe7f6e..926529b 100644 --- a/crates/spacesh-core/Cargo.toml +++ b/crates/spacesh-core/Cargo.toml @@ -7,3 +7,6 @@ version.workspace = true alacritty_terminal.workspace = true serde.workspace = true spacesh-proto = { path = "../spacesh-proto" } + +[dev-dependencies] +serde_json.workspace = true diff --git a/crates/spacesh-core/src/snapshot.rs b/crates/spacesh-core/src/snapshot.rs index 85597a1..37ec462 100644 --- a/crates/spacesh-core/src/snapshot.rs +++ b/crates/spacesh-core/src/snapshot.rs @@ -1,11 +1,11 @@ -use serde::Serialize; +use serde::{Deserialize, Serialize}; use alacritty_terminal::index::Point; use alacritty_terminal::term::cell::Flags; use alacritty_terminal::vte::ansi::Color; use crate::grid::GridSurface; /// Serializable snapshot returned by `attach`. -#[derive(Debug, Clone, Serialize)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct Snapshot { /// ANSI byte dump suitable for `xterm.write()`. pub ansi: String, @@ -120,6 +120,18 @@ mod tests { assert_eq!(a.rows, 3); } + #[test] + fn snapshot_round_trips_through_json() { + let mut g = GridSurface::new(20, 4); + g.feed(b"hello"); + let snap = snapshot_ansi(&g); + let json = serde_json::to_string(&snap).unwrap(); + let back: Snapshot = serde_json::from_str(&json).unwrap(); + assert_eq!(back.ansi, snap.ansi); + assert_eq!((back.cols, back.rows), (snap.cols, snap.rows)); + assert_eq!((back.cursor_row, back.cursor_col), (snap.cursor_row, snap.cursor_col)); + } + #[test] fn cursor_is_one_based_after_input() { let mut g = GridSurface::new(10, 3);