sark_http/payload/response/dispatch/
mod.rs1use o3::buffer::resident::bound;
2
3use crate::{
4 field, payload,
5 payload::{
6 fixed::Body,
7 response::{
8 self, encoded,
9 headers::{self, set},
10 status::Code,
11 },
12 },
13};
14
15pub mod chunk;
16
17pub enum Sync {}
18pub enum Polled {}
19pub enum Never {}
20
21pub trait Dispatch {
22 type Output<'d, E: Sink<'d>, S>;
23
24 fn fold<'d, E: Sink<'d>, S, T>(
25 output: Self::Output<'d, E, S>,
26 sync: impl FnOnce(E::Output) -> T,
27 stream: impl FnOnce(E::Stream<S>) -> T,
28 ) -> T;
29}
30
31impl Dispatch for Sync {
32 type Output<'d, E: Sink<'d>, S> = E::Output;
33
34 fn fold<'d, E: Sink<'d>, S, T>(
35 output: Self::Output<'d, E, S>,
36 sync: impl FnOnce(E::Output) -> T,
37 _: impl FnOnce(E::Stream<S>) -> T,
38 ) -> T {
39 sync(output)
40 }
41}
42
43impl Dispatch for Polled {
44 type Output<'d, E: Sink<'d>, S> = E::Stream<S>;
45
46 fn fold<'d, E: Sink<'d>, S, T>(
47 output: Self::Output<'d, E, S>,
48 _: impl FnOnce(E::Output) -> T,
49 stream: impl FnOnce(E::Stream<S>) -> T,
50 ) -> T {
51 stream(output)
52 }
53}
54
55pub enum Either<L, R> {
57 Left(L),
58 Right(R),
59}
60
61pub trait Accept<'d>: Sized {
63 type Output;
64 type Error;
65
66 fn fixed<H, B>(self, status: Code, headers: H, body: B) -> Result<Self::Output, Self::Error>
67 where
68 H: field::Source<headers::Application>,
69 B: response::Body<'d>;
70
71 fn statics<'body, H>(
72 self,
73 status: Code,
74 headers: H,
75 body: Body<'body>,
76 ) -> Result<Self::Output, Self::Error>
77 where
78 H: field::Source<headers::Application>,
79 'body: 'd,
80 {
81 self.fixed(status, headers, body)
82 }
83
84 fn encoded<const EXACT: bool>(
85 self,
86 status: Code,
87 headers: impl field::Source<headers::Application>,
88 body: impl encoded::Source<EXACT>,
89 ) -> Result<Self::Output, Self::Error>;
90}
91
92pub trait Sink<'d>: Accept<'d> {
94 type Stream<S>;
95
96 fn stream<H, S>(
97 self,
98 status: Code,
99 headers: H,
100 stream: Option<S>,
101 ) -> Result<Self::Stream<S>, Self::Error>
102 where
103 H: field::Source<headers::Application>;
104}
105
106pub trait Emit<'req, 'd>: Sized {
107 type Kind: Dispatch;
108 type Stream;
109 type Bound: bound::Bound + 'd;
110
111 const BODY: payload::Kind;
112
113 fn emit<S>(
114 self,
115 sink: S,
116 ) -> Result<<Self::Kind as Dispatch>::Output<'d, S, Self::Stream>, S::Error>
117 where
118 S: Sink<'d>;
119}
120
121#[doc(hidden)]
122pub trait Complete<'req, 'd>: Sized {
123 type Bound: bound::Bound + 'd;
124
125 const BODY: payload::Kind;
126
127 fn complete<S>(self, sink: S) -> Result<S::Output, S::Error>
128 where
129 S: Accept<'d>;
130}
131
132impl<'req, 'd, T: Complete<'req, 'd>> Emit<'req, 'd> for T {
133 type Kind = Sync;
134 type Stream = Never;
135 type Bound = T::Bound;
136
137 const BODY: payload::Kind = T::BODY;
138
139 fn emit<S: Sink<'d>>(self, sink: S) -> Result<S::Output, S::Error> {
140 self.complete(sink)
141 }
142}
143
144impl<'req, 'd, T, E> Complete<'req, 'd> for Result<T, E>
145where
146 T: Complete<'req, 'd>,
147 E: Complete<'req, 'd>,
148{
149 type Bound = bound::Max<T::Bound, E::Bound>;
150
151 const BODY: payload::Kind = payload::Kind::Inline;
152
153 fn complete<S: Accept<'d>>(self, sink: S) -> Result<S::Output, S::Error> {
154 match self {
155 Ok(output) => output.complete(sink),
156 Err(error) => error.complete(sink),
157 }
158 }
159}
160
161impl<'req, 'd, L, R> Complete<'req, 'd> for Either<L, R>
162where
163 L: Complete<'req, 'd>,
164 R: Complete<'req, 'd>,
165{
166 type Bound = bound::Max<L::Bound, R::Bound>;
167
168 const BODY: payload::Kind = payload::Kind::Inline;
169
170 fn complete<S>(self, sink: S) -> Result<S::Output, S::Error>
171 where
172 S: Accept<'d>,
173 {
174 match self {
175 Self::Left(output) => output.complete(sink),
176 Self::Right(output) => output.complete(sink),
177 }
178 }
179}
180
181pub struct Stream<'req, S, const N: usize = 0> {
182 status: Code,
183 headers: set::Set<'req, N>,
184 stream: Option<S>,
185}
186
187impl<S, const N: usize> Stream<'_, S, N> {
188 pub fn with_status(mut self, status: Code) -> Self {
189 self.status = status;
190 self
191 }
192}
193
194impl<'req, S> Stream<'req, S, 0> {
195 pub fn new(stream: Option<S>) -> Self {
196 Self {
197 status: Code::OK,
198 headers: set::Set::new(),
199 stream,
200 }
201 }
202
203 pub fn headers<const N: usize>(self, headers: set::Set<'req, N>) -> Stream<'req, S, N> {
204 Stream {
205 status: self.status,
206 headers,
207 stream: self.stream,
208 }
209 }
210}
211
212impl<'req, 'd, 'head: 'req, S, const N: usize> Emit<'req, 'd> for Stream<'head, S, N> {
213 type Kind = Polled;
214 type Stream = S;
215 type Bound = bound::Unbounded;
216
217 const BODY: payload::Kind = payload::Kind::Stream;
218
219 fn emit<E>(self, sink: E) -> Result<E::Stream<S>, E::Error>
220 where
221 E: Sink<'d>,
222 {
223 let Self {
224 status,
225 headers,
226 stream,
227 } = self;
228 sink.stream(status, headers, stream)
229 }
230}