ParseConfig

Struct ParseConfig 

Source
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

SettingDefaultRationale
max_recursion_depth128Matches serde_json default
max_tokensusize::MAXNo 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: usize

Maximum 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: usize

Maximum 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

Source

pub const DEFAULT: Self

Default configuration, usable in const contexts.

Equivalent to ParseConfig::default() but available at compile time.

Source

pub const fn new() -> Self

Creates a new configuration with default values.

Source

pub const fn with_max_recursion_depth(self, depth: usize) -> Self

Sets the maximum recursion depth.

§Arguments
  • depth - Maximum nesting level. Use usize::MAX to disable the limit.
§Example
let config = ParseConfig::new()
    .with_max_recursion_depth(256);
Source

pub const fn with_max_tokens(self, count: usize) -> Self

Sets the maximum token count.

§Arguments
  • count - Maximum tokens to process. Use usize::MAX to disable.
Source

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

Source§

fn clone(&self) -> ParseConfig

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ParseConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ParseConfig

Source§

fn default() -> Self

Returns the default configuration.

  • max_recursion_depth: 128
  • max_tokens: usize::MAX
Source§

impl PartialEq for ParseConfig

Source§

fn eq(&self, other: &ParseConfig) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for ParseConfig

Source§

impl Eq for ParseConfig

Source§

impl StructuralPartialEq for ParseConfig

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.