use qubit_budget::StructureLimits;
#[cfg(feature = "json")]
use qubit_budget::json::JsonValueLimits;
#[derive(Debug, Clone, Copy)]
pub struct RedactionLimitsBuilder {
max_input_bytes: usize,
max_output_bytes: usize,
domain: StructureLimits,
#[cfg(feature = "json")]
json: JsonValueLimits,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RedactionLimits {
max_input_bytes: usize,
max_output_bytes: usize,
domain: StructureLimits,
#[cfg(feature = "json")]
json: JsonValueLimits,
}
impl RedactionLimits {
#[must_use]
pub fn builder() -> RedactionLimitsBuilder {
RedactionLimitsBuilder::default()
}
#[must_use]
pub(crate) fn builder_from(base: &Self) -> RedactionLimitsBuilder {
RedactionLimitsBuilder {
max_input_bytes: base.max_input_bytes,
max_output_bytes: base.max_output_bytes,
domain: base.domain,
#[cfg(feature = "json")]
json: base.json,
}
}
#[must_use]
#[inline(always)]
pub(crate) const fn structural_limits(&self) -> StructureLimits {
self.domain
}
#[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
}
#[must_use]
#[inline(always)]
pub const fn max_depth(&self) -> Option<usize> {
self.domain.max_depth()
}
#[must_use]
#[inline(always)]
pub const fn max_nodes(&self) -> Option<usize> {
self.domain.max_nodes()
}
#[must_use]
#[inline(always)]
pub const fn max_collection_items(&self) -> Option<usize> {
self.domain.max_sequence_items()
}
#[must_use]
#[inline(always)]
pub const fn max_key_bytes(&self) -> Option<usize> {
self.domain.max_key_bytes()
}
pub(crate) fn validate(&self) -> Result<(), super::PolicyError> {
if self.max_output_bytes > isize::MAX as usize {
return Err(super::PolicyError::OutputLimitTooLarge {
maximum: self.max_output_bytes,
});
}
Ok(())
}
#[cfg(feature = "json")]
#[must_use]
#[inline(always)]
pub(crate) const fn json_limits(&self) -> JsonValueLimits {
self.json
}
#[cfg(feature = "json")]
#[must_use]
#[inline(always)]
pub const fn max_json_depth(&self) -> Option<usize> {
self.json.max_depth()
}
#[cfg(feature = "json")]
#[must_use]
#[inline(always)]
pub const fn max_json_nodes(&self) -> Option<usize> {
self.json.max_nodes()
}
#[cfg(feature = "json")]
#[must_use]
#[inline(always)]
pub const fn max_json_collection_items(&self) -> Option<usize> {
self.json.max_sequence_items()
}
#[cfg(feature = "json")]
#[must_use]
#[inline(always)]
pub const fn max_json_key_bytes(&self) -> Option<usize> {
self.json.max_key_bytes()
}
#[cfg(feature = "json")]
#[must_use]
#[inline(always)]
pub const fn max_json_string_bytes(&self) -> Option<usize> {
self.json.max_string_bytes()
}
#[cfg(feature = "json")]
#[must_use]
#[inline(always)]
pub const fn max_json_number_bytes(&self) -> Option<usize> {
self.json.max_number_bytes()
}
#[cfg(feature = "json")]
#[must_use]
#[inline(always)]
pub const fn max_json_payload_bytes(&self) -> Option<usize> {
self.json.max_payload_bytes()
}
}
impl RedactionLimitsBuilder {
pub fn max_input_bytes(&mut self, maximum: usize) -> &mut Self {
self.max_input_bytes = maximum;
self
}
pub fn max_output_bytes(&mut self, maximum: usize) -> &mut Self {
self.max_output_bytes = maximum;
self
}
pub fn max_depth(&mut self, maximum: usize) -> &mut Self {
self.domain = self.domain.into_builder().max_depth(maximum).build();
self
}
pub fn max_nodes(&mut self, maximum: usize) -> &mut Self {
self.domain = self.domain.into_builder().max_nodes(maximum).build();
self
}
pub fn max_collection_items(&mut self, maximum: usize) -> &mut Self {
self.domain = self
.domain
.into_builder()
.max_sequence_items(maximum)
.max_map_entries(maximum)
.build();
self
}
pub fn max_key_bytes(&mut self, maximum: usize) -> &mut Self {
self.domain = self.domain.into_builder().max_key_bytes(maximum).build();
self
}
#[cfg(feature = "json")]
pub fn max_json_depth(&mut self, maximum: usize) -> &mut Self {
self.json = self.json.into_builder().max_depth(maximum).build();
self
}
#[cfg(feature = "json")]
pub fn max_json_nodes(&mut self, maximum: usize) -> &mut Self {
self.json = self.json.into_builder().max_nodes(maximum).build();
self
}
#[cfg(feature = "json")]
pub fn max_json_collection_items(&mut self, maximum: usize) -> &mut Self {
self.json = self
.json
.into_builder()
.max_sequence_items(maximum)
.max_map_entries(maximum)
.build();
self
}
#[cfg(feature = "json")]
pub fn max_json_key_bytes(&mut self, maximum: usize) -> &mut Self {
self.json = self.json.into_builder().max_key_bytes(maximum).build();
self
}
#[cfg(feature = "json")]
pub fn max_json_string_bytes(&mut self, maximum: usize) -> &mut Self {
self.json = self.json.into_builder().max_string_bytes(maximum).build();
self
}
#[cfg(feature = "json")]
pub fn max_json_number_bytes(&mut self, maximum: usize) -> &mut Self {
self.json = self.json.into_builder().max_number_bytes(maximum).build();
self
}
#[cfg(feature = "json")]
pub fn max_json_payload_bytes(&mut self, maximum: usize) -> &mut Self {
self.json = self.json.into_builder().max_payload_bytes(maximum).build();
self
}
#[must_use]
pub fn build(self) -> RedactionLimits {
RedactionLimits {
max_input_bytes: self.max_input_bytes,
max_output_bytes: self.max_output_bytes,
domain: self.domain,
#[cfg(feature = "json")]
json: self.json,
}
}
}
impl Default for RedactionLimitsBuilder {
fn default() -> Self {
Self {
max_input_bytes: 64 * 1024,
max_output_bytes: 16 * 1024,
domain: StructureLimits::builder()
.max_depth(32)
.max_nodes(1_024)
.max_sequence_items(256)
.max_map_entries(256)
.max_key_bytes(256)
.build(),
#[cfg(feature = "json")]
json: JsonValueLimits::default(),
}
}
}
impl Default for RedactionLimits {
fn default() -> Self {
Self::builder().build()
}
}