sark_http/payload/response/dispatch/
mod.rs1use o3::{base::Either, buffer::resident::bound};
2
3use crate::{
4 field, payload,
5 payload::{
6 fixed::Body,
7 response::{self, declared, encoded, headers, status::Code},
8 },
9};
10
11pub mod chunk;
12
13pub enum Sync {}
14pub enum Polled {}
15#[derive(Debug)]
16pub enum Never {}
17
18pub trait Transaction<'req, 'd>: Sized {
19 type Stream;
20 type Done;
21
22 fn fixed<H, B>(self, status: Code, fields: H, body: B) -> Self::Done
23 where
24 H: field::Source<headers::Application>,
25 B: response::Body<'d>;
26
27 fn statics<'body: 'd, H>(self, status: Code, fields: H, body: Body<'body>) -> Self::Done
28 where
29 H: field::Source<headers::Application>;
30
31 fn sized_static<'body: 'd, H, const N: usize>(
32 self,
33 status: Code,
34 fields: H,
35 body: &'body [u8; N],
36 ) -> Self::Done
37 where
38 H: field::Source<headers::Application>,
39 {
40 self.statics(status, fields, Body::new(body))
41 }
42
43 fn encoded<const EXACT: bool>(
44 self,
45 status: Code,
46 fields: impl field::Source<headers::Application>,
47 body: impl encoded::Source<EXACT>,
48 ) -> Self::Done;
49
50 fn stream(
51 self,
52 status: Code,
53 fields: impl field::Source<headers::Application>,
54 source: Option<Self::Stream>,
55 ) -> Self::Done;
56}
57
58pub trait Response<'req, 'd, D = ()>: Sized {
59 type Kind;
60 type Stream;
61 type Bound: bound::Bound + 'd;
62
63 const BODY: payload::Kind;
64 const DECLARED: Option<declared::Head<Self>> = None;
65
66 fn consume<W: Transaction<'req, 'd, Stream = Self::Stream>>(self, writer: W) -> W::Done;
67}
68
69pub trait Success {
70 const STATUS: Code;
71 const FIELDS: headers::Group<'static>;
72}
73
74impl<'req, 'd, D: Success, B: response::Body<'d>> Response<'req, 'd, D> for B {
75 type Kind = Sync;
76 type Stream = Never;
77 type Bound = <B as response::Body<'d>>::Bound;
78
79 const BODY: payload::Kind = payload::Kind::Inline;
80
81 fn consume<W: Transaction<'req, 'd, Stream = Never>>(self, writer: W) -> W::Done {
82 writer.fixed(D::STATUS, D::FIELDS, self)
83 }
84}
85
86impl<'req, 'd, D: Success> Response<'req, 'd, D> for () {
87 type Kind = Sync;
88 type Stream = Never;
89 type Bound = bound::AtMost<0>;
90
91 const BODY: payload::Kind = payload::Kind::Inline;
92
93 fn consume<W: Transaction<'req, 'd, Stream = Never>>(self, writer: W) -> W::Done {
94 writer.statics(D::STATUS, D::FIELDS, Body::new(b""))
95 }
96}
97
98impl<'req, 'd, D, L, R> Response<'req, 'd, D> for Either<L, R>
99where
100 L: Response<'req, 'd, D, Kind = Sync, Stream = Never>,
101 R: Response<'req, 'd, D, Kind = Sync, Stream = Never>,
102{
103 type Kind = Sync;
104 type Stream = Never;
105 type Bound = bound::Max<L::Bound, R::Bound>;
106
107 const BODY: payload::Kind = payload::Kind::Inline;
108
109 fn consume<W: Transaction<'req, 'd, Stream = Never>>(self, writer: W) -> W::Done {
110 match self {
111 Self::Left(value) => <L as Response<'req, 'd, D>>::consume(value, writer),
112 Self::Right(value) => <R as Response<'req, 'd, D>>::consume(value, writer),
113 }
114 }
115}
116
117impl<'req, 'd, D, T, E> Response<'req, 'd, D> for Result<T, E>
118where
119 T: Response<'req, 'd, D, Kind = Sync, Stream = Never>,
120 E: Response<'req, 'd, (), Kind = Sync, Stream = Never>,
121{
122 type Kind = Sync;
123 type Stream = Never;
124 type Bound = bound::Max<T::Bound, E::Bound>;
125
126 const BODY: payload::Kind = payload::Kind::Inline;
127
128 fn consume<W: Transaction<'req, 'd, Stream = Never>>(self, writer: W) -> W::Done {
129 match self {
130 Ok(value) => <T as Response<'req, 'd, D>>::consume(value, writer),
131 Err(error) => <E as Response<'req, 'd, ()>>::consume(error, writer),
132 }
133 }
134}
135
136pub struct Stream<S, H = ()> {
137 status: Code,
138 headers: H,
139 stream: Option<S>,
140}
141
142impl<S, H> Stream<S, H> {
143 pub fn with_status(mut self, status: Code) -> Self {
144 self.status = status;
145 self
146 }
147}
148
149impl<S> Stream<S> {
150 pub fn new(stream: Option<S>) -> Self {
151 Self {
152 status: Code::OK,
153 headers: (),
154 stream,
155 }
156 }
157
158 pub fn headers<H: field::Source<headers::Application>>(self, headers: H) -> Stream<S, H> {
159 Stream {
160 status: self.status,
161 headers,
162 stream: self.stream,
163 }
164 }
165}
166
167impl<'req, 'd, D, S, H: field::Source<headers::Application>> Response<'req, 'd, D>
168 for Stream<S, H>
169{
170 type Kind = Polled;
171 type Stream = S;
172 type Bound = bound::Unbounded;
173
174 const BODY: payload::Kind = payload::Kind::Stream;
175
176 fn consume<W: Transaction<'req, 'd, Stream = S>>(self, writer: W) -> W::Done {
177 writer.stream(self.status, self.headers, self.stream)
178 }
179}