1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use super::{CompressionLevel, CompressionType, EncryptionType};

/// Definition of the handshake to perform for a transport
#[derive(Clone, Debug)]
pub enum Handshake {
    /// Indicates that the handshake is being performed from the client-side
    Client {
        /// Preferred compression algorithm when presented options by server
        preferred_compression_type: Option<CompressionType>,

        /// Preferred compression level when presented options by server
        preferred_compression_level: Option<CompressionLevel>,

        /// Preferred encryption algorithm when presented options by server
        preferred_encryption_type: Option<EncryptionType>,
    },

    /// Indicates that the handshake is being performed from the server-side
    Server {
        /// List of available compression algorithms for use between client and server
        compression_types: Vec<CompressionType>,

        /// List of available encryption algorithms for use between client and server
        encryption_types: Vec<EncryptionType>,
    },
}

impl Handshake {
    /// Creates a new client handshake definition, providing defaults for the preferred compression
    /// type, compression level, and encryption type
    pub fn client() -> Self {
        Self::Client {
            preferred_compression_type: None,
            preferred_compression_level: None,
            preferred_encryption_type: Some(EncryptionType::XChaCha20Poly1305),
        }
    }

    /// Creates a new server handshake definition, providing defaults for the compression types and
    /// encryption types by including all known variants
    pub fn server() -> Self {
        Self::Server {
            compression_types: CompressionType::known_variants().to_vec(),
            encryption_types: EncryptionType::known_variants().to_vec(),
        }
    }

    /// Returns true if handshake is from client-side
    pub fn is_client(&self) -> bool {
        matches!(self, Self::Client { .. })
    }

    /// Returns true if handshake is from server-side
    pub fn is_server(&self) -> bool {
        matches!(self, Self::Server { .. })
    }
}