Skip to content

Commit

Permalink
try_ methods to handle capacity overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
glebpom committed Jul 20, 2023
1 parent 78e3d37 commit 54a1525
Show file tree
Hide file tree
Showing 5 changed files with 501 additions and 171 deletions.
39 changes: 39 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ enum ErrorKind {
UriParts(uri::InvalidUriParts),
HeaderName(header::InvalidHeaderName),
HeaderValue(header::InvalidHeaderValue),
MaxSizeReached(MaxSizeReached),
}

impl fmt::Debug for Error {
Expand Down Expand Up @@ -61,6 +62,7 @@ impl Error {
UriParts(ref e) => e,
HeaderName(ref e) => e,
HeaderValue(ref e) => e,
MaxSizeReached(ref e) => e,
}
}
}
Expand All @@ -73,6 +75,14 @@ impl error::Error for Error {
}
}

impl From<MaxSizeReached> for Error {
fn from(err: MaxSizeReached) -> Error {
Error {
inner: ErrorKind::MaxSizeReached(err),
}
}
}

impl From<status::InvalidStatusCode> for Error {
fn from(err: status::InvalidStatusCode) -> Error {
Error {
Expand Down Expand Up @@ -127,6 +137,35 @@ impl From<std::convert::Infallible> for Error {
}
}

/// Error returned when max capacity of `HeaderMap` is exceeded
pub struct MaxSizeReached {
_priv: (),
}

impl MaxSizeReached {
/// Create new `MaxSizeReached` instance
pub fn new() -> MaxSizeReached {
MaxSizeReached { _priv: () }
}
}

impl fmt::Debug for MaxSizeReached {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("MaxSizeReached")
// skip _priv noise
.finish()
}
}

impl fmt::Display for MaxSizeReached {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("max size reached")
}
}

impl std::error::Error for MaxSizeReached {}


#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading

0 comments on commit 54a1525

Please sign in to comment.