Skip to main content

openvpn_mgmt_codec/
client_event.rs

1use std::fmt;
2
3/// The sub-type of a `>CLIENT:` notification.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum ClientEvent {
6    /// A new client is connecting (`>CLIENT:CONNECT`).
7    Connect,
8
9    /// An existing client is re-authenticating (`>CLIENT:REAUTH`).
10    Reauth,
11
12    /// A client connection has been fully established (`>CLIENT:ESTABLISHED`).
13    Established,
14
15    /// A client has disconnected (`>CLIENT:DISCONNECT`).
16    Disconnect,
17
18    /// An unrecognized event type (forward compatibility).
19    Custom(String),
20}
21
22impl ClientEvent {
23    /// Parse a wire event string into a typed variant.
24    pub(crate) fn parse(s: &str) -> Self {
25        match s {
26            "CONNECT" => Self::Connect,
27            "REAUTH" => Self::Reauth,
28            "ESTABLISHED" => Self::Established,
29            "DISCONNECT" => Self::Disconnect,
30            other => Self::Custom(other.to_string()),
31        }
32    }
33}
34
35impl fmt::Display for ClientEvent {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        match self {
38            Self::Connect => f.write_str("CONNECT"),
39            Self::Reauth => f.write_str("REAUTH"),
40            Self::Established => f.write_str("ESTABLISHED"),
41            Self::Disconnect => f.write_str("DISCONNECT"),
42            Self::Custom(s) => f.write_str(s),
43        }
44    }
45}