Skip to main content

crdt_graph/types/
simple.rs

1use crate::graph::{
2    TwoPTwoPAddEdge, TwoPTwoPAddVertex, TwoPTwoPGraph, TwoPTwoPId, UpdateOperation,
3};
4use super::{RemoveEdge, RemoveVertex};
5use uuid::Uuid;
6
7/// A vertex-add operation carrying only an identifier.
8#[derive(Clone, Debug, PartialEq, Eq, Hash)]
9pub struct AddVertex {
10    pub id: Uuid,
11}
12
13impl TwoPTwoPId<Uuid> for AddVertex {
14    fn id(&self) -> &Uuid {
15        &self.id
16    }
17}
18
19impl TwoPTwoPAddVertex<Uuid> for AddVertex {}
20
21/// An edge-add operation carrying only identifiers.
22#[derive(Clone, Debug, PartialEq, Eq, Hash)]
23pub struct AddEdge {
24    pub id: Uuid,
25    pub source: Uuid,
26    pub target: Uuid,
27}
28
29impl TwoPTwoPId<Uuid> for AddEdge {
30    fn id(&self) -> &Uuid {
31        &self.id
32    }
33}
34
35impl TwoPTwoPAddEdge<Uuid> for AddEdge {
36    fn source(&self) -> &Uuid {
37        &self.source
38    }
39    fn target(&self) -> &Uuid {
40        &self.target
41    }
42}
43
44/// A graph whose vertices and edges carry no payload beyond their IDs.
45pub type Graph = TwoPTwoPGraph<AddVertex, RemoveVertex, AddEdge, RemoveEdge, Uuid>;
46
47/// An update operation for [`Graph`].
48pub type Operation = UpdateOperation<AddVertex, RemoveVertex, AddEdge, RemoveEdge>;
49
50impl From<AddVertex> for Operation {
51    fn from(v: AddVertex) -> Self {
52        UpdateOperation::AddVertex(v)
53    }
54}
55
56impl From<RemoveVertex> for Operation {
57    fn from(v: RemoveVertex) -> Self {
58        UpdateOperation::RemoveVertex(v)
59    }
60}
61
62impl From<AddEdge> for Operation {
63    fn from(e: AddEdge) -> Self {
64        UpdateOperation::AddEdge(e)
65    }
66}
67
68impl From<RemoveEdge> for Operation {
69    fn from(e: RemoveEdge) -> Self {
70        UpdateOperation::RemoveEdge(e)
71    }
72}