Skip to main content

crdt_graph/types/
string.rs

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