1use facet::Facet;
2use std::fmt;
3
4use crate::{Edge, EdgeKind, Entity, EntityId, Event, Scope, ScopeId, Snapshot};
5
6#[derive(Facet, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
10#[facet(transparent)]
11pub struct SeqNo(pub u64);
12
13impl SeqNo {
14 pub const ZERO: Self = Self(0);
15
16 pub fn next(self) -> Self {
17 Self(self.0.saturating_add(1))
18 }
19}
20
21#[derive(Facet, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
26#[facet(transparent)]
27pub struct StreamId(pub String);
28
29#[derive(Facet, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
31#[facet(transparent)]
32pub struct CutId(pub String);
33
34impl CutId {
35 pub fn new(id: impl Into<String>) -> Self {
36 Self(id.into())
37 }
38
39 pub fn from_ordinal(value: u64) -> Self {
40 Self(format!("cut:{value}"))
41 }
42
43 pub fn as_str(&self) -> &str {
44 self.0.as_str()
45 }
46}
47
48impl fmt::Display for CutId {
49 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50 f.write_str(&self.0)
51 }
52}
53
54#[derive(Facet)]
56#[repr(u8)]
57#[facet(rename_all = "snake_case")]
58pub enum Change {
59 UpsertEntity(Entity),
61 UpsertScope(Scope),
63 RemoveEntity { id: EntityId },
65 RemoveScope { id: ScopeId },
67 UpsertEntityScopeLink {
69 entity_id: EntityId,
70 scope_id: ScopeId,
71 },
72 RemoveEntityScopeLink {
74 entity_id: EntityId,
75 scope_id: ScopeId,
76 },
77 UpsertEdge(Edge),
79 RemoveEdge {
81 src: EntityId,
82 dst: EntityId,
83 kind: EdgeKind,
84 },
85 AppendEvent(Event),
87}
88
89#[derive(Facet)]
91pub struct StampedChange {
92 pub seq_no: SeqNo,
93 pub change: Change,
94}
95
96#[derive(Facet)]
98pub struct PullChangesRequest {
99 pub stream_id: StreamId,
101 pub from_seq_no: SeqNo,
103 pub max_changes: u32,
105}
106
107#[derive(Facet)]
109pub struct PullChangesResponse {
110 pub stream_id: StreamId,
112 pub from_seq_no: SeqNo,
114 pub next_seq_no: SeqNo,
116 pub changes: Vec<StampedChange>,
118 pub truncated: bool,
120 #[facet(skip_unless_truthy)]
124 pub compacted_before_seq_no: Option<SeqNo>,
125}
126
127#[derive(Facet)]
129pub struct StreamCursor {
130 pub stream_id: StreamId,
131 pub next_seq_no: SeqNo,
132}
133
134#[derive(Facet)]
136pub struct CutRequest {
137 pub cut_id: CutId,
138}
139
140#[derive(Facet)]
142pub struct CutAck {
143 pub cut_id: CutId,
144 pub cursor: StreamCursor,
145}
146
147#[derive(Facet)]
152pub struct DiffCheckpoint {
153 pub stream_id: StreamId,
154 pub at_seq_no: SeqNo,
155 pub snapshot: Snapshot,
156}