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