h3/
ext.rs

1//! Extensions for the HTTP/3 protocol.
2
3use std::str::FromStr;
4
5/// Describes the `:protocol` pseudo-header for extended connect
6///
7/// See: <https://www.rfc-editor.org/rfc/rfc8441#section-4>
8#[derive(Copy, PartialEq, Debug, Clone)]
9pub struct Protocol(ProtocolInner);
10
11impl Protocol {
12    /// WebTransport protocol
13    pub const WEB_TRANSPORT: Protocol = Protocol(ProtocolInner::WebTransport);
14    /// RFC 9298 protocol
15    pub const CONNECT_UDP: Protocol = Protocol(ProtocolInner::ConnectUdp);
16
17    /// Return a &str representation of the `:protocol` pseudo-header value
18    #[inline]
19    pub fn as_str(&self) -> &str {
20        match self.0 {
21            ProtocolInner::WebTransport => "webtransport",
22            ProtocolInner::ConnectUdp => "connect-udp",
23        }
24    }
25}
26
27#[derive(Copy, PartialEq, Debug, Clone)]
28enum ProtocolInner {
29    WebTransport,
30    ConnectUdp,
31}
32
33/// Error when parsing the protocol
34pub struct InvalidProtocol;
35
36impl FromStr for Protocol {
37    type Err = InvalidProtocol;
38
39    fn from_str(s: &str) -> Result<Self, Self::Err> {
40        match s {
41            "webtransport" => Ok(Self(ProtocolInner::WebTransport)),
42            "connect-udp" => Ok(Self(ProtocolInner::ConnectUdp)),
43            _ => Err(InvalidProtocol),
44        }
45    }
46}