summaryrefslogtreecommitdiff
path: root/crdt/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crdt/src/lib.rs')
-rw-r--r--crdt/src/lib.rs38
1 files changed, 31 insertions, 7 deletions
diff --git a/crdt/src/lib.rs b/crdt/src/lib.rs
index a92b52d..cd86b97 100644
--- a/crdt/src/lib.rs
+++ b/crdt/src/lib.rs
@@ -1,6 +1,6 @@
mod vector_clock;
-use std::{collections::BTreeSet, fmt::Display};
+use std::{collections::BTreeSet, fmt::Display, mem};
use num_rational::Ratio;
use uuid::Uuid;
@@ -165,6 +165,10 @@ impl Op {
(end_cell_idx, start_cell_idx)
};
+ for entry in row.value_mut().cells[i..(j + 1)].iter_mut() {
+ entry.delete(self.id);
+ }
+
let span_duration: Ratio<u32> = row.value().cells[i..j + 1]
.iter()
.map(|cell| cell.value().duration)
@@ -210,12 +214,20 @@ pub struct Doc {
}
#[derive(Debug)]
-pub enum Entry<T> {
+pub enum Entry<T: Default> {
Active { value: T },
Deleted { value: T, deleted_by: Uuid },
}
-impl<T> Entry<T> {
+impl<T: Default> Default for Entry<T> {
+ fn default() -> Self {
+ Self::Active {
+ value: T::default(),
+ }
+ }
+}
+
+impl<T: Default> Entry<T> {
pub fn active(value: T) -> Self {
Self::Active { value }
}
@@ -240,27 +252,39 @@ impl<T> Entry<T> {
Self::Deleted { value, .. } => value,
}
}
+
+ pub fn delete(&mut self, op_id: Uuid) {
+ if matches!(self, Self::Active { .. }) {
+ *self = match mem::take(self) {
+ Self::Active { value } => Self::Deleted {
+ value,
+ deleted_by: op_id,
+ },
+ _ => unreachable!(),
+ }
+ }
+ }
}
-#[derive(Debug)]
+#[derive(Debug, Default)]
pub struct Grid {
id: DerivedId,
rows: Vec<Entry<Row>>,
}
-#[derive(Debug)]
+#[derive(Debug, Default)]
pub struct Row {
id: DerivedId,
cells: Vec<Entry<Cell>>,
}
-#[derive(Debug)]
+#[derive(Debug, Default)]
pub struct Cell {
id: DerivedId,
duration: Ratio<u32>,
}
-#[derive(PartialEq, Eq, Debug, Clone, Copy)]
+#[derive(PartialEq, Eq, Debug, Clone, Copy, Default)]
pub struct DerivedId {
// TODO These IDs can be interned on the Doc
id: Uuid,