use crate::{CborError, CborErrorCode};
pub const MAX_SAFE_INTEGER: u64 = 9_007_199_254_740_991;
pub const MAX_SAFE_INTEGER_I64: i64 = 9_007_199_254_740_991;
pub const MIN_SAFE_INTEGER: i64 = -MAX_SAFE_INTEGER_I64;
pub const DEFAULT_MAX_DEPTH: usize = 256;
pub const DEFAULT_MAX_CONTAINER_LEN: usize = 1 << 16;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DecodeLimits {
pub max_depth: usize,
pub max_total_items: usize,
pub max_array_len: usize,
pub max_map_len: usize,
pub max_bytes_len: usize,
pub max_text_len: usize,
}
impl DecodeLimits {
#[must_use]
pub fn for_bytes(max_message_bytes: usize) -> Self {
let max_container_len = max_message_bytes.min(DEFAULT_MAX_CONTAINER_LEN);
Self {
max_depth: DEFAULT_MAX_DEPTH,
max_total_items: max_message_bytes,
max_array_len: max_container_len,
max_map_len: max_container_len,
max_bytes_len: max_message_bytes,
max_text_len: max_message_bytes,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CborLimits {
pub max_message_bytes: usize,
pub max_state_bytes: usize,
}
impl CborLimits {
pub const fn new(max_message_bytes: usize, max_state_bytes: usize) -> Result<Self, CborError> {
if max_state_bytes > max_message_bytes {
return Err(CborError::encode(CborErrorCode::InvalidLimits));
}
Ok(Self {
max_message_bytes,
max_state_bytes,
})
}
#[must_use]
pub fn message_limits(self) -> DecodeLimits {
DecodeLimits::for_bytes(self.max_message_bytes)
}
#[must_use]
pub fn state_limits(self) -> DecodeLimits {
DecodeLimits::for_bytes(self.max_state_bytes)
}
}