kutil_http/pieces/
response.rs

1use super::body::*;
2
3use {
4    bytes::*,
5    http::response::*,
6    std::{error::*, fmt},
7};
8
9//
10// ResponsePieces
11//
12
13/// [Response] pieces.
14///
15/// Can be used to reconstruct a response, e.g. with
16/// [BodyReader::new_with_first_bytes](super::super::body::BodyReader::new_with_first_bytes).
17#[derive(Clone, Debug)]
18pub struct ResponsePieces<ResponseBodyT> {
19    /// Response.
20    pub response: Response<ResponseBodyT>,
21
22    /// First bytes.
23    pub first_bytes: Bytes,
24}
25
26impl<ResponseBodyT> ResponsePieces<ResponseBodyT> {
27    /// Constructor.
28    pub fn new(parts: Parts, body: ResponseBodyT, first_bytes: Bytes) -> Self {
29        Self { response: Response::from_parts(parts, body), first_bytes }
30    }
31
32    /// Constructor.
33    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
38//
39// ErrorWithResponsePieces
40//
41
42/// [Error] with optional [ResponsePieces].
43pub struct ErrorWithResponsePieces<ErrorT, BodyT> {
44    /// Error.
45    pub error: ErrorT,
46
47    /// Pieces.
48    pub pieces: Option<ResponsePieces<BodyT>>,
49}
50
51impl<ErrorT, BodyT> ErrorWithResponsePieces<ErrorT, BodyT> {
52    /// Constructor.
53    pub fn new(error: ErrorT, pieces: Option<ResponsePieces<BodyT>>) -> Self {
54        Self { error, pieces }
55    }
56
57    /// Constructor.
58    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}