Skip to main content

sark_http/payload/response/
declared.rs

1use std::marker::PhantomData;
2
3use super::{
4    dispatch, headers,
5    status::{self, Code},
6};
7use crate::{h1, payload};
8
9/// An input-independent response whose fields and body are admitted constants.
10pub trait Spec: headers::Fields {
11    const STATUS: Code;
12    const BODY: &'static [u8];
13    const HEAD: Head<Reply<Self>>;
14}
15
16pub struct Reply<D>(PhantomData<fn() -> D>);
17
18impl<D: Spec> Reply<D> {
19    pub const VALUE: Self = Self(PhantomData);
20}
21
22impl<S: Spec> status::Constant for Reply<S> {
23    const CODE: Code = S::STATUS;
24}
25
26impl<'req, 'd, Ranges: Copy + 'd, D, S: Spec> dispatch::Emit<'req, 'd, D, Ranges> for Reply<S> {
27    type Kind = dispatch::Sync;
28    type Stream = dispatch::Never;
29    type Bound = o3::buffer::resident::bound::Unbounded;
30    const BODY: payload::Kind = payload::Kind::Inline;
31    const DECLARED: Option<Head<Self>> = Some(S::HEAD);
32
33    fn consume<W: dispatch::Transaction<'req, 'd, dispatch::Never, Ranges>>(
34        self,
35        writer: W,
36    ) -> W::Done {
37        writer.fixed::<Self, _, _>((), PhantomData::<S>, payload::fixed::Body::new(S::BODY))
38    }
39}
40
41/// Immutable HTTP/1 framing for the exact declared reply.
42pub struct Head<R> {
43    status: Code,
44    prefix: &'static [u8],
45    body: &'static [u8],
46    reply: PhantomData<fn() -> R>,
47}
48
49impl<D: Spec> Head<Reply<D>> {
50    const LENGTH: ([u8; 20], usize) = h1::codec::numbers::decimal_length(D::BODY.len());
51    const PARTS: [&'static [u8]; 9] = [
52        b" ",
53        &D::STATUS.as_bytes(),
54        b" ",
55        match D::STATUS.reason() {
56            Some(reason) => reason.as_bytes(),
57            None => b"",
58        },
59        b"\r\n",
60        D::FIELDS.wire(),
61        h1::CONTENT_LENGTH,
62        Self::LENGTH.0.split_at(Self::LENGTH.1).1,
63        b"\r\n",
64    ];
65    pub const LEN: usize = {
66        let mut len = 0;
67        let mut i = 0;
68        while i < Self::PARTS.len() {
69            len += Self::PARTS[i].len();
70            i += 1;
71        }
72        len
73    };
74
75    pub const fn new<const N: usize>() -> Self {
76        let bytes = &const {
77            assert!(N == Self::LEN, "length");
78            let mut bytes = [0; N];
79            let mut rest = bytes.as_mut_slice();
80            let mut i = 0;
81            while i < Self::PARTS.len() {
82                let part = Self::PARTS[i];
83                let (dst, next) = rest.split_at_mut(part.len());
84                dst.copy_from_slice(part);
85                rest = next;
86                i += 1;
87            }
88            bytes
89        };
90        Self {
91            status: D::STATUS,
92            prefix: bytes,
93            body: D::BODY,
94            reply: PhantomData,
95        }
96    }
97}
98
99impl<R> Head<R> {
100    pub fn parts(&self) -> (Code, &'static [u8], &'static [u8]) {
101        (self.status, self.prefix, self.body)
102    }
103}