#[derive(Debug, Clone, Copy)]
pub struct DeserializerConfig {
max_array_size: Option<usize>,
max_map_size: Option<usize>,
max_recursion_depth: u8,
max_indefinite_iterations: Option<usize>,
}
impl Default for DeserializerConfig {
fn default() -> Self {
Self {
max_array_size: Some(100_000),
max_map_size: Some(100_000),
max_recursion_depth: 128,
max_indefinite_iterations: Some(100_000),
}
}
}
impl DeserializerConfig {
#[must_use]
pub const fn unlimited() -> Self {
Self {
max_array_size: None,
max_map_size: None,
max_recursion_depth: 255,
max_indefinite_iterations: None,
}
}
#[must_use]
pub const fn strict() -> Self {
Self {
max_array_size: Some(1_000),
max_map_size: Some(1_000),
max_recursion_depth: 32,
max_indefinite_iterations: Some(1_000),
}
}
#[must_use]
pub const fn max_array_size(mut self, limit: usize) -> Self {
self.max_array_size = Some(limit);
self
}
#[must_use]
pub const fn unlimited_array_size(mut self) -> Self {
self.max_array_size = None;
self
}
#[must_use]
pub const fn max_map_size(mut self, limit: usize) -> Self {
self.max_map_size = Some(limit);
self
}
#[must_use]
pub const fn unlimited_map_size(mut self) -> Self {
self.max_map_size = None;
self
}
#[must_use]
pub const fn max_recursion_depth(mut self, depth: u8) -> Self {
self.max_recursion_depth = depth;
self
}
#[must_use]
pub const fn max_indefinite_iterations(mut self, limit: usize) -> Self {
self.max_indefinite_iterations = Some(limit);
self
}
#[must_use]
pub const fn unlimited_indefinite_iterations(mut self) -> Self {
self.max_indefinite_iterations = None;
self
}
#[must_use]
pub const fn get_max_array_size(&self) -> Option<usize> {
self.max_array_size
}
#[must_use]
pub const fn get_max_map_size(&self) -> Option<usize> {
self.max_map_size
}
#[must_use]
pub const fn get_max_recursion_depth(&self) -> u8 {
self.max_recursion_depth
}
#[must_use]
pub const fn get_max_indefinite_iterations(&self) -> Option<usize> {
self.max_indefinite_iterations
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = DeserializerConfig::default();
assert_eq!(config.get_max_array_size(), Some(100_000));
assert_eq!(config.get_max_map_size(), Some(100_000));
assert_eq!(config.get_max_recursion_depth(), 128);
assert_eq!(config.get_max_indefinite_iterations(), Some(100_000));
}
#[test]
fn test_unlimited_config() {
let config = DeserializerConfig::unlimited();
assert_eq!(config.get_max_array_size(), None);
assert_eq!(config.get_max_map_size(), None);
assert_eq!(config.get_max_recursion_depth(), 255);
assert_eq!(config.get_max_indefinite_iterations(), None);
}
#[test]
fn test_strict_config() {
let config = DeserializerConfig::strict();
assert_eq!(config.get_max_array_size(), Some(1_000));
assert_eq!(config.get_max_map_size(), Some(1_000));
assert_eq!(config.get_max_recursion_depth(), 32);
assert_eq!(config.get_max_indefinite_iterations(), Some(1_000));
}
#[test]
fn test_custom_config() {
let config = DeserializerConfig::default()
.max_array_size(500)
.max_map_size(250)
.max_recursion_depth(16)
.max_indefinite_iterations(1000);
assert_eq!(config.get_max_array_size(), Some(500));
assert_eq!(config.get_max_map_size(), Some(250));
assert_eq!(config.get_max_recursion_depth(), 16);
assert_eq!(config.get_max_indefinite_iterations(), Some(1000));
}
#[test]
fn test_unlimited_modifications() {
let config = DeserializerConfig::default()
.unlimited_array_size()
.unlimited_map_size()
.unlimited_indefinite_iterations();
assert_eq!(config.get_max_array_size(), None);
assert_eq!(config.get_max_map_size(), None);
assert_eq!(config.get_max_indefinite_iterations(), None);
}
}