use core::{
error::Error,
fmt::{self, Display},
};
const DEFAULT_MAX_RECURSION_DEPTH: usize = 1000;
const DEFAULT_MIN_STACK_HEIGHT: usize = 1_000;
const DEFAULT_MAX_STACK_HEIGHT: usize = 1_000_000;
const DEFAULT_MAX_CACHED_STACKS: usize = 2;
#[derive(Debug)]
pub enum StackConfigError {
MinStackHeightExceedsMax,
}
impl Error for StackConfigError {}
impl Display for StackConfigError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
StackConfigError::MinStackHeightExceedsMax => {
write!(f, "minimum value stack height exceeds maximum stack height")
}
}
}
}
#[derive(Debug, Copy, Clone)]
pub struct StackConfig {
max_recursion_depth: usize,
min_stack_height: usize,
max_stack_height: usize,
max_cached_stacks: usize,
}
impl Default for StackConfig {
fn default() -> Self {
Self {
max_recursion_depth: DEFAULT_MAX_RECURSION_DEPTH,
min_stack_height: DEFAULT_MIN_STACK_HEIGHT,
max_stack_height: DEFAULT_MAX_STACK_HEIGHT,
max_cached_stacks: DEFAULT_MAX_CACHED_STACKS,
}
}
}
impl StackConfig {
pub fn set_max_recursion_depth(&mut self, value: usize) {
self.max_recursion_depth = value;
}
pub fn set_min_stack_height(&mut self, value: usize) -> Result<(), StackConfigError> {
if value > self.max_stack_height {
return Err(StackConfigError::MinStackHeightExceedsMax);
}
self.min_stack_height = value;
Ok(())
}
pub fn set_max_stack_height(&mut self, value: usize) -> Result<(), StackConfigError> {
if value < self.min_stack_height {
return Err(StackConfigError::MinStackHeightExceedsMax);
}
self.max_stack_height = value;
Ok(())
}
pub fn set_max_cached_stacks(&mut self, value: usize) {
self.max_cached_stacks = value;
}
pub fn max_recursion_depth(&self) -> usize {
self.max_recursion_depth
}
pub fn min_stack_height(&self) -> usize {
self.min_stack_height
}
pub fn max_stack_height(&self) -> usize {
self.max_stack_height
}
pub fn max_cached_stacks(&mut self) -> usize {
self.max_cached_stacks
}
}