diff --git a/crates/http/src/protocol/message.rs b/crates/http/src/protocol/message.rs index e871131..49695ee 100644 --- a/crates/http/src/protocol/message.rs +++ b/crates/http/src/protocol/message.rs @@ -1,32 +1,53 @@ use bytes::{Buf, Bytes}; -/// represent the request/response message +/// Represents a HTTP message that can either be a header or payload. +/// +/// This enum is used to handle both request and response messages in the HTTP protocol. +/// The generic parameter `T` typically represents the header type (request or response header), +/// while `Data` represents the type of the payload data (defaults to `Bytes`). pub enum Message { + /// Contains the header information of type `T` Header(T), + /// Contains a chunk of payload data or EOF marker Payload(PayloadItem), } -/// payload item produced from payload decoder +/// Represents an item in the HTTP message payload stream. +/// +/// This enum is used by the payload decoder to produce either data chunks +/// or signal the end of the payload stream (EOF). #[derive(Debug, Clone, PartialEq, Eq)] pub enum PayloadItem { + /// A chunk of payload data Chunk(Data), + /// Marks the end of the payload stream Eof, } -/// represent the payload size +/// Represents the size information of an HTTP payload. +/// +/// This enum is used to determine how the payload should be processed: +/// - Known length: Process exact number of bytes +/// - Chunked: Process using chunked transfer encoding +/// - Empty: No payload to process #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum PayloadSize { + /// Payload with known length in bytes Length(u64), + /// Payload using chunked transfer encoding Chunked, + /// Empty payload (no body) Empty, } impl PayloadSize { + /// Returns true if the payload uses chunked transfer encoding #[inline] pub fn is_chunked(&self) -> bool { matches!(self, PayloadSize::Chunked) } + /// Returns true if the payload is empty #[inline] pub fn is_empty(&self) -> bool { matches!(self, PayloadSize::Empty) @@ -34,20 +55,21 @@ impl PayloadSize { } impl Message { + /// Returns true if this message contains payload data + #[inline] pub fn is_payload(&self) -> bool { - match self { - Message::Header(_) => false, - Message::Payload(_) => true, - } + matches!(self, Message::Payload(_)) } + /// Returns true if this message contains header information + #[inline] pub fn is_header(&self) -> bool { - match self { - Message::Header(_) => true, - Message::Payload(_) => false, - } + matches!(self, Message::Header(_)) } + /// Converts the message into a PayloadItem if it contains payload data + /// + /// Returns None if the message contains header information pub fn into_payload_item(self) -> Option { match self { Message::Header(_) => None, @@ -56,6 +78,10 @@ impl Message { } } +/// Converts bytes into a Message +/// +/// This allows bytes to be directly converted into a Message for sending payload data. +/// The generic type T is unused since this only creates payload messages. impl From for Message { fn from(bytes: Bytes) -> Self { Self::Payload(PayloadItem::Chunk(bytes)) @@ -63,22 +89,23 @@ impl From for Message { } impl PayloadItem { + /// Returns true if this item represents the end of the payload stream + #[inline] pub fn is_eof(&self) -> bool { - match self { - PayloadItem::Chunk(_) => false, - PayloadItem::Eof => true, - } + matches!(self, PayloadItem::Eof) } + /// Returns true if this item contains chunk data + #[inline] pub fn is_chunk(&self) -> bool { - match self { - PayloadItem::Chunk(_) => true, - PayloadItem::Eof => false, - } + matches!(self, PayloadItem::Chunk(_)) } } impl PayloadItem { + /// Returns a reference to the contained bytes if this is a Chunk + /// + /// Returns None if this is an EOF marker pub fn as_bytes(&self) -> Option<&Bytes> { match self { PayloadItem::Chunk(bytes) => Some(bytes), @@ -86,6 +113,9 @@ impl PayloadItem { } } + /// Returns a mutable reference to the contained bytes if this is a Chunk + /// + /// Returns None if this is an EOF marker pub fn as_mut_bytes(&mut self) -> Option<&mut Bytes> { match self { PayloadItem::Chunk(bytes) => Some(bytes), @@ -93,6 +123,9 @@ impl PayloadItem { } } + /// Consumes the PayloadItem and returns the contained bytes if this is a Chunk + /// + /// Returns None if this is an EOF marker pub fn into_bytes(self) -> Option { match self { PayloadItem::Chunk(bytes) => Some(bytes),