Skip to main content

sark_http/payload/response/dispatch/
mod.rs

1use std::{convert::Infallible, marker::PhantomData};
2
3use o3::{base::Either, buffer::resident::bound};
4
5use crate::{
6    field, payload,
7    payload::{
8        fixed::Body,
9        response::{
10            self, declared, encoded, exact, headers,
11            status::{self, Code},
12        },
13    },
14};
15
16pub mod chunk;
17mod counter;
18pub(crate) use counter::Counter;
19
20pub enum Sync {}
21pub enum Polled<const KNOWN: bool = false> {}
22#[derive(Debug)]
23pub enum Never {}
24
25#[doc(hidden)]
26pub trait Extent<K>: Counter<K> {
27    type Count: Copy + Default;
28    fn remaining(_: &mut Self::Count) -> Option<&mut u64> {
29        None
30    }
31}
32impl<S> Extent<Sync> for S {
33    type Count = ();
34}
35impl<S> Extent<Polled> for S {
36    type Count = ();
37}
38impl<S> Extent<Polled<true>> for S {
39    type Count = u64;
40
41    fn remaining(count: &mut u64) -> Option<&mut u64> {
42        Some(count)
43    }
44}
45
46pub trait Source<Kind = Polled<true>>: Extent<Kind> {
47    fn length(&self) -> u64;
48}
49
50impl<S> Source<Polled> for S {
51    fn length(&self) -> u64 {
52        0
53    }
54}
55
56pub trait Transaction<'req, 'd, S, Ranges: Copy + 'd = (), Kind = Sync>: Sized {
57    type Done;
58
59    fn fixed<Status: status::Source, H, B>(
60        self,
61        status: Status::Value,
62        fields: H,
63        body: B,
64    ) -> Self::Done
65    where
66        H: field::Source<headers::Application>,
67        B: response::Body<'d, Ranges>;
68
69    fn statics<'body: 'd, Status: status::Source, H>(
70        self,
71        status: Status::Value,
72        fields: H,
73        body: Body<'body>,
74    ) -> Self::Done
75    where
76        H: field::Source<headers::Application>,
77    {
78        self.fixed::<Status, _, _>(status, fields, body)
79    }
80
81    fn sized_static<'body: 'd, Status: status::Source, H, const N: usize>(
82        self,
83        status: Status::Value,
84        fields: H,
85        body: &'body [u8; N],
86    ) -> Self::Done
87    where
88        H: field::Source<headers::Application>,
89    {
90        self.statics::<Status, _>(status, fields, Body::new(body))
91    }
92
93    fn encoded<const EXACT: bool, Status: status::Source>(
94        self,
95        status: Status::Value,
96        fields: impl field::Source<headers::Application>,
97        body: impl encoded::Source<EXACT>,
98    ) -> Self::Done;
99
100    fn stream<Status: status::Source>(
101        self,
102        status: Status::Value,
103        fields: impl field::Source<headers::Application>,
104        source: Option<S>,
105    ) -> Self::Done
106    where
107        S: Source<Kind>;
108}
109
110pub trait Emit<'req, 'd, D = (), Ranges: Copy + 'd = ()>: Sized {
111    type Kind;
112    type Stream: Extent<Self::Kind>;
113    type Bound: bound::Bound + 'd;
114
115    const BODY: payload::Kind;
116    const DECLARED: Option<declared::Head<Self>> = None;
117
118    fn into_exact(self) -> Result<impl exact::Reply, Self> {
119        Err::<(Code, (), Infallible), _>(self)
120    }
121
122    fn consume<W: Transaction<'req, 'd, Self::Stream, Ranges, Self::Kind>>(
123        self,
124        writer: W,
125    ) -> W::Done;
126}
127
128pub trait Success: headers::Fields {
129    const STATUS: Code;
130}
131
132impl<'req, 'd, Ranges: Copy + 'd, D: Success, B: response::Body<'d, Ranges>>
133    Emit<'req, 'd, D, Ranges> for B
134{
135    type Kind = Sync;
136    type Stream = Never;
137    type Bound = <B as response::Body<'d, Ranges>>::Bound;
138
139    const BODY: payload::Kind = payload::Kind::Inline;
140
141    fn consume<W: Transaction<'req, 'd, Never, Ranges>>(self, writer: W) -> W::Done {
142        writer.fixed::<D, _, _>((), PhantomData::<D>, self)
143    }
144}
145
146impl<'req, 'd, Ranges: Copy + 'd, D: Success> Emit<'req, 'd, D, Ranges> for () {
147    type Kind = Sync;
148    type Stream = Never;
149    type Bound = bound::AtMost<0>;
150
151    const BODY: payload::Kind = payload::Kind::Inline;
152
153    fn consume<W: Transaction<'req, 'd, Never, Ranges>>(self, writer: W) -> W::Done {
154        writer.statics::<D, _>((), PhantomData::<D>, Body::new(b""))
155    }
156}
157
158impl<'req, 'd, Ranges: Copy + 'd, D, L, R> Emit<'req, 'd, D, Ranges> for Either<L, R>
159where
160    L: Emit<'req, 'd, D, Ranges, Kind = Sync, Stream = Never>,
161    R: Emit<'req, 'd, D, Ranges, Kind = Sync, Stream = Never>,
162{
163    type Kind = Sync;
164    type Stream = Never;
165    type Bound = bound::Max<L::Bound, R::Bound>;
166
167    const BODY: payload::Kind = payload::Kind::Inline;
168
169    fn consume<W: Transaction<'req, 'd, Never, Ranges>>(self, writer: W) -> W::Done {
170        match self {
171            Self::Left(value) => <L as Emit<'req, 'd, D, Ranges>>::consume(value, writer),
172            Self::Right(value) => <R as Emit<'req, 'd, D, Ranges>>::consume(value, writer),
173        }
174    }
175}
176
177impl<'req, 'd, Ranges: Copy + 'd, D, T, E> Emit<'req, 'd, D, Ranges> for Result<T, E>
178where
179    T: Emit<'req, 'd, D, Ranges, Kind = Sync, Stream = Never>,
180    E: Emit<'req, 'd, (), Ranges, Kind = Sync, Stream = Never>,
181{
182    type Kind = Sync;
183    type Stream = Never;
184    type Bound = bound::Max<T::Bound, E::Bound>;
185
186    const BODY: payload::Kind = payload::Kind::Inline;
187
188    fn into_exact(self) -> Result<impl exact::Reply, Self> {
189        match self {
190            Ok(value) => <T as Emit<'req, 'd, D, Ranges>>::into_exact(value).map_err(Ok),
191            Err(error) => Err(Err(error)),
192        }
193    }
194
195    fn consume<W: Transaction<'req, 'd, Never, Ranges>>(self, writer: W) -> W::Done {
196        match self {
197            Ok(value) => <T as Emit<'req, 'd, D, Ranges>>::consume(value, writer),
198            Err(error) => <E as Emit<'req, 'd, (), Ranges>>::consume(error, writer),
199        }
200    }
201}
202
203pub struct Stream<S, H = (), const KNOWN: bool = false> {
204    status: Code,
205    headers: H,
206    stream: Option<S>,
207}
208
209impl<S, H, const KNOWN: bool> Stream<S, H, KNOWN> {
210    pub fn with_status(mut self, status: Code) -> Self {
211        self.status = status;
212        self
213    }
214}
215
216impl<S> Stream<S> {
217    pub fn new(stream: Option<S>) -> Self {
218        Self {
219            status: Code::OK,
220            headers: (),
221            stream,
222        }
223    }
224
225    pub fn known(stream: Option<S>) -> Stream<S, (), true>
226    where
227        S: Source,
228    {
229        Stream {
230            status: Code::OK,
231            headers: (),
232            stream,
233        }
234    }
235}
236
237impl<S, const KNOWN: bool> Stream<S, (), KNOWN> {
238    pub fn headers<H: field::Source<headers::Application>>(
239        self,
240        headers: H,
241    ) -> Stream<S, H, KNOWN> {
242        Stream {
243            status: self.status,
244            headers,
245            stream: self.stream,
246        }
247    }
248}
249
250impl<
251    'req,
252    'd,
253    Ranges: Copy + 'd,
254    D,
255    S: Source<Polled<KNOWN>>,
256    H: field::Source<headers::Application>,
257    const KNOWN: bool,
258> Emit<'req, 'd, D, Ranges> for Stream<S, H, KNOWN>
259{
260    type Kind = Polled<KNOWN>;
261    type Stream = S;
262    type Bound = bound::Unbounded;
263
264    const BODY: payload::Kind = payload::Kind::Stream;
265
266    fn consume<W: Transaction<'req, 'd, S, Ranges, Self::Kind>>(self, writer: W) -> W::Done {
267        writer.stream::<Code>(self.status, self.headers, self.stream)
268    }
269}
270
271impl<'req, 'd, Ranges: Copy + 'd, D> Emit<'req, 'd, D, Ranges> for Infallible {
272    type Kind = Sync;
273    type Stream = Never;
274    type Bound = bound::AtMost<0>;
275
276    const BODY: payload::Kind = payload::Kind::Inline;
277
278    fn consume<W: Transaction<'req, 'd, Never, Ranges>>(self, _: W) -> W::Done {
279        match self {}
280    }
281}