Skip to main content

dig_peer/
state.rs

1//! The [`DigPeer`](crate::DigPeer) connection lifecycle.
2//!
3//! A DigPeer moves through a small, explicit state machine so callers (and the crate itself) always
4//! know whether an RPC may be issued. The transitions are deliberately minimal — dig-peer's job is to
5//! be a thin, honest client, so the state is just enough to reject use-after-disconnect and to report
6//! liveness, not a full reconnection engine (opportunistic re-dial is a documented follow-up).
7
8/// The lifecycle state of a [`DigPeer`](crate::DigPeer) connection.
9///
10/// A freshly [`connect`](crate::DigPeer::connect)ed peer is [`Connected`](PeerState::Connected).
11/// [`disconnect`](crate::DigPeer::disconnect) moves it to [`Closed`](PeerState::Closed) terminally;
12/// any RPC attempted after that fails with [`DigPeerError::InvalidState`](crate::DigPeerError::InvalidState).
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
14#[non_exhaustive]
15pub enum PeerState {
16    /// The mTLS connection is established and RPCs may be issued.
17    Connected,
18    /// The connection was explicitly torn down; no further RPCs are possible. Terminal.
19    Closed,
20}
21
22impl PeerState {
23    /// Whether an RPC may be issued in this state.
24    #[must_use]
25    pub const fn is_usable(self) -> bool {
26        matches!(self, PeerState::Connected)
27    }
28}