pub struct ParseConfig {
pub max_recursion_depth: usize,
pub max_tokens: usize,
}Expand description
Configuration for parser behavior and resource limits.
Controls limits on recursion depth, token count, and other resources to prevent denial-of-service attacks via malformed input.
§Default Values
| Setting | Default | Rationale |
|---|---|---|
max_recursion_depth | 128 | Matches serde_json default |
max_tokens | usize::MAX | No limit by default |
§Security Considerations
Without recursion limits, deeply nested input like [[[[[[...]]]]]] can
cause stack overflow. The default limit of 128 prevents most attacks while
allowing reasonable nesting for typical use cases.
§Example
let config = ParseConfig::default();
let mut parser = TokenStream::with_config(tokens, config);
// In recursive parse implementation:
fn parse_nested(stream: &mut TokenStream) -> Result<Nested, Error> {
stream.enter_nested()?; // Increments depth, checks limit
let inner = stream.parse()?;
stream.exit_nested(); // Decrements depth
Ok(Nested { inner })
}Fields§
§max_recursion_depth: usizeMaximum allowed recursion depth.
When parsing nested structures, each level of nesting increments a
depth counter. If the counter exceeds this limit, parsing fails with
Error::RecursionLimitExceeded.
Default: 128 (matching serde_json)
max_tokens: usizeMaximum number of tokens to process.
If the parser consumes more than this many tokens, parsing fails. This can prevent resource exhaustion from extremely long inputs.
Default: usize::MAX (no limit)
Implementations§
Source§impl ParseConfig
impl ParseConfig
Sourcepub const DEFAULT: Self
pub const DEFAULT: Self
Default configuration, usable in const contexts.
Equivalent to ParseConfig::default() but available at compile time.
Sourcepub const fn with_max_recursion_depth(self, depth: usize) -> Self
pub const fn with_max_recursion_depth(self, depth: usize) -> Self
Sourcepub const fn with_max_tokens(self, count: usize) -> Self
pub const fn with_max_tokens(self, count: usize) -> Self
Sets the maximum token count.
§Arguments
count- Maximum tokens to process. Useusize::MAXto disable.
Sourcepub const fn disable_recursion_limit(self) -> Self
pub const fn disable_recursion_limit(self) -> Self
Disables the recursion limit.
§Warning
Only use this when parsing trusted input! Untrusted deeply-nested input can cause stack overflow.
Trait Implementations§
Source§impl Clone for ParseConfig
impl Clone for ParseConfig
Source§fn clone(&self) -> ParseConfig
fn clone(&self) -> ParseConfig
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more