latexml_core/binding/def/
builder.rs1use super::dialect::{def_constructor, def_environment};
19use crate::{
20 common::{
21 def_parser::{parse_parameters, parse_prototype},
22 error::{Error, Result},
23 },
24 definition::{
25 BeforeDigestClosure, ConstructionClosure, DigestionClosure, FontDirective, PropertiesClosure,
26 ReplacementClosure, Reversion, constructor::ConstructorOptions,
27 },
28 parameter::Parameters,
29 token::Token,
30 util::text::{Delimiter, extract_bracketed},
31};
32
33macro_rules! shared_hook_setters {
37 () => {
38 pub fn after_digest(mut self, hook: DigestionClosure) -> Self {
41 self.options.after_digest.push(hook);
42 self
43 }
44
45 pub fn after_digest_begin(mut self, hook: DigestionClosure) -> Self {
48 self.options.after_digest_begin.push(hook);
49 self
50 }
51
52 pub fn after_digest_body(mut self, hook: DigestionClosure) -> Self {
55 self.options.after_digest_body.push(hook);
56 self
57 }
58
59 pub fn before_digest(mut self, hook: BeforeDigestClosure) -> Self {
62 self.options.before_digest.push(hook);
63 self
64 }
65
66 pub fn before_digest_end(mut self, hook: BeforeDigestClosure) -> Self {
68 self.options.before_digest_end.push(hook);
69 self
70 }
71
72 pub fn before_construct(mut self, hook: ConstructionClosure) -> Self {
74 self.options.before_construct.push(hook);
75 self
76 }
77
78 pub fn after_construct(mut self, hook: ConstructionClosure) -> Self {
80 self.options.after_construct.push(hook);
81 self
82 }
83
84 pub fn properties(mut self, props: PropertiesClosure) -> Self {
87 self.options.properties = props;
88 self
89 }
90
91 pub fn reversion(mut self, rev: Reversion) -> Self {
93 self.options.reversion = Some(rev);
94 self
95 }
96
97 pub fn font(mut self, font: FontDirective) -> Self {
99 self.options.font = Some(font);
100 self
101 }
102
103 pub fn sizer(mut self, sizer: crate::definition::SizingClosure) -> Self {
105 self.options.sizer = Some(sizer);
106 self
107 }
108 };
109}
110
111pub enum OptionValue {
115 Str(String),
116 Bool(bool),
117 Int(i64),
118}
119
120impl OptionValue {
121 fn into_string(self) -> Result<String> {
122 match self {
123 OptionValue::Str(s) => Ok(s),
124 _ => Err(Error::from("constructor option expected a string value")),
125 }
126 }
127
128 pub fn into_bool(self) -> Result<bool> {
133 match self {
134 OptionValue::Bool(b) => Ok(b),
135 OptionValue::Int(i) => Ok(i != 0),
136 OptionValue::Str(s) => Ok(!s.is_empty()),
137 }
138 }
139}
140
141pub struct ConstructorBuilder {
143 cs: Token,
144 paramlist: Option<Parameters>,
145 replacement: Option<ReplacementClosure>,
146 options: ConstructorOptions,
147}
148
149impl ConstructorBuilder {
150 pub fn new(proto: &str) -> Result<Self> {
152 let (cs, paramlist) = parse_prototype(proto, true)?;
153 Ok(Self {
154 cs,
155 paramlist,
156 replacement: None,
157 options: ConstructorOptions::default(),
158 })
159 }
160
161 pub fn from_cs(cs: Token, paramlist: Option<Parameters>) -> Self {
163 Self {
164 cs,
165 paramlist,
166 replacement: None,
167 options: ConstructorOptions::default(),
168 }
169 }
170
171 pub fn replacement(mut self, repl: ReplacementClosure) -> Self {
173 self.replacement = Some(repl);
174 self
175 }
176
177 pub fn set_option(mut self, key: &str, value: OptionValue) -> Result<Self> {
180 apply_scalar_option(&mut self.options, key, value)?;
181 Ok(self)
182 }
183
184 shared_hook_setters!();
185
186 pub fn install(self) -> Result<()> {
188 def_constructor(self.cs, self.paramlist, self.replacement, self.options);
189 Ok(())
190 }
191}
192
193fn apply_scalar_option(
199 options: &mut ConstructorOptions,
200 key: &str,
201 value: OptionValue,
202) -> Result<()> {
203 match key {
204 "mode" => options.mode = Some(value.into_string()?),
205 "bounded" => options.bounded = value.into_bool()?,
206 "requireMath" => options.require_math = value.into_bool()?,
207 "forbidMath" => options.forbid_math = value.into_bool()?,
208 "enterHorizontal" => options.enter_horizontal = value.into_bool()?,
209 "leaveHorizontal" => options.leave_horizontal = value.into_bool()?,
210 "captureBody" => options.capture_body = value.into_bool()?,
211 "alias" => options.alias = Some(value.into_string()?),
212 "locked" => options.locked = value.into_bool()?,
213 "robust" => options.robust = value.into_bool()?,
214 "scope" => options.scope = super::traits::IntoOption::into_option(value.into_string()?),
216 "nargs" => match value {
217 OptionValue::Int(n) => options.nargs = usize::try_from(n).ok(),
218 _ => return Err(Error::from("nargs expects an integer")),
219 },
220 _ => return Err(Error::from(format!("unknown constructor option {key}"))),
222 }
223 Ok(())
224}
225
226pub struct EnvironmentBuilder {
231 name: String,
232 paramlist: Option<Parameters>,
233 replacement: Option<ReplacementClosure>,
234 options: ConstructorOptions,
235}
236
237impl EnvironmentBuilder {
238 pub fn new(proto: &str) -> Result<Self> {
242 let mut proto = proto.trim_start().to_string();
243 let name = extract_bracketed(&mut proto, Some(&Delimiter::Brace)).ok_or_else(|| {
244 Error::from(format!(
245 "DefEnvironment prototype must start with {{name}}: {proto:?}"
246 ))
247 })?;
248 let paramlist_str = proto.trim_start().to_string();
249 let paramlist = if paramlist_str.is_empty() {
250 None
251 } else {
252 let cs = crate::T_CS!(crate::s!("\\{}", &name));
253 parse_parameters(¶mlist_str, &cs, true)?
254 };
255 Ok(Self {
256 name,
257 paramlist,
258 replacement: None,
259 options: ConstructorOptions::default(),
260 })
261 }
262
263 pub fn replacement(mut self, repl: ReplacementClosure) -> Self {
265 self.replacement = Some(repl);
266 self
267 }
268
269 pub fn set_option(mut self, key: &str, value: OptionValue) -> Result<Self> {
271 apply_scalar_option(&mut self.options, key, value)?;
272 Ok(self)
273 }
274
275 shared_hook_setters!();
276
277 pub fn install(self) -> Result<()> {
279 def_environment(self.name, self.paramlist, self.replacement, self.options);
280 Ok(())
281 }
282}