#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub enum RequestBodyPolicy {
#[default]
Reject,
Buffer { max_bytes: u64 },
Stream { max_bytes: u64 },
}
impl RequestBodyPolicy {
pub fn is_reject(&self) -> bool {
matches!(self, Self::Reject)
}
pub fn max_bytes(&self) -> Option<u64> {
match self {
Self::Reject => None,
Self::Buffer { max_bytes } | Self::Stream { max_bytes } => Some(*max_bytes),
}
}
pub fn allows_buffer(&self) -> bool {
matches!(self, Self::Buffer { .. })
}
pub fn allows_stream(&self) -> bool {
matches!(self, Self::Stream { .. })
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_is_reject() {
assert_eq!(RequestBodyPolicy::default(), RequestBodyPolicy::Reject);
}
#[test]
fn reject_is_reject() {
assert!(RequestBodyPolicy::Reject.is_reject());
assert!(!RequestBodyPolicy::Buffer { max_bytes: 1024 }.is_reject());
}
#[test]
fn max_bytes_returns_limit() {
assert_eq!(RequestBodyPolicy::Reject.max_bytes(), None);
assert_eq!(
RequestBodyPolicy::Buffer { max_bytes: 1024 }.max_bytes(),
Some(1024)
);
assert_eq!(
RequestBodyPolicy::Stream { max_bytes: 4096 }.max_bytes(),
Some(4096)
);
}
#[test]
fn allows_buffer_only_for_buffer() {
assert!(!RequestBodyPolicy::Reject.allows_buffer());
assert!(RequestBodyPolicy::Buffer { max_bytes: 1024 }.allows_buffer());
assert!(!RequestBodyPolicy::Stream { max_bytes: 1024 }.allows_buffer());
}
#[test]
fn allows_stream_only_for_stream() {
assert!(!RequestBodyPolicy::Reject.allows_stream());
assert!(!RequestBodyPolicy::Buffer { max_bytes: 1024 }.allows_stream());
assert!(RequestBodyPolicy::Stream { max_bytes: 1024 }.allows_stream());
}
}