diff options
| author | Josh Kingsley <josh@joshkingsley.me> | 2025-11-13 11:50:32 +0200 |
|---|---|---|
| committer | Josh Kingsley <josh@joshkingsley.me> | 2025-11-13 11:50:32 +0200 |
| commit | bd1fcb295ea4ef921d9e38b44cb27f212d55f644 (patch) | |
| tree | eec756795da1989daff9d39aead3bf3aba2e2112 /crdt/src/lib.rs | |
| parent | bebe4efd7987676201381c9e9cb9dfb16c5adaa3 (diff) | |
feat(crdt): add crdt crate
Diffstat (limited to 'crdt/src/lib.rs')
| -rw-r--r-- | crdt/src/lib.rs | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/crdt/src/lib.rs b/crdt/src/lib.rs new file mode 100644 index 0000000..dcef55c --- /dev/null +++ b/crdt/src/lib.rs @@ -0,0 +1,92 @@ +mod vector_clock; + +use uuid::Uuid; + +use crate::vector_clock::VectorClock; + +pub struct Doc { + ops: Vec<Op>, +} + +impl Doc { + pub fn new(actor_id: &Uuid) -> Self { + Self { + ops: vec![Op { + id: Uuid::now_v7(), + clock: VectorClock::new().inc(actor_id), + payload: OpPayload::Init, + }], + } + } + + pub fn append_op(&mut self, payload: OpPayload, actor_id: &Uuid) { + let clock = self + .ops + .last() + .expect("doc should have at least an Init op") + .clock + .inc(actor_id); + + self.ops.push(Op { + id: Uuid::now_v7(), + clock, + payload, + }); + } + + pub fn realize(&self) -> RealizedDoc { + let mut realized = RealizedDoc::default(); + for op in &self.ops { + op.apply(&mut realized); + } + realized + } +} + +pub struct Op { + id: Uuid, + payload: OpPayload, + clock: VectorClock, +} + +impl Op { + fn apply(&self, realized: &mut RealizedDoc) { + match self.payload { + OpPayload::Init => {} + OpPayload::ChangeSubdivisions { rowId } => {} + } + } +} + +pub enum OpPayload { + Init, + ChangeSubdivisions { rowId: Uuid }, +} + +pub struct RealizedDoc { + grids: Vec<Grid>, +} + +impl Default for RealizedDoc { + fn default() -> Self { + RealizedDoc { + grids: vec![Grid {}], + } + } +} + +pub struct Grid {} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn realize_doc() { + let actor_id = Uuid::now_v7(); + let doc = Doc::new(&actor_id); + let realized = doc.realize(); + + assert!(realized.grids.len() == 1); + } +} |
