Skip to content

Commit

Permalink
feat(LengthDelimitedCodec): added an encode error: MessageTooBig
Browse files Browse the repository at this point in the history
Signed-off-by: JadKHaddad <jadkhaddad@gmail.com>
  • Loading branch information
JadKHaddad committed Sep 17, 2024
1 parent 40f5bc8 commit 0905658
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/codec/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,17 @@ impl std::error::Error for LengthDelimitedDecodeError {}
pub enum LengthDelimitedEncodeError {
/// The input buffer is too small to fit the encoded sequesnce of bytes.
InputBufferTooSmall,
/// The message is too big to fit into the frame.
///
/// Message size is limited to `u32::MAX` bytes.
MessageTooBig,
}

impl core::fmt::Display for LengthDelimitedEncodeError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::InputBufferTooSmall => write!(f, "Input buffer too small"),
Self::MessageTooBig => write!(f, "Message too big"),
}
}
}
Expand All @@ -78,10 +83,9 @@ impl<const N: usize> LengthDelimitedCodec<N> {
let item_size = item.len();
let frame_size = item_size + 4;

// TODO: make this a feature or a configuration option
// if item_size > u32::MAX as usize {
// return Err(LengthDelimitedEncodeError::MessageTooBig);
// }
if item_size > u32::MAX as usize {
return Err(LengthDelimitedEncodeError::MessageTooBig);
}

#[cfg(all(feature = "logging", feature = "tracing"))]
{
Expand Down

0 comments on commit 0905658

Please sign in to comment.