Skip to main content

hyperlane_core/config/
impl.rs

1use super::*;
2
3/// Implementation block for ServerProtocol.
4impl ServerProtocol {
5    /// Returns the ALPN protocol identifier used to negotiate this protocol.
6    ///
7    /// The value is sent verbatim in the TLS handshake, so it must be the
8    /// wire identifier and not the human-readable version string.
9    ///
10    /// # Returns
11    ///
12    /// - `&'static [u8]` - The ALPN protocol identifier bytes.
13    #[inline(always)]
14    pub fn get_alpn_protocol(&self) -> &'static [u8] {
15        match self {
16            Self::Http1 => ALPN_HTTP1,
17            Self::Http2 => ALPN_HTTP2,
18            Self::Http3 => ALPN_HTTP3,
19        }
20    }
21
22    /// Returns the HTTP version negotiated by this protocol.
23    ///
24    /// # Returns
25    ///
26    /// - `HttpVersion` - The protocol's HTTP version.
27    #[inline(always)]
28    pub fn get_version(&self) -> HttpVersion {
29        match self {
30            Self::Http1 => HttpVersion::Http1_1,
31            Self::Http2 => HttpVersion::Http2,
32            Self::Http3 => HttpVersion::Http3,
33        }
34    }
35
36    /// Returns `true` when this protocol runs over QUIC rather than TCP.
37    ///
38    /// # Returns
39    ///
40    /// - `bool` - `true` for QUIC transports, `false` for TCP transports.
41    #[inline(always)]
42    pub fn is_quic(&self) -> bool {
43        matches!(self, Self::Http3)
44    }
45}
46
47/// Provides a default implementation for ServerConfig.
48impl Default for ServerConfig {
49    /// Creates a new ServerConfig instance with default values.
50    ///
51    /// # Returns
52    ///
53    /// - `Self` - A new instance with default configuration.
54    #[inline(always)]
55    fn default() -> Self {
56        Self {
57            address: Server::format_bind_address(DEFAULT_HOST, DEFAULT_WEB_PORT),
58            nodelay: DEFAULT_NODELAY,
59            ttl: DEFAULT_TTI,
60            protocol: ServerProtocol::default(),
61            tls: None,
62        }
63    }
64}
65
66/// Implementation block for ServerConfig.
67impl ServerConfig {
68    /// Creates a ServerConfig from a JSON string.
69    ///
70    /// # Arguments
71    ///
72    /// - `C` - The configuration.
73    ///
74    /// # Returns
75    ///
76    /// - `Result<Self, serde_json::Error>` - A `Result` containing either the `ServerConfig` or a `serde_json::Error`.
77    pub fn from_json<C>(json: C) -> Result<Self, serde_json::Error>
78    where
79        C: AsRef<str>,
80    {
81        serde_json::from_str(json.as_ref())
82    }
83
84    /// Returns the negotiated ALPN protocol identifier for this configuration.
85    ///
86    /// # Returns
87    ///
88    /// - `&'static [u8]` - The ALPN identifier of the configured protocol.
89    #[inline(always)]
90    pub fn get_alpn_protocol(&self) -> &'static [u8] {
91        self.get_protocol().get_alpn_protocol()
92    }
93}