1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use crate::js_engine::JsValue;
use derive_builder::Builder;
use std::collections::HashMap;
#[non_exhaustive]
#[derive(Clone, Builder, Debug, Default)]
#[builder(default)]
#[builder(setter(into, strip_option))]
pub struct Opts {
display_mode: Option<bool>,
output_type: Option<OutputType>,
leqno: Option<bool>,
fleqn: Option<bool>,
throw_on_error: Option<bool>,
error_color: Option<String>,
macros: HashMap<String, String>,
min_rule_thickness: Option<f64>,
#[allow(clippy::option_option)]
max_size: Option<Option<f64>>,
#[allow(clippy::option_option)]
max_expand: Option<Option<i32>>,
trust: Option<bool>,
}
impl Opts {
pub fn builder() -> OptsBuilder {
OptsBuilder::default()
}
pub fn set_display_mode(&mut self, flag: bool) {
self.display_mode = Some(flag);
}
pub fn set_output_type(&mut self, output_type: OutputType) {
self.output_type = Some(output_type);
}
pub fn set_leqno(&mut self, flag: bool) {
self.leqno = Some(flag);
}
pub fn set_fleqn(&mut self, flag: bool) {
self.fleqn = Some(flag);
}
pub fn set_throw_on_error(&mut self, flag: bool) {
self.throw_on_error = Some(flag);
}
pub fn set_error_color(&mut self, color: String) {
self.error_color = Some(color);
}
pub fn add_macro(&mut self, entry_name: String, entry_data: String) {
self.macros.insert(entry_name, entry_data);
}
pub fn set_min_rule_thickness(&mut self, value: f64) {
self.min_rule_thickness = Some(value);
}
pub fn set_max_size(&mut self, value: Option<f64>) {
self.max_size = Some(value);
}
pub fn set_max_expand(&mut self, value: Option<i32>) {
self.max_expand = Some(value);
}
pub fn set_trust(&mut self, flag: bool) {
self.trust = Some(flag);
}
pub(crate) fn to_js_value<T: JsValue>(&self) -> T {
let mut opt: HashMap<String, T> = HashMap::new();
if let Some(display_mode) = self.display_mode {
opt.insert("displayMode".to_owned(), T::from_bool(display_mode));
}
if let Some(output_type) = self.output_type {
opt.insert(
"output".to_owned(),
T::from_string(
match output_type {
OutputType::Html => "html",
OutputType::Mathml => "mathml",
OutputType::HtmlAndMathml => "htmlAndMathml",
}
.to_owned(),
),
);
}
if let Some(leqno) = self.leqno {
opt.insert("leqno".to_owned(), T::from_bool(leqno));
}
if let Some(fleqn) = self.fleqn {
opt.insert("fleqn".to_owned(), T::from_bool(fleqn));
}
if let Some(throw_on_error) = self.throw_on_error {
opt.insert("throwOnError".to_owned(), T::from_bool(throw_on_error));
}
if let Some(error_color) = &self.error_color {
opt.insert("errorColor".to_owned(), T::from_string(error_color.clone()));
}
if !self.macros.is_empty() {
opt.insert(
"macros".to_owned(),
T::from_object(
self.macros
.iter()
.map(|(k, v)| (k.clone(), T::from_string(v.clone()))),
),
);
}
if let Some(min_rule_thickness) = self.min_rule_thickness {
opt.insert(
"minRuleThickness".to_owned(),
T::from_float(min_rule_thickness),
);
}
if let Some(Some(max_size)) = self.max_size {
opt.insert("maxSize".to_owned(), T::from_float(max_size));
}
if let Some(max_expand) = self.max_expand {
match max_expand {
Some(max_expand) => {
opt.insert("maxExpand".to_owned(), T::from_int(max_expand));
}
None => {
opt.insert("maxExpand".to_owned(), T::from_int(i32::max_value()));
}
}
}
if let Some(trust) = self.trust {
opt.insert("trust".to_owned(), T::from_bool(trust));
}
T::from_object(opt.into_iter())
}
}
impl AsRef<Opts> for Opts {
fn as_ref(&self) -> &Opts {
self
}
}
impl OptsBuilder {
pub fn add_macro(mut self, entry_name: String, entry_data: String) -> Self {
match self.macros.as_mut() {
Some(macros) => {
macros.insert(entry_name, entry_data);
}
None => {
let mut macros = HashMap::new();
macros.insert(entry_name, entry_data);
self.macros = Some(macros);
}
}
self
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum OutputType {
Html,
Mathml,
HtmlAndMathml,
}