net_wire/http3/frame/
headers.rs1use super::envelope::{Http3Frame, Http3FrameBuildError, Http3FrameBuilder};
4use super::payload::{Http3FramePayloadParseError, validate_type};
5use crate::http3::codepoints::Http3FrameType;
6
7#[derive(Clone, Copy, Debug, Eq, PartialEq)]
9pub struct Http3Headers<'a> {
10 frame: Http3Frame<'a>,
11}
12
13impl<'a> Http3Headers<'a> {
14 pub(in crate::http3) const fn from_validated(frame: Http3Frame<'a>) -> Self {
16 Self { frame }
17 }
18
19 pub fn parse(
21 bytes: &'a [u8],
22 maximum_payload: usize,
23 ) -> Result<Self, Http3FramePayloadParseError> {
24 Self::from_frame(
25 Http3Frame::parse(bytes, maximum_payload)
26 .map_err(Http3FramePayloadParseError::Frame)?,
27 )
28 }
29
30 pub fn from_frame(frame: Http3Frame<'a>) -> Result<Self, Http3FramePayloadParseError> {
32 validate_type(frame, Http3FrameType::HEADERS)?;
33 Ok(Self { frame })
34 }
35
36 pub const fn frame(&self) -> Http3Frame<'a> {
38 self.frame
39 }
40
41 pub const fn as_bytes(&self) -> &'a [u8] {
43 self.frame.as_bytes()
44 }
45
46 pub fn encoded_field_section(&self) -> &'a [u8] {
48 self.frame.payload()
49 }
50}
51
52pub struct Http3HeadersBuilder<'output, 'section> {
54 destination: &'output mut [u8],
55 encoded_field_section: &'section [u8],
56}
57
58impl<'output, 'section> Http3HeadersBuilder<'output, 'section> {
59 pub fn new(destination: &'output mut [u8], encoded_field_section: &'section [u8]) -> Self {
61 Self {
62 destination,
63 encoded_field_section,
64 }
65 }
66
67 pub fn build(self) -> Result<Http3Headers<'output>, Http3FrameBuildError> {
69 let frame = Http3FrameBuilder::new(
70 self.destination,
71 Http3FrameType::HEADERS,
72 self.encoded_field_section,
73 )
74 .build()?;
75 Ok(Http3Headers::from_validated(frame))
76 }
77}