Skip to main content

haystack_core/graph/
changelog.rs

1// Graph change tracking — records mutations for replication / undo.
2
3use crate::data::HDict;
4
5/// The kind of mutation that was applied.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum DiffOp {
8    /// A new entity was added.
9    Add,
10    /// An existing entity was updated.
11    Update,
12    /// An entity was removed.
13    Remove,
14}
15
16/// A single mutation record.
17#[derive(Debug, Clone)]
18pub struct GraphDiff {
19    /// The graph version *after* this mutation.
20    pub version: u64,
21    /// The kind of mutation.
22    pub op: DiffOp,
23    /// The entity's ref value.
24    pub ref_val: String,
25    /// The entity state before the mutation (None for Add).
26    pub old: Option<HDict>,
27    /// The entity state after the mutation (None for Remove).
28    pub new: Option<HDict>,
29}
30
31#[cfg(test)]
32mod tests {
33    use super::*;
34
35    #[test]
36    fn diff_op_equality() {
37        assert_eq!(DiffOp::Add, DiffOp::Add);
38        assert_ne!(DiffOp::Add, DiffOp::Update);
39        assert_ne!(DiffOp::Update, DiffOp::Remove);
40    }
41
42    #[test]
43    fn diff_op_clone() {
44        let op = DiffOp::Remove;
45        let cloned = op.clone();
46        assert_eq!(op, cloned);
47    }
48
49    #[test]
50    fn graph_diff_construction() {
51        let diff = GraphDiff {
52            version: 1,
53            op: DiffOp::Add,
54            ref_val: "site-1".to_string(),
55            old: None,
56            new: Some(HDict::new()),
57        };
58        assert_eq!(diff.version, 1);
59        assert_eq!(diff.op, DiffOp::Add);
60        assert_eq!(diff.ref_val, "site-1");
61        assert!(diff.old.is_none());
62        assert!(diff.new.is_some());
63    }
64
65    #[test]
66    fn graph_diff_clone() {
67        let diff = GraphDiff {
68            version: 2,
69            op: DiffOp::Update,
70            ref_val: "equip-1".to_string(),
71            old: Some(HDict::new()),
72            new: Some(HDict::new()),
73        };
74        let cloned = diff.clone();
75        assert_eq!(cloned.version, 2);
76        assert_eq!(cloned.op, DiffOp::Update);
77        assert_eq!(cloned.ref_val, "equip-1");
78    }
79}