summaryrefslogtreecommitdiff
path: root/crdt/src/lib.rs
blob: dcef55cad5e8682df4014518a6e1271d76fedc98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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);
    }
}