titan_rust_client/
state.rs1use std::fmt;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum ConnectionState {
8 Connected,
10
11 Reconnecting {
13 attempt: u32,
15 },
16
17 Disconnected {
19 reason: String,
21 },
22}
23
24impl ConnectionState {
25 pub fn is_connected(&self) -> bool {
27 matches!(self, Self::Connected)
28 }
29
30 pub fn is_reconnecting(&self) -> bool {
32 matches!(self, Self::Reconnecting { .. })
33 }
34
35 pub fn is_disconnected(&self) -> bool {
37 matches!(self, Self::Disconnected { .. })
38 }
39
40 pub fn reconnect_attempt(&self) -> Option<u32> {
42 match self {
43 Self::Reconnecting { attempt } => Some(*attempt),
44 _ => None,
45 }
46 }
47
48 pub fn disconnect_reason(&self) -> Option<&str> {
50 match self {
51 Self::Disconnected { reason } => Some(reason),
52 _ => None,
53 }
54 }
55}
56
57impl Default for ConnectionState {
58 fn default() -> Self {
59 Self::Disconnected {
60 reason: "Not connected".to_string(),
61 }
62 }
63}
64
65impl fmt::Display for ConnectionState {
66 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67 match self {
68 Self::Connected => write!(f, "Connected"),
69 Self::Reconnecting { attempt } => write!(f, "Reconnecting (attempt {})", attempt),
70 Self::Disconnected { reason } => write!(f, "Disconnected: {}", reason),
71 }
72 }
73}