Skip to main content

lightstreamer_rs/client/
model.rs

1/******************************************************************************
2   Author: Joaquín Béjar García
3   Email: jb@taunais.com
4   Date: 16/5/25
5******************************************************************************/
6
7/// Represents the current status of the `LightstreamerClient`.
8#[derive(Debug, Clone, PartialEq, Eq)]
9#[repr(u8)]
10pub enum ClientStatus {
11    /// The client is attempting to connect to the Lightstreamer Server.
12    Connecting,
13    /// The client has successfully connected to the Lightstreamer Server.
14    /// Contains the type of connection established.
15    Connected(ConnectionType),
16    /// The connection has been temporarily interrupted.
17    /// The client will automatically try to recover the connection.
18    Stalled,
19    /// The client is disconnected from the Lightstreamer Server.
20    /// Contains information about the disconnection type.
21    Disconnected(DisconnectionType),
22}
23
24impl std::fmt::Display for ClientStatus {
25    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26        match self {
27            ClientStatus::Connecting => write!(f, "CONNECTING"),
28            ClientStatus::Connected(connection_type) => match connection_type {
29                ConnectionType::HttpPolling => write!(f, "CONNECTED:HTTP-POLLING"),
30                ConnectionType::HttpStreaming => write!(f, "CONNECTED:HTTP-STREAMING"),
31                ConnectionType::StreamSensing => write!(f, "CONNECTED:STREAM-SENSING"),
32                ConnectionType::WsPolling => write!(f, "CONNECTED:WS-POLLING"),
33                ConnectionType::WsStreaming => write!(f, "CONNECTED:WS-STREAMING"),
34            },
35            ClientStatus::Stalled => write!(f, "STALLED"),
36            ClientStatus::Disconnected(disconnection_type) => match disconnection_type {
37                DisconnectionType::WillRetry => write!(f, "DISCONNECTED:WILL-RETRY"),
38                DisconnectionType::TryingRecovery => write!(f, "DISCONNECTED:TRYING-RECOVERY"),
39            },
40        }
41    }
42}
43
44/// Represents the type of connection established with the Lightstreamer Server.
45///
46/// This enum indicates the specific transport protocol and connection mode being used
47/// for communication with the server.
48#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
49#[repr(u8)]
50pub enum ConnectionType {
51    /// Connection established using HTTP polling transport.
52    HttpPolling,
53    /// Connection established using HTTP streaming transport.
54    HttpStreaming,
55    /// Connection in stream-sensing mode, where the client is determining the best
56    /// transport to use.
57    StreamSensing,
58    /// Connection established using WebSocket polling transport.
59    WsPolling,
60    /// Connection established using WebSocket streaming transport.
61    WsStreaming,
62}
63
64/// Represents the type of disconnection that occurred with the Lightstreamer Server.
65///
66/// This enum provides information about the disconnection state and what actions
67/// the client will take following the disconnection.
68#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
69#[repr(u8)]
70pub enum DisconnectionType {
71    /// The client will automatically try to reconnect to the server.
72    WillRetry,
73    /// The client is attempting to recover the previous session.
74    /// This happens when a temporary disconnection is detected and the client
75    /// is trying to restore the previous session without losing subscriptions.
76    TryingRecovery,
77}
78
79/// Represents the type of logging to be used by the LightstreamerClient.
80///
81/// This enum determines how log messages from the client will be handled and output.
82#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
83#[repr(u8)]
84pub enum LogType {
85    /// Use standard output (stdout/stderr) for logging.
86    /// This provides simpler logging directly to the console.
87    #[default]
88    StdLogs = 0,
89    /// Use the tracing crate for logging.
90    /// This provides structured, leveled logging with spans and events.
91    TracingLogs = 1,
92}
93
94/// The transport type to be used by the client.
95/// - WS: the Stream-Sense algorithm is enabled as in the `None` case but the client will
96///   only use WebSocket based connections. If a connection over WebSocket is not possible
97///   because of the environment the client will not connect at all.
98/// - HTTP: the Stream-Sense algorithm is enabled as in the `None` case but the client
99///   will only use HTTP based connections. If a connection over HTTP is not possible because
100///   of the environment the client will not connect at all.
101/// - WS-STREAMING: the Stream-Sense algorithm is disabled and the client will only connect
102///   on Streaming over WebSocket. If Streaming over WebSocket is not possible because of
103///   the environment the client will not connect at all.
104/// - HTTP-STREAMING: the Stream-Sense algorithm is disabled and the client will only
105///   connect on Streaming over HTTP. If Streaming over HTTP is not possible because of the
106///   browser/environment the client will not connect at all.
107/// - WS-POLLING: the Stream-Sense algorithm is disabled and the client will only connect
108///   on Polling over WebSocket. If Polling over WebSocket is not possible because of the
109///   environment the client will not connect at all.
110/// - HTTP-POLLING: the Stream-Sense algorithm is disabled and the client will only connect
111///   on Polling over HTTP. If Polling over HTTP is not possible because of the environment
112///   the client will not connect at all.
113#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
114#[repr(u8)]
115pub enum Transport {
116    /// WebSocket transport with Stream-Sense algorithm enabled. The client will only use WebSocket-based connections.
117    Ws,
118    /// HTTP transport with Stream-Sense algorithm enabled. The client will only use HTTP-based connections.
119    Http,
120    /// WebSocket Streaming transport with Stream-Sense algorithm disabled. The client will only connect on Streaming over WebSocket.
121    WsStreaming,
122    /// HTTP Streaming transport with Stream-Sense algorithm disabled. The client will only connect on Streaming over HTTP.
123    HttpStreaming,
124    /// WebSocket Polling transport with Stream-Sense algorithm disabled. The client will only connect on Polling over WebSocket.
125    WsPolling,
126    /// HTTP Polling transport with Stream-Sense algorithm disabled. The client will only connect on Polling over HTTP.
127    HttpPolling,
128}