kutil_http/pieces/
body.rs

1use {
2    bytes::*,
3    std::{error::*, fmt},
4};
5
6//
7// BodyPieces
8//
9
10/// [Body](http_body::Body) pieces.
11///
12/// Can be used to reconstruct a body, e.g. with
13/// [BodyReader::new_with_first_bytes](super::super::body::BodyReader::new_with_first_bytes).
14#[derive(Clone, Debug)]
15pub struct BodyPieces<BodyT> {
16    /// Body.
17    pub body: BodyT,
18
19    /// First bytes.
20    pub first_bytes: Bytes,
21}
22
23impl<BodyT> BodyPieces<BodyT> {
24    /// Constructor.
25    pub fn new(body: BodyT, first_bytes: Bytes) -> Self {
26        Self { body, first_bytes }
27    }
28}
29
30//
31// ErrorWithBodyPieces
32//
33
34/// [Error] with optional [BodyPieces].
35pub struct ErrorWithBodyPieces<ErrorT, BodyT> {
36    /// Error.
37    pub error: ErrorT,
38
39    /// Pieces.
40    pub pieces: Option<BodyPieces<BodyT>>,
41}
42
43impl<ErrorT, BodyT> ErrorWithBodyPieces<ErrorT, BodyT> {
44    /// Constructor.
45    pub fn new(error: ErrorT, pieces: Option<BodyPieces<BodyT>>) -> Self {
46        Self { error, pieces }
47    }
48}
49
50impl<ErrorT, BodyT> fmt::Debug for ErrorWithBodyPieces<ErrorT, BodyT>
51where
52    ErrorT: fmt::Debug,
53{
54    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
55        fmt::Debug::fmt(&self.error, formatter)
56    }
57}
58
59impl<ErrorT, BodyT> fmt::Display for ErrorWithBodyPieces<ErrorT, BodyT>
60where
61    ErrorT: fmt::Display,
62{
63    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
64        fmt::Display::fmt(&self.error, formatter)
65    }
66}
67
68impl<ErrorT, BodyT> Error for ErrorWithBodyPieces<ErrorT, BodyT>
69where
70    ErrorT: Error,
71{
72    fn source(&self) -> Option<&(dyn Error + 'static)> {
73        self.error.source()
74    }
75}
76
77impl<ErrorT, BodyT> From<ErrorT> for ErrorWithBodyPieces<ErrorT, BodyT> {
78    fn from(error: ErrorT) -> Self {
79        Self::new(error, None)
80    }
81}