gui/network/connection_state.rs
1//! # Connection State Module
2//!
3//! This module defines the core data structures used to track the GUI application's
4//! network connection status and user authentication session state.
5
6use std::fmt;
7use std::fmt::{Debug, Display, Formatter};
8
9/// Describes the physical network connection state to the kernel server.
10#[derive(PartialEq, Debug, Clone)]
11pub enum ConnectionState {
12 /// Active network connection to the server, carrying a nested [`SessionState`].
13 Connected {
14 /// The active user authentication session status.
15 session_state: SessionState,
16 },
17 /// No network connection is active.
18 Disconnected,
19 /// Connection attempt failed or an error occurred on the TCP socket.
20 Error,
21 /// Connection attempt is currently in progress (e.g. TCP handshake ongoing).
22 ConnectionPending,
23}
24
25/// Formats [`ConnectionState`] for human-readable display.
26///
27/// The output delegates to [`SessionState`]'s [`Display`] implementation when the
28/// connection is active, so the full state reads e.g. `"Connected and Log in"`.
29impl Display for ConnectionState {
30 /// Writes a human-readable representation of the connection state to `f`.
31 ///
32 /// # Variants
33 /// - `Connected { session_state }` → `"Connected and <session_state>"`
34 /// - `Disconnected` → `"Disconnected"`
35 /// - `Error` → `"Error"`
36 /// - `ConnectionPending` → `"Connection Pending"`
37 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
38 match self {
39 ConnectionState::Connected { session_state } => write!(f, "Connected and {}", session_state),
40 ConnectionState::Disconnected => write!(f, "Disconnected"),
41 ConnectionState::Error => write!(f, "Error"),
42 ConnectionState::ConnectionPending => write!(f, "Connection Pending")
43 }
44 }
45}
46
47/// Describes the user authentication session state within an active connection.
48#[derive(PartialEq, Debug, Clone)]
49pub enum SessionState {
50 /// Authentication request has been sent to the server, awaiting response.
51 LoginPending,
52 /// Connected to the server, but no user is currently authenticated.
53 LoggedOut,
54 /// Successfully authenticated and logged in to the server.
55 LoggedIn,
56 /// Authentication attempt failed, carrying the failure reason string provided by the server.
57 LoginFailed(String),
58}
59
60/// Formats [`SessionState`] for human-readable display.
61///
62/// Used by [`ConnectionState`]'s [`Display`] implementation to compose the full
63/// connection status string, and anywhere a `SessionState` is shown in the UI.
64impl Display for SessionState {
65 /// Writes a human-readable representation of the session state to `f`.
66 ///
67 /// # Variants
68 /// - `LoginPending` → `"Login"`
69 /// - `LoggedOut` → `"Logout"`
70 /// - `LoggedIn` → `"Log in"`
71 /// - `LoginFailed(msg)` → `"LoginFailed(<msg>)"` where `msg` is the server-supplied error
72 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
73 match self {
74 SessionState::LoginPending => write!(f, "Login"),
75 SessionState::LoggedOut => write!(f, "Logout"),
76 SessionState::LoggedIn => write!(f, "Logged in"),
77 SessionState::LoginFailed(msg) => write!(f, "LoginFailed({})", msg),
78 }
79 }
80}