use crate::ast::ParseError;
pub const DEFAULT_MAX_DEPTH: usize = 2000;
pub struct DepthLimiter {
max_depth: usize,
current_depth: usize,
}
impl Default for DepthLimiter {
fn default() -> Self {
Self {
max_depth: DEFAULT_MAX_DEPTH,
current_depth: 0,
}
}
}
impl DepthLimiter {
pub fn new(max_depth: usize) -> Self {
Self {
max_depth,
current_depth: 0,
}
}
pub fn bump(&self) -> Result<Self, ParseError> {
if self.current_depth >= self.max_depth {
Err(ParseError::MaxDepthExceeded)
} else {
Ok(Self {
max_depth: self.max_depth,
current_depth: self.current_depth + 1,
})
}
}
}