#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct Limits {
pub max_depth: usize,
pub max_input_bytes: usize,
pub max_string_bytes: usize,
pub max_number_bytes: usize,
pub max_elements: usize,
}
impl Limits {
#[must_use]
pub const fn new() -> Self {
Self {
max_depth: 128,
max_input_bytes: 64 * 1024 * 1024,
max_string_bytes: 16 * 1024 * 1024,
max_number_bytes: 1024,
max_elements: 4 * 1024 * 1024,
}
}
#[must_use]
pub const fn unlimited() -> Self {
Self {
max_depth: usize::MAX,
max_input_bytes: usize::MAX,
max_string_bytes: usize::MAX,
max_number_bytes: usize::MAX,
max_elements: usize::MAX,
}
}
#[must_use]
pub const fn with_max_depth(mut self, depth: usize) -> Self {
self.max_depth = depth;
self
}
#[must_use]
pub const fn with_max_input_bytes(mut self, bytes: usize) -> Self {
self.max_input_bytes = bytes;
self
}
#[must_use]
pub const fn with_max_string_bytes(mut self, bytes: usize) -> Self {
self.max_string_bytes = bytes;
self
}
#[must_use]
pub const fn with_max_number_bytes(mut self, bytes: usize) -> Self {
self.max_number_bytes = bytes;
self
}
#[must_use]
pub const fn with_max_elements(mut self, count: usize) -> Self {
self.max_elements = count;
self
}
}
impl Default for Limits {
#[inline]
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_limits_default_matches_documented_table() {
let l = Limits::default();
assert_eq!(l.max_depth, 128);
assert_eq!(l.max_input_bytes, 64 << 20);
assert_eq!(l.max_string_bytes, 16 << 20);
assert_eq!(l.max_number_bytes, 1024);
assert_eq!(l.max_elements, 4 << 20);
}
#[test]
fn test_limits_setters_chain() {
let l = Limits::unlimited()
.with_max_depth(1)
.with_max_input_bytes(2)
.with_max_string_bytes(3)
.with_max_number_bytes(4)
.with_max_elements(5);
assert_eq!(
(
l.max_depth,
l.max_input_bytes,
l.max_string_bytes,
l.max_number_bytes,
l.max_elements
),
(1, 2, 3, 4, 5)
);
}
}