kutil_http/pieces/
response.rs1use super::body::*;
2
3use {
4 bytes::*,
5 http::response::*,
6 std::{error::*, fmt},
7};
8
9#[derive(Clone, Debug)]
18pub struct ResponsePieces<ResponseBodyT> {
19 pub response: Response<ResponseBodyT>,
21
22 pub first_bytes: Bytes,
24}
25
26impl<ResponseBodyT> ResponsePieces<ResponseBodyT> {
27 pub fn new(parts: Parts, body: ResponseBodyT, first_bytes: Bytes) -> Self {
29 Self { response: Response::from_parts(parts, body), first_bytes }
30 }
31
32 pub fn new_from_body_pieces(parts: Parts, body_pieces: BodyPieces<ResponseBodyT>) -> Self {
34 Self::new(parts, body_pieces.body, body_pieces.first_bytes)
35 }
36}
37
38pub struct ErrorWithResponsePieces<ErrorT, BodyT> {
44 pub error: ErrorT,
46
47 pub pieces: Option<ResponsePieces<BodyT>>,
49}
50
51impl<ErrorT, BodyT> ErrorWithResponsePieces<ErrorT, BodyT> {
52 pub fn new(error: ErrorT, pieces: Option<ResponsePieces<BodyT>>) -> Self {
54 Self { error, pieces }
55 }
56
57 pub fn new_from_body(error: ErrorWithBodyPieces<ErrorT, BodyT>, parts: Parts) -> Self {
59 Self::new(error.error, error.pieces.map(|pieces| ResponsePieces::new_from_body_pieces(parts, pieces)))
60 }
61}
62
63impl<ErrorT, BodyT> fmt::Debug for ErrorWithResponsePieces<ErrorT, BodyT>
64where
65 ErrorT: fmt::Debug,
66{
67 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
68 fmt::Debug::fmt(&self.error, formatter)
69 }
70}
71
72impl<ErrorT, BodyT> fmt::Display for ErrorWithResponsePieces<ErrorT, BodyT>
73where
74 ErrorT: fmt::Display,
75{
76 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
77 fmt::Display::fmt(&self.error, formatter)
78 }
79}
80
81impl<ErrorT, BodyT> Error for ErrorWithResponsePieces<ErrorT, BodyT>
82where
83 ErrorT: Error,
84{
85 fn source(&self) -> Option<&(dyn Error + 'static)> {
86 self.error.source()
87 }
88}
89
90impl<ErrorT, BodyT> From<ErrorT> for ErrorWithResponsePieces<ErrorT, BodyT> {
91 fn from(error: ErrorT) -> Self {
92 Self::new(error, None)
93 }
94}