1pub mod delta;
10pub mod lww;
11pub mod vector_clock;
12
13pub use delta::{apply_delta, compute_delta, merge_deltas, Delta};
14pub use lww::LWWField;
15pub use vector_clock::VectorClock;
16
17use crate::ClientID;
18use serde::{Deserialize, Serialize};
19
20#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
26pub struct Timestamp {
27 pub clock: u64,
29
30 pub client_id: ClientID,
32}
33
34impl Timestamp {
35 pub fn new(clock: u64, client_id: ClientID) -> Self {
37 Self { clock, client_id }
38 }
39
40 pub fn compare_lww(&self, other: &Timestamp) -> std::cmp::Ordering {
47 match self.clock.cmp(&other.clock) {
48 std::cmp::Ordering::Equal => {
49 self.client_id.cmp(&other.client_id)
51 }
52 ordering => ordering,
53 }
54 }
55
56 pub fn is_newer_than(&self, other: &Timestamp) -> bool {
58 self.compare_lww(other) == std::cmp::Ordering::Greater
59 }
60}
61
62#[cfg(test)]
63mod tests {
64 use super::*;
65
66 #[test]
67 fn test_timestamp_comparison() {
68 let ts1 = Timestamp::new(1, "c1".to_string());
69 let ts2 = Timestamp::new(2, "c1".to_string());
70
71 assert!(ts2.is_newer_than(&ts1));
73 assert!(!ts1.is_newer_than(&ts2));
74 }
75
76 #[test]
77 fn test_timestamp_tie_breaking() {
78 let ts1 = Timestamp::new(1, "c1".to_string());
79 let ts2 = Timestamp::new(1, "c2".to_string());
80
81 assert!(ts2.is_newer_than(&ts1));
84 assert!(!ts1.is_newer_than(&ts2));
85 }
86
87 #[test]
88 fn test_timestamp_equality() {
89 let ts1 = Timestamp::new(1, "c1".to_string());
90 let ts2 = Timestamp::new(1, "c1".to_string());
91
92 assert_eq!(ts1.compare_lww(&ts2), std::cmp::Ordering::Equal);
93 assert!(!ts1.is_newer_than(&ts2));
94 assert!(!ts2.is_newer_than(&ts1));
95 }
96}