h2/
ext.rs

1//! Extensions specific to the HTTP/2 protocol.
2
3use crate::hpack::BytesStr;
4
5use bytes::Bytes;
6use std::fmt;
7
8/// Represents the `:protocol` pseudo-header used by
9/// the [Extended CONNECT Protocol].
10///
11/// [Extended CONNECT Protocol]: https://datatracker.ietf.org/doc/html/rfc8441#section-4
12#[derive(Clone, Eq, PartialEq)]
13pub struct Protocol {
14    value: BytesStr,
15}
16
17impl Protocol {
18    /// Converts a static string to a protocol name.
19    pub const fn from_static(value: &'static str) -> Self {
20        Self {
21            value: BytesStr::from_static(value),
22        }
23    }
24
25    /// Returns a str representation of the header.
26    pub fn as_str(&self) -> &str {
27        self.value.as_str()
28    }
29
30    pub(crate) fn try_from(bytes: Bytes) -> Result<Self, std::str::Utf8Error> {
31        Ok(Self {
32            value: BytesStr::try_from(bytes)?,
33        })
34    }
35}
36
37impl<'a> From<&'a str> for Protocol {
38    fn from(value: &'a str) -> Self {
39        Self {
40            value: BytesStr::from(value),
41        }
42    }
43}
44
45impl AsRef<[u8]> for Protocol {
46    fn as_ref(&self) -> &[u8] {
47        self.value.as_ref()
48    }
49}
50
51impl fmt::Debug for Protocol {
52    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53        self.value.fmt(f)
54    }
55}