Skip to main content

net_wire/http3/frame/
data.rs

1//! Typed HTTP/3 DATA frame payload view and builder.
2
3use super::envelope::{Http3Frame, Http3FrameBuildError, Http3FrameBuilder};
4use super::payload::{Http3FramePayloadParseError, validate_type};
5use crate::http3::codepoints::Http3FrameType;
6
7/// A validated borrowed HTTP/3 DATA frame.
8#[derive(Clone, Copy, Debug, Eq, PartialEq)]
9pub struct Http3Data<'a> {
10    frame: Http3Frame<'a>,
11}
12
13impl<'a> Http3Data<'a> {
14    /// Assembles a DATA view from a builder-validated frame.
15    pub(in crate::http3) const fn from_validated(frame: Http3Frame<'a>) -> Self {
16        Self { frame }
17    }
18
19    /// Parses the first complete DATA frame, enforcing the caller-provided payload maximum.
20    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    /// Validates a raw DATA frame's intrinsic layout.
31    pub fn from_frame(frame: Http3Frame<'a>) -> Result<Self, Http3FramePayloadParseError> {
32        validate_type(frame, Http3FrameType::DATA)?;
33        Ok(Self { frame })
34    }
35
36    /// Returns the validated raw frame.
37    pub const fn frame(&self) -> Http3Frame<'a> {
38        self.frame
39    }
40
41    /// Returns the exact represented frame bytes.
42    pub const fn as_bytes(&self) -> &'a [u8] {
43        self.frame.as_bytes()
44    }
45
46    /// Returns the complete opaque DATA payload.
47    pub fn data(&self) -> &'a [u8] {
48        self.frame.payload()
49    }
50}
51
52/// Builds a typed HTTP/3 DATA frame in caller-owned storage.
53pub struct Http3DataBuilder<'output, 'payload> {
54    destination: &'output mut [u8],
55    data: &'payload [u8],
56}
57
58impl<'output, 'payload> Http3DataBuilder<'output, 'payload> {
59    /// Creates a DATA builder for opaque payload bytes.
60    pub fn new(destination: &'output mut [u8], data: &'payload [u8]) -> Self {
61        Self { destination, data }
62    }
63
64    /// Canonically encodes and atomically writes the DATA frame.
65    pub fn build(self) -> Result<Http3Data<'output>, Http3FrameBuildError> {
66        let frame =
67            Http3FrameBuilder::new(self.destination, Http3FrameType::DATA, self.data).build()?;
68        Ok(Http3Data::from_validated(frame))
69    }
70}