use jsonx_lib::Parser;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct Limits {
pub max_message_bytes: usize,
pub max_header_bytes: usize,
pub max_headers: usize,
pub max_batch: usize,
pub max_skip_bytes: u64,
pub json: jsonx_lib::Limits,
}
impl Limits {
#[must_use]
pub const fn new() -> Self {
Self {
max_message_bytes: 64 * 1024 * 1024,
max_header_bytes: 8 * 1024,
max_headers: 32,
max_batch: 1024,
max_skip_bytes: u64::MAX,
json: jsonx_lib::Limits::new(),
}
}
#[must_use]
pub const fn lsp() -> Self {
Self::new()
}
#[must_use]
pub const fn mcp() -> Self {
Self::new().without_batches()
}
#[must_use]
pub const fn network() -> Self {
Self {
max_message_bytes: 4 * 1024 * 1024,
max_header_bytes: 4 * 1024,
max_headers: 16,
max_batch: 128,
max_skip_bytes: 64 * 1024 * 1024,
json: jsonx_lib::Limits::new()
.with_max_depth(64)
.with_max_string_bytes(1024 * 1024)
.with_max_number_bytes(128)
.with_max_elements(256 * 1024),
}
}
#[must_use]
pub const fn strict() -> Self {
Self {
max_message_bytes: 1024 * 1024,
max_header_bytes: 2 * 1024,
max_headers: 8,
max_batch: 16,
max_skip_bytes: 4 * 1024 * 1024,
json: jsonx_lib::Limits::new()
.with_max_depth(32)
.with_max_string_bytes(256 * 1024)
.with_max_number_bytes(64)
.with_max_elements(64 * 1024),
}
}
#[must_use]
pub const fn without_batches(self) -> Self {
self.with_max_batch(0)
}
#[must_use]
pub const fn with_max_message_bytes(mut self, bytes: usize) -> Self {
self.max_message_bytes = bytes;
self
}
#[must_use]
pub const fn with_max_header_bytes(mut self, bytes: usize) -> Self {
self.max_header_bytes = bytes;
self
}
#[must_use]
pub const fn with_max_headers(mut self, count: usize) -> Self {
self.max_headers = count;
self
}
#[must_use]
pub const fn with_max_batch(mut self, count: usize) -> Self {
self.max_batch = count;
self
}
#[must_use]
pub const fn with_max_skip_bytes(mut self, bytes: u64) -> Self {
self.max_skip_bytes = bytes;
self
}
#[must_use]
pub const fn with_json(mut self, json: jsonx_lib::Limits) -> Self {
self.json = json;
self
}
pub const fn parser(&self) -> Parser {
Parser::new().limits(self.json.with_max_input_bytes(self.max_message_bytes))
}
}
impl Default for Limits {
fn default() -> Self {
Self::new()
}
}