sark_http/payload/response/
output.rs1use std::marker::PhantomData;
2
3use o3::buffer::resident::bound;
4
5use super::{Body, dispatch, encoded, headers::Application, status::Code};
6use crate::{field, payload};
7
8pub struct Output<H, B, F> {
9 status: Code,
10 fields: H,
11 body: B,
12 form: PhantomData<fn() -> F>,
13}
14
15impl<H: Copy> Copy for Output<H, payload::fixed::Body<'_>, Static> {}
16impl<H: Copy> Clone for Output<H, payload::fixed::Body<'_>, Static> {
17 fn clone(&self) -> Self {
18 *self
19 }
20}
21
22impl<H, B, F> Output<H, B, F> {
23 pub fn new(status: Code, fields: H, body: B) -> Self {
24 Self {
25 status,
26 fields,
27 body,
28 form: PhantomData,
29 }
30 }
31
32 pub fn status(&self) -> Code {
33 self.status
34 }
35
36 pub fn body(&self) -> &B {
37 &self.body
38 }
39}
40
41pub enum Fixed {}
42pub enum Static {}
43pub struct Encoded<const EXACT: bool>;
44
45mod sealed {
46 pub trait Form {}
47 impl Form for super::Fixed {}
48 impl Form for super::Static {}
49 impl<const EXACT: bool> Form for super::Encoded<EXACT> {}
50}
51
52pub trait Form<'d, B>: sealed::Form {
53 type Bound: bound::Bound + 'd;
54 const BODY: payload::Kind;
55
56 fn emit<S: dispatch::Accept<'d>>(
57 sink: S,
58 status: Code,
59 headers: impl field::Source<Application>,
60 body: B,
61 ) -> Result<S::Output, S::Error>;
62}
63
64impl<'d, B: Body<'d>> Form<'d, B> for Fixed {
65 type Bound = bound::Unbounded;
66 const BODY: payload::Kind = payload::Kind::Inline;
67
68 fn emit<S: dispatch::Accept<'d>>(
69 sink: S,
70 status: Code,
71 headers: impl field::Source<Application>,
72 body: B,
73 ) -> Result<S::Output, S::Error> {
74 sink.fixed(status, headers, body)
75 }
76}
77
78impl<'body: 'd, 'd> Form<'d, payload::fixed::Body<'body>> for Static {
79 type Bound = bound::AtMost<0>;
80 const BODY: payload::Kind = payload::Kind::Static;
81
82 fn emit<S: dispatch::Accept<'d>>(
83 sink: S,
84 status: Code,
85 headers: impl field::Source<Application>,
86 body: payload::fixed::Body<'body>,
87 ) -> Result<S::Output, S::Error> {
88 sink.statics(status, headers, body)
89 }
90}
91
92impl<'d, B: encoded::Source<EXACT>, const EXACT: bool> Form<'d, B> for Encoded<EXACT> {
93 type Bound = bound::Unbounded;
94 const BODY: payload::Kind = payload::Kind::Inline;
95
96 fn emit<S: dispatch::Accept<'d>>(
97 sink: S,
98 status: Code,
99 headers: impl field::Source<Application>,
100 body: B,
101 ) -> Result<S::Output, S::Error> {
102 sink.encoded::<EXACT>(status, headers, body)
103 }
104}
105
106impl<'req, 'd, H: field::Source<Application>, B, F: Form<'d, B>> dispatch::Complete<'req, 'd>
107 for Output<H, B, F>
108{
109 type Bound = F::Bound;
110 const BODY: payload::Kind = F::BODY;
111
112 fn complete<S: dispatch::Accept<'d>>(self, sink: S) -> Result<S::Output, S::Error> {
113 F::emit(sink, self.status, self.fields, self.body)
114 }
115}