Skip to main content

hyperi_rustlib/transport/grpc/
token.rs

1// Project:   hyperi-rustlib
2// File:      src/transport/grpc/token.rs
3// Purpose:   gRPC commit token abstraction
4// Language:  Rust
5//
6// License:   BUSL-1.1
7// Copyright: (c) 2026 HYPERI PTY LIMITED
8
9use super::super::traits::CommitToken;
10use std::fmt;
11use std::sync::Arc;
12
13/// Commit token for gRPC transport.
14///
15/// gRPC has no broker-side persistence, so commit is a no-op.
16/// The token provides sequence tracking for application-level ordering.
17#[derive(Debug, Clone)]
18pub struct GrpcToken {
19    /// Local sequence number (monotonically increasing per transport instance).
20    pub seq: u64,
21
22    /// Remote peer identifier (if available from gRPC metadata).
23    pub source: Option<Arc<str>>,
24}
25
26impl GrpcToken {
27    /// Create a new token with sequence number.
28    #[must_use]
29    pub fn new(seq: u64) -> Self {
30        Self { seq, source: None }
31    }
32
33    /// Create a new token with sequence number and source.
34    #[must_use]
35    pub fn with_source(seq: u64, source: Arc<str>) -> Self {
36        Self {
37            seq,
38            source: Some(source),
39        }
40    }
41}
42
43impl CommitToken for GrpcToken {}
44
45impl fmt::Display for GrpcToken {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        match &self.source {
48            Some(src) => write!(f, "grpc:{}:{}", src, self.seq),
49            None => write!(f, "grpc:{}", self.seq),
50        }
51    }
52}