Skip to main content

granit_parser/
options.rs

1/// Options controlling parser and scanner behavior and resource usage.
2///
3/// Construct this type with [`crate::options!`] so that code remains compatible when new options
4/// are added in future releases.
5///
6/// # Examples
7///
8/// ```rust
9/// let options = granit_parser::options! {
10///     max_buffered_comment_events: 64,
11///     emit_comments: false,
12///     flow_nesting_limit: 512,
13///     block_nesting_limit: 256,
14/// };
15///
16/// assert_eq!(options.max_buffered_comment_events, 64);
17/// assert!(!options.emit_comments);
18/// assert_eq!(options.flow_nesting_limit, 512);
19/// assert_eq!(options.block_nesting_limit, 256);
20/// ```
21#[non_exhaustive]
22#[derive(Clone, Debug, Eq, PartialEq)]
23pub struct Options {
24    /// Whether scanners emit comment tokens and parsers emit comment events.
25    ///
26    /// The default is `true`. When this is `false`, comments are still recognized and validated
27    /// as YAML syntax, but their text is not captured and no comment tokens or events are emitted.
28    /// Comment bytes are still consumed, so this is not an input-size or processing-time limit.
29    /// [`Self::max_buffered_comment_events`] has no effect while comment emission is disabled.
30    pub emit_comments: bool,
31    /// Maximum number of consecutive comment events buffered while resolving an ambiguous
32    /// collection entry.
33    ///
34    /// The default is 96. A value of zero rejects the first comment that would need buffering.
35    pub max_buffered_comment_events: usize,
36    /// Maximum number of characters inspected while resolving a simple key.
37    ///
38    /// The default is 1024, matching YAML's simple-key length restriction. A key at exactly this
39    /// limit is accepted. Lower values impose a stricter resource limit; higher values relax that
40    /// YAML restriction.
41    pub simple_key_max_lookahead: usize,
42    /// Maximum number of simultaneously nested flow collections.
43    ///
44    /// The default is 255. A value of zero rejects the first flow collection opener.
45    pub flow_nesting_limit: usize,
46    /// Maximum number of simultaneously nested block collections parsed, or block indentation
47    /// levels retained by a scanner used directly.
48    ///
49    /// The default is 255. A value of zero rejects the first block sequence or mapping. This
50    /// bounds both indentation state retained while scanning and the number of closing tokens
51    /// queued when nested block collections end together. Parsers also count indentless block
52    /// sequences, which do not add scanner indentation state.
53    pub block_nesting_limit: usize,
54    /// Maximum number of UTF-8 source bytes retained for a directive name and payload.
55    ///
56    /// The default is 1024. Separating blanks before retained values count toward the limit.
57    /// `%YAML` version components instead have a fixed nine-digit limit and are stored as integers.
58    /// Real directives are far shorter than the default.
59    pub max_directive_bytes: usize,
60    /// Maximum number of parameters retained for a single reserved directive.
61    ///
62    /// The default is 16. The parser ignores reserved directives, so their parameters only reach
63    /// code driving [`crate::Scanner`] directly. A value of zero rejects the first parameter.
64    pub max_reserved_directive_params: usize,
65}
66
67impl Default for Options {
68    fn default() -> Self {
69        Self {
70            emit_comments: true,
71            max_buffered_comment_events: 96,
72            simple_key_max_lookahead: 1024,
73            flow_nesting_limit: 255,
74            block_nesting_limit: 255,
75            max_directive_bytes: 1024,
76            max_reserved_directive_params: 16,
77        }
78    }
79}