use crate::diff::DiffChunk;
use serde::{Deserialize, Serialize};
use std::sync::Mutex;
use once_cell::sync::Lazy;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Snapshot {
pub label: String,
pub debug_repr: String,
pub file: String,
pub line: u32,
pub timestamp_ms: u64,
}
impl Snapshot {
pub fn capture(label: &str, debug_repr: String, file: &str, line: u32) -> Self {
let timestamp_ms = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_millis() as u64)
.unwrap_or(0);
Self {
label: label.to_string(),
debug_repr,
file: file.to_string(),
line,
timestamp_ms,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiffEvent {
pub old: Snapshot,
pub new: Snapshot,
pub chunks: Vec<DiffChunk>,
}
#[derive(Default)]
pub struct InspectorState {
snapshots: Mutex<Vec<Snapshot>>,
diffs: Mutex<Vec<DiffEvent>>,
}
impl InspectorState {
pub fn new() -> Self {
Self::default()
}
pub fn push_snapshot(&self, snap: Snapshot) {
self.snapshots.lock().unwrap().push(snap);
}
pub fn push_diff(&self, old: Snapshot, new: Snapshot, chunks: Vec<DiffChunk>) {
self.diffs.lock().unwrap().push(DiffEvent { old, new, chunks });
}
pub fn snapshots(&self) -> Vec<Snapshot> {
self.snapshots.lock().unwrap().clone()
}
pub fn diffs(&self) -> Vec<DiffEvent> {
self.diffs.lock().unwrap().clone()
}
}
pub static INSPECTOR_STATE: Lazy<InspectorState> = Lazy::new(InspectorState::new);