Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PushError::LimitExceeded. #349

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/base/message_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ impl<Target: Composer> MessageBuilder<Target> {
let new_pos = self.target.as_ref().len();
if new_pos >= self.limit {
self.target.truncate(pos);
return Err(PushError::ShortBuf);
return Err(PushError::LimitExceeded);
}

if inc(self.counts_mut()).is_err() {
Expand Down Expand Up @@ -2332,6 +2332,7 @@ impl<Target: Composer> Truncate for TreeCompressor<Target> {
#[derive(Clone, Copy, Debug)]
pub enum PushError {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be nice to document these variants to explain what they mean in rustdoc.

CountOverflow,
LimitExceeded,
ShortBuf,
}

Expand All @@ -2345,6 +2346,7 @@ impl fmt::Display for PushError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
PushError::CountOverflow => f.write_str("counter overflow"),
PushError::LimitExceeded => f.write_str("limit exceeded"),
PushError::ShortBuf => ShortBuf.fmt(f),
}
}
Expand Down Expand Up @@ -2451,7 +2453,10 @@ mod test {
msg.set_push_limit(25);

// Verify that push fails.
assert!(msg.push(|t| t.append_slice(&[0u8; 1]), |_| Ok(())).is_err());
assert!(matches!(
msg.push(|t| t.append_slice(&[0u8; 1]), |_| Ok(())),
Err(PushError::LimitExceeded)
));
assert_eq!(msg.as_slice().len(), hdr_len + 50);

// Remove the limit.
Expand All @@ -2464,7 +2469,10 @@ mod test {
assert_eq!(msg.as_slice().len(), 100);

// Verify that exceeding the underlying capacity limit fails.
assert!(msg.push(|t| t.append_slice(&[0u8; 1]), |_| Ok(())).is_err());
assert!(matches!(
msg.push(|t| t.append_slice(&[0u8; 1]), |_| Ok(())),
Err(PushError::ShortBuf)
));
assert_eq!(msg.as_slice().len(), 100);
}

Expand Down