sark_http/payload/response/bounded/
mod.rs1use std::convert::Infallible;
2
3use crate::payload::response::status::Code;
4mod plan;
5
6use o3::buffer::resident::bound;
7pub use plan::Plan;
8
9use crate::payload::response::{
10 Encode, dispatch,
11 headers::{head, set},
12};
13
14pub trait Body: Encode {
15 fn max_len(&self) -> usize;
16}
17
18pub trait TryBody: Encode {
20 type Error;
21
22 fn try_max_len(&self) -> Result<usize, Self::Error>;
23}
24
25impl<B: Body> TryBody for B {
26 type Error = Infallible;
27
28 fn try_max_len(&self) -> Result<usize, Self::Error> {
29 Ok(self.max_len())
30 }
31}
32
33pub struct Output<'req, B, const N: usize = { set::DEFAULT_CAPACITY }> {
34 status: Code,
35 head: head::Head<'req, N>,
36 body: B,
37}
38
39impl<'req, B, const N: usize> Output<'req, B, N>
40where
41 B: Body,
42{
43 pub fn new(
44 status: Code,
45 template: &'req head::Template<'req>,
46 headers: set::Set<'req, N>,
47 body: B,
48 ) -> Self {
49 Self {
50 status,
51 head: head::Head::new(template, headers),
52 body,
53 }
54 }
55
56 pub fn status(&self) -> Code {
57 self.status
58 }
59
60 pub fn into_parts(self) -> (Code, head::Head<'req, N>, B) {
61 (self.status, self.head, self.body)
62 }
63}
64
65impl<'req, 'd, 'head: 'req, B, const N: usize> dispatch::IntoOutput<'req, 'd>
66 for Output<'head, B, N>
67where
68 B: Body,
69{
70 type Output = Self;
71
72 fn into_output(self) -> Self::Output {
73 self
74 }
75}
76
77impl<'req, 'd, 'head: 'req, B, const N: usize> dispatch::Complete<'req, 'd> for Output<'head, B, N>
78where
79 B: Body,
80{
81 const BODY: crate::payload::Kind = crate::payload::Kind::Inline;
82 type Bound = bound::Unbounded;
83
84 fn complete<S>(self, sink: S) -> Result<S::Output, S::Error>
85 where
86 S: dispatch::Accept<'d>,
87 {
88 let Self { status, head, body } = self;
89 dispatch::Accept::bounded(sink, status, head, body)
90 }
91}