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
use crdt_tree::{OpMove, State, TreeId, TreeMeta, TreeReplica};
use brb::BRBDataType;
use serde::Serialize;
use std::{fmt::Debug, hash::Hash};
use thiserror::Error;
#[derive(Debug, Serialize, PartialEq, Eq, Clone)]
pub struct BRBTree<A: Clone + Hash + Ord + Debug, ID: TreeId, M: TreeMeta> {
actor: A,
treereplica: TreeReplica<ID, M, A>,
}
impl<A: Clone + Hash + Ord + Debug, ID: TreeId, M: TreeMeta> BRBTree<A, ID, M> {
pub fn opmove(&self, parent: ID, meta: M, child: ID) -> OpMove<ID, M, A> {
self.treereplica.opmove(parent, meta, child)
}
pub fn actor(&self) -> &A {
&self.actor
}
pub fn treestate(&self) -> &State<ID, M, A> {
&self.treereplica.state()
}
pub fn treereplica(&self) -> &TreeReplica<ID, M, A> {
&self.treereplica
}
}
#[derive(Error, Debug, PartialEq, Eq)]
pub enum ValidationError {
#[error("The source actor does not match the actor associated with the operation")]
SourceDoesNotMatchOp,
}
impl<
A: Hash + Ord + Clone + Debug + Serialize + 'static,
ID: TreeId + Debug + Serialize,
M: TreeMeta + Eq + Debug + Hash + Serialize,
> BRBDataType<A> for BRBTree<A, ID, M>
{
type Op = OpMove<ID, M, A>;
type ValidationError = ValidationError;
fn new(actor: A) -> Self {
BRBTree {
actor: actor.clone(),
treereplica: TreeReplica::new(actor),
}
}
fn validate(&self, source: &A, op: &Self::Op) -> Result<(), Self::ValidationError> {
if op.timestamp().actor_id() != source {
Err(ValidationError::SourceDoesNotMatchOp)
} else {
Ok(())
}
}
fn apply(&mut self, op: Self::Op) {
self.treereplica.apply_op(op);
}
}