http_request/response/trait.rs
1use crate::*;
2
3/// A trait representing common behaviors for HTTP response types.
4///
5/// This trait provides methods for transforming an HTTP response into
6/// different formats (text and binary) and parsing raw HTTP response data.
7/// Implementing types should define how to convert the response into text
8/// and binary formats, as well as how to parse raw response data into a
9/// structured representation.
10///
11/// # Associated Types
12/// - `OutputText`: The type returned by the `text` method, typically a text-based HTTP response.
13/// - `OutputBinary`: The type returned by the `binary` method, typically a binary-based HTTP response.
14pub trait ResponseTrait: Send + Debug {
15 type OutputText: Clone + Sized;
16 type OutputBinary: Clone + Sized;
17
18 /// Transforms the HTTP response into a text representation.
19 ///
20 /// This method converts the body of the HTTP response into a string format.
21 ///
22 /// # Returns
23 /// - `Self::OutputText`: The text representation of the HTTP response, typically a string.
24 fn text(&self) -> Self::OutputText;
25
26 /// Transforms the HTTP response into a binary representation.
27 ///
28 /// This method converts the body of the HTTP response into a byte-based format.
29 ///
30 /// # Returns
31 /// - `Self::OutputBinary`: The binary representation of the HTTP response, typically a byte vector.
32 fn binary(&self) -> Self::OutputBinary;
33
34 /// Parses a raw HTTP response into the associated type `Output`.
35 ///
36 /// This method is responsible for parsing a byte slice representing a raw HTTP response
37 /// and transforming it into a structured HTTP response object.
38 ///
39 /// # Parameters
40 /// - `response`: A byte slice representing the raw HTTP response.
41 ///
42 /// # Returns
43 /// - `Self`: An instance of the implementing type, populated with parsed data.
44 fn from(response: &[u8]) -> Self
45 where
46 Self: Sized;
47
48 /// Decodes the data using a specified buffer size.
49 ///
50 /// This method takes a buffer size as input and performs the decoding process.
51 /// It returns the decoded output in the form of `Self::OutputBinary`.
52 ///
53 /// # Parameters
54 /// - `buffer_size`: The buffer size to be used during decoding.
55 ///
56 /// # Returns
57 /// Returns the decoded data as `Self::OutputBinary`. The exact type of `OutputBinary` depends on the implementation of the `Self` type.
58 fn decode(&self, buffer_size: usize) -> Self::OutputBinary;
59}