asport/model/
server_hello.rs

1use std::fmt::{Debug, Formatter, Result as FmtResult};
2
3use crate::{Header, ServerHello as ServerHelloHeader};
4
5use super::side::{self, Side};
6
7/// The model of the `ServerHello` command
8pub struct ServerHello<M> {
9    inner: Side<Tx, Rx>,
10    _marker: M,
11}
12
13struct Tx {
14    header: Header,
15}
16
17impl ServerHello<side::Tx> {
18    pub(super) fn new(server_hello: ServerHelloHeader) -> Self {
19        Self {
20            inner: Side::Tx(Tx {
21                header: Header::ServerHello(server_hello),
22            }),
23            _marker: side::Tx,
24        }
25    }
26
27    /// Returns the header of the `ServerHello` command
28    pub fn header(&self) -> &Header {
29        let Side::Tx(tx) = &self.inner else {
30            unreachable!()
31        };
32        &tx.header
33    }
34}
35
36struct Rx {
37    handshake_code: u8,
38    port: Option<u16>,
39}
40
41impl ServerHello<side::Rx> {
42    pub(super) fn new(handshake_code: u8, port: Option<u16>) -> Self {
43        Self {
44            inner: Side::Rx(Rx {
45                handshake_code,
46                port,
47            }),
48            _marker: side::Rx,
49        }
50    }
51
52    /// Returns the handshake code of the `ServerHello` command
53    pub fn handshake_code(&self) -> u8 {
54        let Side::Rx(rx) = &self.inner else {
55            unreachable!()
56        };
57        rx.handshake_code
58    }
59
60    /// Returns the port of the `ServerHello` command
61    pub fn port(&self) -> Option<u16> {
62        let Side::Rx(rx) = &self.inner else {
63            unreachable!()
64        };
65        rx.port
66    }
67}
68
69impl Debug for ServerHello<side::Rx> {
70    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
71        let Side::Rx(rx) = &self.inner else {
72            unreachable!()
73        };
74        f.debug_struct("ServerHello")
75            .field("handshake_code", &rx.handshake_code)
76            .field("port", &rx.port)
77            .finish()
78    }
79}