use facet::Facet;
use std::fmt;
use crate::{Edge, EdgeKind, Entity, EntityId, Event, Scope, ScopeId, Snapshot};
#[derive(Facet, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[facet(transparent)]
pub struct SeqNo(pub u64);
impl SeqNo {
pub const ZERO: Self = Self(0);
pub fn next(self) -> Self {
Self(self.0.saturating_add(1))
}
}
#[derive(Facet, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[facet(transparent)]
pub struct StreamId(pub String);
#[derive(Facet, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[facet(transparent)]
pub struct CutId(pub String);
impl CutId {
pub fn new(id: impl Into<String>) -> Self {
Self(id.into())
}
pub fn from_ordinal(value: u64) -> Self {
Self(format!("cut:{value}"))
}
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
impl fmt::Display for CutId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
#[derive(Facet)]
#[repr(u8)]
#[facet(rename_all = "snake_case")]
pub enum Change {
UpsertEntity(Entity),
UpsertScope(Scope),
RemoveEntity { id: EntityId },
RemoveScope { id: ScopeId },
UpsertEntityScopeLink {
entity_id: EntityId,
scope_id: ScopeId,
},
RemoveEntityScopeLink {
entity_id: EntityId,
scope_id: ScopeId,
},
UpsertEdge(Edge),
RemoveEdge {
src: EntityId,
dst: EntityId,
kind: EdgeKind,
},
AppendEvent(Event),
}
#[derive(Facet)]
pub struct StampedChange {
pub seq_no: SeqNo,
pub change: Change,
}
#[derive(Facet)]
pub struct PullChangesRequest {
pub stream_id: StreamId,
pub from_seq_no: SeqNo,
pub max_changes: u32,
}
#[derive(Facet)]
pub struct PullChangesResponse {
pub stream_id: StreamId,
pub from_seq_no: SeqNo,
pub next_seq_no: SeqNo,
pub changes: Vec<StampedChange>,
pub truncated: bool,
#[facet(skip_unless_truthy)]
pub compacted_before_seq_no: Option<SeqNo>,
}
#[derive(Facet)]
pub struct StreamCursor {
pub stream_id: StreamId,
pub next_seq_no: SeqNo,
}
#[derive(Facet)]
pub struct CutRequest {
pub cut_id: CutId,
}
#[derive(Facet)]
pub struct CutAck {
pub cut_id: CutId,
pub cursor: StreamCursor,
}
#[derive(Facet)]
pub struct DiffCheckpoint {
pub stream_id: StreamId,
pub at_seq_no: SeqNo,
pub snapshot: Snapshot,
}