use super::BodyBudgetError;
#[must_use]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BodyBudget {
max_input_bytes: usize,
max_output_bytes: usize,
}
impl BodyBudget {
pub const MIN_OUTPUT_BYTES: usize = "<truncated>".len();
#[inline]
pub const fn new(
max_input_bytes: usize,
max_output_bytes: usize,
) -> Result<Self, BodyBudgetError> {
if max_input_bytes == 0 {
return Err(BodyBudgetError::ZeroInput);
}
if max_output_bytes < Self::MIN_OUTPUT_BYTES {
return Err(BodyBudgetError::OutputTooSmall {
minimum: Self::MIN_OUTPUT_BYTES,
actual: max_output_bytes,
});
}
Ok(Self {
max_input_bytes,
max_output_bytes,
})
}
#[must_use]
#[inline(always)]
pub const fn max_input_bytes(self) -> usize {
self.max_input_bytes
}
#[must_use]
#[inline(always)]
pub const fn max_output_bytes(self) -> usize {
self.max_output_bytes
}
}
impl Default for BodyBudget {
#[inline(always)]
fn default() -> Self {
Self {
max_input_bytes: 16 * 1024,
max_output_bytes: 64 * 1024,
}
}
}