texform_core/parse/config.rs
1//! User-facing parse configuration.
2
3/// Parse-time configuration knobs.
4///
5/// `ParseConfig` is plain data describing how a single parse call should
6/// behave. It is independent of [`ParseContext`](super::ParseContext), which
7/// owns the knowledge base; the same context can be reused across many calls
8/// with different configs.
9///
10/// Two orthogonal axes control parsing, both with **`true` = stricter**:
11///
12/// - [`reject_unknown`](Self::reject_unknown): unknown command/environment names
13/// become diagnostics (`true`) or `known: false` nodes (`false`).
14/// - [`abort_on_error`](Self::abort_on_error): stop at the first error (`true`)
15/// or continue parsing to collect every diagnostic (`false`, slower).
16/// Recovery may return a read-only document containing `Error` nodes; use
17/// [`ParseResult::try_into_document`](super::ParseResult::try_into_document)
18/// when downstream code requires a complete tree.
19///
20/// Named extremes [`STRICT`](Self::STRICT) and [`LENIENT`](Self::LENIENT) cover
21/// the two corners where both axes agree. For mixed settings, use struct-update
22/// syntax, e.g. `ParseConfig { reject_unknown: true, ..Default::default() }`.
23#[derive(Clone, Debug, PartialEq, Eq)]
24pub struct ParseConfig {
25 /// When `true`, unknown command/environment names become diagnostics
26 /// instead of being preserved as `known: false` nodes.
27 ///
28 /// This controls **unknown-name handling only**, not general error strictness.
29 pub reject_unknown: bool,
30 /// When `true`, parsing stops at the first error in each content item and
31 /// may return no document for that item.
32 ///
33 /// When `false`, the parser uses recovery fallbacks to collect every
34 /// diagnostic (useful for IDEs and playgrounds, but slower on large corpora)
35 /// and may attach `Error` nodes to the returned document. A document with
36 /// error nodes is read-only and cannot be used by transform entry points.
37 pub abort_on_error: bool,
38 /// Hard upper bound on nested `{ ... }` brace group depth.
39 pub max_group_depth: usize,
40}
41
42impl ParseConfig {
43 /// Default hard upper bound on nested `{ ... }` brace group depth.
44 pub const DEFAULT_MAX_GROUP_DEPTH: usize = 128;
45
46 /// Both axes strict: `reject_unknown = true`, `abort_on_error = true`.
47 ///
48 /// Used by normalization and other correctness-sensitive paths.
49 pub const STRICT: Self = Self {
50 reject_unknown: true,
51 abort_on_error: true,
52 max_group_depth: Self::DEFAULT_MAX_GROUP_DEPTH,
53 };
54
55 /// Both axes lenient: `reject_unknown = false`, `abort_on_error = false`.
56 ///
57 /// Default for standalone parse-only entry points and interactive tools.
58 pub const LENIENT: Self = Self {
59 reject_unknown: false,
60 abort_on_error: false,
61 max_group_depth: Self::DEFAULT_MAX_GROUP_DEPTH,
62 };
63}
64
65impl Default for ParseConfig {
66 fn default() -> Self {
67 Self::LENIENT
68 }
69}