use crate::error::{Error, Result};
use crate::workflow::WorkflowContext;
use serde::{de::DeserializeOwned, Serialize};
use std::collections::HashMap;
use std::time::SystemTime;
#[derive(Debug, Clone)]
pub struct StateSnapshot {
pub workflow_id: String,
pub timestamp: SystemTime,
state: HashMap<String, Vec<u8>>,
label: Option<String>,
}
impl StateSnapshot {
pub fn capture(ctx: &WorkflowContext) -> Self {
Self {
workflow_id: ctx.workflow_id().to_string(),
timestamp: SystemTime::now(),
state: HashMap::new(),
label: None,
}
}
pub fn capture_with_label(ctx: &WorkflowContext, label: impl Into<String>) -> Self {
let mut snapshot = Self::capture(ctx);
snapshot.label = Some(label.into());
snapshot
}
pub fn new(workflow_id: impl Into<String>) -> Self {
Self {
workflow_id: workflow_id.into(),
timestamp: SystemTime::now(),
state: HashMap::new(),
label: None,
}
}
pub fn set<T: Serialize>(&mut self, key: impl Into<String>, value: &T) -> Result<()> {
let key = key.into();
let value_bytes = serde_json::to_vec(value)
.map_err(|e| Error::Serialization(format!("Failed to serialize value: {}", e)))?;
self.state.insert(key, value_bytes);
Ok(())
}
pub fn get<T: DeserializeOwned>(&self, key: &str) -> Result<Option<T>> {
match self.state.get(key) {
Some(value_bytes) => {
let value: T = serde_json::from_slice(value_bytes).map_err(|e| {
Error::Serialization(format!("Failed to deserialize value: {}", e))
})?;
Ok(Some(value))
}
None => Ok(None),
}
}
pub fn contains(&self, key: &str) -> bool {
self.state.contains_key(key)
}
pub fn keys(&self) -> Vec<String> {
self.state.keys().cloned().collect()
}
pub fn len(&self) -> usize {
self.state.len()
}
pub fn is_empty(&self) -> bool {
self.state.is_empty()
}
pub fn label(&self) -> Option<&str> {
self.label.as_deref()
}
pub fn diff(&self, other: &StateSnapshot) -> StateDiff {
let mut added = Vec::new();
let mut removed = Vec::new();
let mut changed = Vec::new();
for key in other.keys() {
if !self.contains(&key) {
added.push(key.clone());
} else if self.state.get(&key) != other.state.get(&key) {
changed.push(key.clone());
}
}
for key in self.keys() {
if !other.contains(&key) {
removed.push(key.clone());
}
}
StateDiff {
from_label: self.label.clone(),
to_label: other.label.clone(),
added_keys: added,
removed_keys: removed,
changed_keys: changed,
}
}
pub fn with_label(mut self, label: impl Into<String>) -> Self {
self.label = Some(label.into());
self
}
pub fn raw_state(&self) -> &HashMap<String, Vec<u8>> {
&self.state
}
}
#[derive(Debug, Clone)]
pub struct StateDiff {
pub from_label: Option<String>,
pub to_label: Option<String>,
pub added_keys: Vec<String>,
pub removed_keys: Vec<String>,
pub changed_keys: Vec<String>,
}
impl StateDiff {
pub fn has_changes(&self) -> bool {
!self.added_keys.is_empty()
|| !self.removed_keys.is_empty()
|| !self.changed_keys.is_empty()
}
pub fn all_changed_keys(&self) -> Vec<String> {
let mut keys = Vec::new();
keys.extend(self.added_keys.iter().cloned());
keys.extend(self.removed_keys.iter().cloned());
keys.extend(self.changed_keys.iter().cloned());
keys
}
pub fn change_count(&self) -> usize {
self.added_keys.len() + self.removed_keys.len() + self.changed_keys.len()
}
pub fn summary(&self) -> String {
let mut parts = Vec::new();
if !self.added_keys.is_empty() {
parts.push(format!("{} added", self.added_keys.len()));
}
if !self.removed_keys.is_empty() {
parts.push(format!("{} removed", self.removed_keys.len()));
}
if !self.changed_keys.is_empty() {
parts.push(format!("{} changed", self.changed_keys.len()));
}
if parts.is_empty() {
"No changes".to_string()
} else {
parts.join(", ")
}
}
pub fn added_keys(&self) -> &[String] {
&self.added_keys
}
pub fn removed_keys(&self) -> &[String] {
&self.removed_keys
}
pub fn changed_keys(&self) -> &[String] {
&self.changed_keys
}
}
#[derive(Debug, Clone)]
#[allow(dead_code)] pub struct StateHistory {
workflow_id: String,
snapshots: Vec<StateSnapshot>,
}
#[allow(dead_code)] impl StateHistory {
pub fn new(workflow_id: impl Into<String>) -> Self {
Self {
workflow_id: workflow_id.into(),
snapshots: Vec::new(),
}
}
pub fn add(&mut self, snapshot: StateSnapshot) {
self.snapshots.push(snapshot);
}
pub fn capture(&mut self, ctx: &WorkflowContext, label: Option<String>) {
let snapshot = if let Some(label) = label {
StateSnapshot::capture_with_label(ctx, label)
} else {
StateSnapshot::capture(ctx)
};
self.add(snapshot);
}
pub fn snapshots(&self) -> &[StateSnapshot] {
&self.snapshots
}
pub fn latest(&self) -> Option<&StateSnapshot> {
self.snapshots.last()
}
pub fn first(&self) -> Option<&StateSnapshot> {
self.snapshots.first()
}
pub fn len(&self) -> usize {
self.snapshots.len()
}
pub fn is_empty(&self) -> bool {
self.snapshots.is_empty()
}
pub fn get_by_label(&self, label: &str) -> Option<&StateSnapshot> {
self.snapshots
.iter()
.find(|s| s.label.as_deref() == Some(label))
}
pub fn diff_between(&self, from_idx: usize, to_idx: usize) -> Option<StateDiff> {
if from_idx >= self.snapshots.len() || to_idx >= self.snapshots.len() {
return None;
}
Some(self.snapshots[from_idx].diff(&self.snapshots[to_idx]))
}
pub fn overall_diff(&self) -> Option<StateDiff> {
if self.snapshots.len() < 2 {
return None;
}
let first_idx = 0;
let last_idx = self.snapshots.len() - 1;
self.diff_between(first_idx, last_idx)
}
pub fn clear(&mut self) {
self.snapshots.clear();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_state_snapshot_creation() {
let snapshot = StateSnapshot::new("wf-123");
assert_eq!(snapshot.workflow_id, "wf-123");
assert!(snapshot.is_empty());
assert!(snapshot.label.is_none());
}
#[test]
fn test_state_snapshot_set_get() {
let mut snapshot = StateSnapshot::new("wf-123");
snapshot.set("counter", &42i32).unwrap();
snapshot.set("name", &"test".to_string()).unwrap();
let counter: Option<i32> = snapshot.get("counter").unwrap();
assert_eq!(counter, Some(42));
let name: Option<String> = snapshot.get("name").unwrap();
assert_eq!(name, Some("test".to_string()));
let missing: Option<String> = snapshot.get("missing").unwrap();
assert_eq!(missing, None);
}
#[test]
fn test_state_snapshot_keys() {
let mut snapshot = StateSnapshot::new("wf-123");
snapshot.set("key1", &1).unwrap();
snapshot.set("key2", &2).unwrap();
let keys = snapshot.keys();
assert_eq!(keys.len(), 2);
assert!(keys.contains(&"key1".to_string()));
assert!(keys.contains(&"key2".to_string()));
}
#[test]
fn test_state_snapshot_contains() {
let mut snapshot = StateSnapshot::new("wf-123");
snapshot.set("exists", &true).unwrap();
assert!(snapshot.contains("exists"));
assert!(!snapshot.contains("missing"));
}
#[test]
fn test_state_snapshot_with_label() {
let snapshot = StateSnapshot::new("wf-123").with_label("after_payment");
assert_eq!(snapshot.label(), Some("after_payment"));
}
#[test]
fn test_state_diff_no_changes() {
let mut s1 = StateSnapshot::new("wf-123");
s1.set("key", &42).unwrap();
let mut s2 = StateSnapshot::new("wf-123");
s2.set("key", &42).unwrap();
let diff = s1.diff(&s2);
assert!(!diff.has_changes());
assert_eq!(diff.change_count(), 0);
}
#[test]
fn test_state_diff_added_keys() {
let s1 = StateSnapshot::new("wf-123");
let mut s2 = StateSnapshot::new("wf-123");
s2.set("new_key", &1).unwrap();
let diff = s1.diff(&s2);
assert!(diff.has_changes());
assert_eq!(diff.added_keys.len(), 1);
assert!(diff.added_keys.contains(&"new_key".to_string()));
}
#[test]
fn test_state_diff_removed_keys() {
let mut s1 = StateSnapshot::new("wf-123");
s1.set("old_key", &1).unwrap();
let s2 = StateSnapshot::new("wf-123");
let diff = s1.diff(&s2);
assert!(diff.has_changes());
assert_eq!(diff.removed_keys.len(), 1);
assert!(diff.removed_keys.contains(&"old_key".to_string()));
}
#[test]
fn test_state_diff_changed_keys() {
let mut s1 = StateSnapshot::new("wf-123");
s1.set("key", &1).unwrap();
let mut s2 = StateSnapshot::new("wf-123");
s2.set("key", &2).unwrap();
let diff = s1.diff(&s2);
assert!(diff.has_changes());
assert_eq!(diff.changed_keys.len(), 1);
assert!(diff.changed_keys.contains(&"key".to_string()));
}
#[test]
fn test_state_diff_summary() {
let mut s1 = StateSnapshot::new("wf-123");
s1.set("old", &1).unwrap();
s1.set("changed", &1).unwrap();
let mut s2 = StateSnapshot::new("wf-123");
s2.set("new", &2).unwrap();
s2.set("changed", &2).unwrap();
let diff = s1.diff(&s2);
let summary = diff.summary();
assert!(summary.contains("added"));
assert!(summary.contains("removed"));
assert!(summary.contains("changed"));
}
#[test]
fn test_state_history_creation() {
let history = StateHistory::new("wf-123");
assert_eq!(history.workflow_id, "wf-123");
assert!(history.is_empty());
}
#[test]
fn test_state_history_add() {
let mut history = StateHistory::new("wf-123");
let snapshot1 = StateSnapshot::new("wf-123").with_label("snapshot1");
let snapshot2 = StateSnapshot::new("wf-123").with_label("snapshot2");
history.add(snapshot1);
history.add(snapshot2);
assert_eq!(history.len(), 2);
assert_eq!(history.first().unwrap().label(), Some("snapshot1"));
assert_eq!(history.latest().unwrap().label(), Some("snapshot2"));
}
#[test]
fn test_state_history_get_by_label() {
let mut history = StateHistory::new("wf-123");
let snapshot = StateSnapshot::new("wf-123").with_label("test_label");
history.add(snapshot);
let found = history.get_by_label("test_label");
assert!(found.is_some());
assert_eq!(found.unwrap().label(), Some("test_label"));
let not_found = history.get_by_label("missing");
assert!(not_found.is_none());
}
#[test]
fn test_state_history_diff_between() {
let mut history = StateHistory::new("wf-123");
let mut s1 = StateSnapshot::new("wf-123");
s1.set("key", &1).unwrap();
history.add(s1);
let mut s2 = StateSnapshot::new("wf-123");
s2.set("key", &2).unwrap();
history.add(s2);
let diff = history.diff_between(0, 1).unwrap();
assert!(diff.has_changes());
assert_eq!(diff.changed_keys.len(), 1);
}
#[test]
fn test_state_history_overall_diff() {
let mut history = StateHistory::new("wf-123");
let s1 = StateSnapshot::new("wf-123");
history.add(s1);
let mut s2 = StateSnapshot::new("wf-123");
s2.set("new_key", &1).unwrap();
history.add(s2);
let diff = history.overall_diff().unwrap();
assert!(diff.has_changes());
assert_eq!(diff.added_keys.len(), 1);
}
#[test]
fn test_state_history_clear() {
let mut history = StateHistory::new("wf-123");
history.add(StateSnapshot::new("wf-123"));
history.add(StateSnapshot::new("wf-123"));
assert_eq!(history.len(), 2);
history.clear();
assert_eq!(history.len(), 0);
assert!(history.is_empty());
}
}