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///     strict_indentation: true,
11///     flow_nesting_limit: 512,
12///     block_nesting_limit: 256,
13/// };
14///
15/// assert!(options.strict_indentation);
16/// assert_eq!(options.flow_nesting_limit, 512);
17/// assert_eq!(options.block_nesting_limit, 256);
18/// ```
19#[non_exhaustive]
20#[derive(Clone, Debug, Eq, PartialEq)]
21pub struct Options {
22    /// Whether to enforce strict YAML indentation rules for flow collections, scalar
23    /// continuations, and content following explicit-key anchor or tag properties.
24    ///
25    /// The default parsing is compatible with `PyYAML` and `ruamel.yaml`.
26    /// `true` sets stricter indentation rules as required by the YAML specification,
27    /// some non-compliant YAML that passes with the two libraries above will fail.
28    /// Default is false.
29    pub strict_indentation: bool,
30    /// Whether scanners emit comment tokens and parsers emit comment events.
31    ///
32    /// The default is `true` with the `comments` Cargo feature and `false` without it.
33    /// When this is `false`, comments are still recognized and validated as YAML syntax, but their
34    /// text is not captured and no comment tokens or events are emitted.
35    /// Comment bytes are still consumed, so this is not an input-size or processing-time limit.
36    /// This option is always available, but has no effect without the `comments` Cargo feature.
37    #[cfg_attr(
38        feature = "comments",
39        doc = "[`Self::max_buffered_comment_events`] has no effect while comment emission is disabled."
40    )]
41    pub emit_comments: bool,
42    /// Maximum number of consecutive comment events buffered while resolving an ambiguous
43    /// collection entry.
44    ///
45    /// The default is 96. A value of zero rejects the first comment that would need buffering.
46    /// This option is available only with the `comments` Cargo feature.
47    #[cfg(feature = "comments")]
48    pub max_buffered_comment_events: usize,
49    /// Maximum number of characters inspected while resolving a simple key.
50    ///
51    /// The default is 1024, matching YAML's simple-key length restriction. A key at exactly this
52    /// limit is accepted. Lower values impose a stricter resource limit; higher values relax that
53    /// YAML restriction.
54    pub simple_key_max_lookahead: usize,
55    /// Maximum number of simultaneously nested flow collections.
56    ///
57    /// The default is 255. A value of zero rejects the first flow collection opener.
58    pub flow_nesting_limit: usize,
59    /// Maximum number of simultaneously nested block collections parsed, or block indentation
60    /// levels retained by a scanner used directly.
61    ///
62    /// The default is 255. A value of zero rejects the first block sequence or mapping. This
63    /// bounds both indentation state retained while scanning and the number of closing tokens
64    /// queued when nested block collections end together. Parsers also count indentless block
65    /// sequences, which do not add scanner indentation state.
66    pub block_nesting_limit: usize,
67    /// Maximum number of UTF-8 source bytes retained for a directive name and payload.
68    ///
69    /// The default is 1024. Separating blanks before retained values count toward the limit.
70    /// `%YAML` version components instead have a fixed nine-digit limit and are stored as integers.
71    /// Real directives are far shorter than the default.
72    pub max_directive_bytes: usize,
73    /// Maximum number of parameters retained for a single reserved directive.
74    ///
75    /// The default is 16. The parser ignores reserved directives, so their parameters only reach
76    /// code driving [`crate::Scanner`] directly. A value of zero rejects the first parameter.
77    pub max_reserved_directive_params: usize,
78}
79
80impl Options {
81    /// Controls whether scanners emit comment tokens and parsers emit comment events.
82    ///
83    /// When emission is disabled, comments are still recognized and validated, but their text
84    /// is not captured. This sets [`Self::emit_comments`] in every build, but has no effect on
85    /// parsing or scanning without the `comments` Cargo feature.
86    ///
87    /// This method is always available so downstream crates can disable comment emission
88    /// even when another dependency enables the `comments` feature through feature unification.
89    pub fn set_emit_comments(&mut self, emit_comments: bool) {
90        self.emit_comments = emit_comments;
91    }
92}
93
94impl Default for Options {
95    fn default() -> Self {
96        Self {
97            strict_indentation: false,
98            emit_comments: cfg!(feature = "comments"),
99            #[cfg(feature = "comments")]
100            max_buffered_comment_events: 96,
101            simple_key_max_lookahead: 1024,
102            flow_nesting_limit: 255,
103            block_nesting_limit: 255,
104            max_directive_bytes: 1024,
105            max_reserved_directive_params: 16,
106        }
107    }
108}