Skip to main content

latexml_core/binding/def/
options.rs

1//! The option keys each definer accepts — Perl `Package.pm`'s `$*_options` tables — and
2//! `check_options`, Perl's `CheckOptions` (Package.pm:1062-1067). The runtime (Rhai) front-end
3//! checks every option bag against these; the compile-time macros check theirs at expansion.
4
5/// `DefMacro` (Package.pm:1136-1140, `$macro_options`).
6pub const MACRO_OPTIONS: &[&str] = &[
7  "scope",
8  "locked",
9  "mathactive",
10  "protected",
11  "robust",
12  "outer",
13  "long",
14  "nopackParameters",
15];
16
17/// `DefParameterType` (Package.pm:581-584, `$parameter_options`).
18pub const PARAMETER_OPTIONS: &[&str] = &[
19  "nargs",
20  "reversion",
21  "optional",
22  "novalue",
23  "beforeDigest",
24  "afterDigest",
25  "semiverbatim",
26  "undigested",
27];
28
29/// `DefRegister` (Package.pm:1325-1326, `$register_options`).
30pub const REGISTER_OPTIONS: &[&str] = &["readonly", "getter", "setter", "address", "allocate"];
31
32/// `DefConditional` (Package.pm:1185, `$conditional_options`).
33pub const CONDITIONAL_OPTIONS: &[&str] = &["scope", "locked", "skipper"];
34
35/// `DefPrimitive` (Package.pm:1277-1283, `$primitive_options`).
36pub const PRIMITIVE_OPTIONS: &[&str] = &[
37  "isPrefix",
38  "scope",
39  "font",
40  "mode",
41  "enterHorizontal",
42  "leaveHorizontal",
43  "requireMath",
44  "forbidMath",
45  "beforeDigest",
46  "afterDigest",
47  "bounded",
48  "locked",
49  "alias",
50  "robust",
51  "outer",
52  "long",
53];
54
55/// `DefConstructor` (Package.pm:1432-1440, `$constructor_options`).
56pub const CONSTRUCTOR_OPTIONS: &[&str] = &[
57  "mode",
58  "requireMath",
59  "forbidMath",
60  "font",
61  "enterHorizontal",
62  "leaveHorizontal",
63  "alias",
64  "reversion",
65  "sizer",
66  "properties",
67  "nargs",
68  "attributeForm",
69  "beforeDigest",
70  "afterDigest",
71  "beforeConstruct",
72  "afterConstruct",
73  "captureBody",
74  "scope",
75  "bounded",
76  "locked",
77  "outer",
78  "long",
79  "robust",
80];
81
82/// `DefEnvironment` (Package.pm:1880-1888, `$environment_options`).
83pub const ENVIRONMENT_OPTIONS: &[&str] = &[
84  "mode",
85  "requireMath",
86  "forbidMath",
87  "enterHorizontal",
88  "leaveHorizontal",
89  "properties",
90  "nargs",
91  "font",
92  "beforeDigest",
93  "afterDigest",
94  "afterDigestBegin",
95  "beforeDigestEnd",
96  "afterDigestBody",
97  "beforeConstruct",
98  "afterConstruct",
99  "reversion",
100  "sizer",
101  "scope",
102  "locked",
103  "attributeForm",
104];
105
106/// `DefMath` (Package.pm:1591-1601, `$math_options`).
107pub const MATH_OPTIONS: &[&str] = &[
108  "name",
109  "meaning",
110  "omcd",
111  "reversion",
112  "sizer",
113  "alias",
114  "role",
115  "operator_role",
116  "reorder",
117  "dual",
118  "decl_id",
119  "mathstyle",
120  "font",
121  "scriptpos",
122  "operator_scriptpos",
123  "stretchy",
124  "operator_stretchy",
125  "lpadding",
126  "rpadding",
127  "beforeDigest",
128  "afterDigest",
129  "scope",
130  "nogroup",
131  "locked",
132  "protected",
133  "robust",
134  "revert_as",
135  "hide_content_reversion",
136];
137
138/// Perl `CheckOptions($operation, $allowed, %options)`: one `Error:misdefined` naming every key
139/// `allowed` lacks. Returns whether all keys were allowed (`Err` only when the error cap escalates); the definition proceeds either way, as
140/// in Perl, with the rejected keys ignored.
141pub fn check_options<'a>(
142  operation: &str,
143  allowed: &[&str],
144  keys: impl IntoIterator<Item = &'a str>,
145) -> crate::common::error::Result<bool> {
146  let bad: Vec<&str> = keys.into_iter().filter(|k| !allowed.contains(k)).collect();
147  if bad.is_empty() {
148    return Ok(true);
149  }
150  crate::Error!(
151    "misdefined",
152    operation,
153    format!("{operation} does not accept options:{}", bad.join(", "))
154  );
155  Ok(false)
156}
157
158/// A key Perl accepts whose effect the core cannot carry yet: said, never silently dropped.
159pub fn unsupported_option(operation: &str, key: &str) {
160  crate::Warn!(
161    "unsupported",
162    operation,
163    format!("option {key} is not supported yet; ignored")
164  );
165}