fionn_core/
operations.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2//! Core operation types for DSON processing
3//!
4//! This module defines the fundamental operation types used across the fionn crates.
5
6use super::value::OperationValue;
7
8/// Merge strategies for CRDT conflict resolution
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub enum MergeStrategy {
11    /// Most recent write wins
12    LastWriteWins,
13    /// Combine values additively
14    Additive,
15    /// Keep maximum value
16    Max,
17    /// Keep minimum value
18    Min,
19    /// Union of collections
20    Union,
21    /// Custom merge function
22    Custom(String),
23}
24
25/// DSON operations for document manipulation
26#[derive(Debug, Clone, PartialEq)]
27pub enum DsonOperation {
28    // Structural Operations
29    /// Start a new object at the specified path
30    ObjectStart {
31        /// Path where the object should be created
32        path: String,
33    },
34    /// End the current object at the specified path
35    ObjectEnd {
36        /// Path of the object being ended
37        path: String,
38    },
39    /// Start a new array at the specified path
40    ArrayStart {
41        /// Path where the array should be created
42        path: String,
43    },
44    /// End the current array at the specified path
45    ArrayEnd {
46        /// Path of the array being ended
47        path: String,
48    },
49
50    /// Add a new field at path with value
51    FieldAdd {
52        /// Target path
53        path: String,
54        /// Value to add
55        value: OperationValue,
56    },
57    /// Modify existing field at path
58    FieldModify {
59        /// Target path
60        path: String,
61        /// Old value (for CRDT)
62        old_value: Option<OperationValue>,
63        /// New value
64        new_value: OperationValue,
65    },
66    /// Delete field at path
67    FieldDelete {
68        /// Target path
69        path: String,
70    },
71
72    // Batch Operations
73    /// Execute multiple operations in batch
74    BatchExecute {
75        /// Operations to execute
76        operations: Vec<Self>,
77    },
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    #[test]
85    fn test_merge_strategy_clone() {
86        let strategy = MergeStrategy::LastWriteWins;
87        let cloned = strategy.clone();
88        assert_eq!(strategy, cloned);
89    }
90
91    #[test]
92    fn test_dson_operation_field_add() {
93        let op = DsonOperation::FieldAdd {
94            path: "user.name".to_string(),
95            value: OperationValue::StringRef("Alice".to_string()),
96        };
97        if let DsonOperation::FieldAdd { path, value } = op {
98            assert_eq!(path, "user.name");
99            assert_eq!(value, OperationValue::StringRef("Alice".to_string()));
100        } else {
101            panic!("Expected FieldAdd");
102        }
103    }
104}