Skip to content

Commit f9bf444

Browse files
committed
Implement a builder pattern for TexEngine
This way we can guarantee that the globals are always set.
1 parent 5c628bf commit f9bf444

File tree

5 files changed

+67
-55
lines changed

5 files changed

+67
-55
lines changed

src/cli_driver.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -638,11 +638,10 @@ impl ProcessingSession {
638638

639639
let result = {
640640
let mut stack = self.io.as_stack(false);
641-
let mut engine = TexEngine::new();
642-
engine.set_halt_on_error_mode(true);
643-
engine.set_initex_mode(true);
644-
engine.set_synctex(false);
645-
engine.process(&mut stack, &mut self.events, status, "UNUSED.fmt.gz",
641+
TexEngine::new()
642+
.halt_on_error_mode(true)
643+
.initex_mode(true)
644+
.process(&mut stack, &mut self.events, status, "UNUSED.fmt.gz",
646645
&format!("tectonic-format-{}.tex", stem))
647646
};
648647

@@ -700,17 +699,18 @@ impl ProcessingSession {
700699
fn tex_pass(&mut self, rerun_explanation: Option<&str>, status: &mut TermcolorStatusBackend) -> Result<i32> {
701700
let result = {
702701
let mut stack = self.io.as_stack(true);
703-
let mut engine = TexEngine::new();
704-
engine.set_halt_on_error_mode(true);
705-
engine.set_initex_mode(self.output_format == OutputFormat::Format);
706-
engine.set_synctex(self.synctex_enabled);
707702
if let Some(s) = rerun_explanation {
708703
status.note_highlighted("Rerunning ", "TeX", &format!(" because {} ...", s));
709704
} else {
710705
status.note_highlighted("Running ", "TeX", " ...");
711706
}
712-
engine.process(&mut stack, &mut self.events, status,
713-
&self.format_path, &self.tex_path)
707+
708+
TexEngine::new()
709+
.halt_on_error_mode(true)
710+
.initex_mode(self.output_format == OutputFormat::Format)
711+
.synctex(self.synctex_enabled)
712+
.process(&mut stack, &mut self.events, status,
713+
&self.format_path, &self.tex_path)
714714
};
715715

716716
match result {

src/engines/tex.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,39 @@ pub enum TexResult {
2121
Errors,
2222
}
2323

24-
24+
#[derive(Debug,Default)]
2525
pub struct TexEngine {
2626
// One day, the engine will hold its own state. For the time being,
2727
// though, it's just a proxy for the global constants in the C code.
28+
29+
halt_on_error: bool,
30+
initex_mode: bool,
31+
synctex_enabled: bool,
2832
}
2933

3034

3135
impl TexEngine {
3236
pub fn new () -> TexEngine {
33-
TexEngine {}
37+
TexEngine::default()
3438
}
3539

36-
pub fn set_halt_on_error_mode (&mut self, halt_on_error: bool) {
37-
let v = if halt_on_error { 1 } else { 0 };
38-
unsafe { super::tt_set_int_variable(b"halt_on_error_p\0".as_ptr(), v); }
40+
pub fn halt_on_error_mode (&mut self, halt_on_error: bool) -> &mut Self {
41+
self.halt_on_error = halt_on_error;
42+
self
3943
}
4044

4145
/// Configure the engine to run in "initex" mode, in which it generates a
4246
/// "format" file that serializes the engine state rather than a PDF
4347
/// document.
44-
pub fn set_initex_mode (&mut self, initex: bool) {
45-
let v = if initex { 1 } else { 0 };
46-
unsafe { super::tt_set_int_variable(b"in_initex_mode\0".as_ptr(), v); }
48+
pub fn initex_mode (&mut self, initex: bool) -> &mut Self {
49+
self.initex_mode = initex;
50+
self
4751
}
4852

49-
pub fn set_synctex (&mut self, synctex_enabled: bool) {
50-
let v = if synctex_enabled { 1 } else { 0 };
51-
unsafe { super::tt_set_int_variable(b"synctex_enabled\0".as_ptr(), v); }
53+
/// Configure the engine to produce SyncTeX data.
54+
pub fn synctex (&mut self, synctex_enabled: bool) -> &mut Self {
55+
self.synctex_enabled = synctex_enabled;
56+
self
5257
}
5358

5459
// This function can't be generic across the IoProvider trait, for now,
@@ -65,6 +70,14 @@ impl TexEngine {
6570
let /*mut*/ state = ExecutionState::new(io, events, status);
6671
let bridge = TectonicBridgeApi::new(&state);
6772

73+
// initialize globals
74+
let v = if self.halt_on_error { 1 } else { 0 };
75+
unsafe { super::tt_set_int_variable(b"halt_on_error_p\0".as_ptr(), v); }
76+
let v = if self.initex_mode { 1 } else { 0 };
77+
unsafe { super::tt_set_int_variable(b"in_initex_mode\0".as_ptr(), v); }
78+
let v = if self.synctex_enabled { 1 } else { 0 };
79+
unsafe { super::tt_set_int_variable(b"synctex_enabled\0".as_ptr(), v); }
80+
6881
unsafe {
6982
match super::tex_simple_main(&bridge, cformat.as_ptr(), cinput.as_ptr()) {
7083
0 => Ok(TexResult::Spotless),

tests/formats.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ fn test_format_generation(subdir: &str, texname: &str, fmtname: &str, sha256: &s
111111
])
112112
};
113113

114-
let mut e = TexEngine::new();
115-
e.set_initex_mode(true);
116-
e.process(&mut io, &mut events,
117-
&mut NoopStatusBackend::new(), "unused.fmt.gz", texname).unwrap();
114+
TexEngine::new()
115+
.initex_mode(true)
116+
.process(&mut io, &mut events,
117+
&mut NoopStatusBackend::new(), "unused.fmt.gz", texname).unwrap();
118118
}
119119

120120
// Did we get what we expected?

tests/tex-outputs.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ fn set_up_format_file(tests_dir: &Path) -> Result<SingleInputFileIo> {
4444
&mut fs,
4545
]);
4646

47-
let mut e = TexEngine::new();
48-
e.set_halt_on_error_mode(true);
49-
e.set_initex_mode(true);
50-
e.process(&mut io, &mut NoopIoEventBackend::new(),
51-
&mut NoopStatusBackend::new(), "UNUSED.fmt.gz", "plain.tex")?;
47+
let mut e = TexEngine::new()
48+
.halt_on_error_mode(true)
49+
.initex_mode(true)
50+
.process(&mut io, &mut NoopIoEventBackend::new(),
51+
&mut NoopStatusBackend::new(), "UNUSED.fmt.gz", "plain.tex")?;
5252
}
5353

5454
let mut fmt_file = File::create(&fmt_path)?;
@@ -131,10 +131,9 @@ fn do_one(stem: &str, check_synctex: bool) {
131131
&mut tex,
132132
&mut fmt,
133133
]);
134-
let mut e = TexEngine::new();
135-
e.set_initex_mode(false); // TODO: this shouldn't be necessary
136-
e.process(&mut io, &mut NoopIoEventBackend::new(),
137-
&mut NoopStatusBackend::new(), "plain.fmt.gz", &texname).unwrap();
134+
TexEngine::new()
135+
.process(&mut io, &mut NoopIoEventBackend::new(),
136+
&mut NoopStatusBackend::new(), "plain.fmt.gz", &texname).unwrap();
138137
}
139138

140139
// Check that log and xdv match expectations.

tests/trip.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ fn trip_test() {
118118
&mut tex,
119119
&mut tfm,
120120
]);
121-
let mut e = TexEngine::new();
122-
e.set_halt_on_error_mode(false);
123-
e.set_initex_mode(true);
124-
e.process(&mut io, &mut NoopIoEventBackend::new(),
125-
&mut NoopStatusBackend::new(), "INITEX", "trip").unwrap();
121+
TexEngine::new()
122+
.halt_on_error_mode(false)
123+
.initex_mode(true)
124+
.process(&mut io, &mut NoopIoEventBackend::new(),
125+
&mut NoopStatusBackend::new(), "INITEX", "trip").unwrap();
126126
}
127127

128128
// Second pass -- process it
@@ -132,11 +132,11 @@ fn trip_test() {
132132
&mut tex,
133133
&mut tfm,
134134
]);
135-
let mut e = TexEngine::new();
136-
e.set_halt_on_error_mode(false);
137-
e.set_initex_mode(false);
138-
e.process(&mut io, &mut NoopIoEventBackend::new(),
139-
&mut NoopStatusBackend::new(), "trip.fmt.gz", "trip").unwrap();
135+
TexEngine::new()
136+
.halt_on_error_mode(false)
137+
.initex_mode(false)
138+
.process(&mut io, &mut NoopIoEventBackend::new(),
139+
&mut NoopStatusBackend::new(), "trip.fmt.gz", "trip").unwrap();
140140
}
141141

142142
// Check that outputs match expectations.
@@ -183,11 +183,11 @@ fn etrip_test() {
183183
&mut tex,
184184
&mut tfm,
185185
]);
186-
let mut e = TexEngine::new();
187-
e.set_halt_on_error_mode(false);
188-
e.set_initex_mode(true);
189-
e.process(&mut io, &mut NoopIoEventBackend::new(),
190-
&mut NoopStatusBackend::new(), "INITEX", "etrip").unwrap();
186+
TexEngine::new()
187+
.halt_on_error_mode(false)
188+
.initex_mode(true)
189+
.process(&mut io, &mut NoopIoEventBackend::new(),
190+
&mut NoopStatusBackend::new(), "INITEX", "etrip").unwrap();
191191
}
192192

193193
// Second pass -- process it
@@ -197,11 +197,11 @@ fn etrip_test() {
197197
&mut tex,
198198
&mut tfm,
199199
]);
200-
let mut e = TexEngine::new();
201-
e.set_halt_on_error_mode(false);
202-
e.set_initex_mode(false);
203-
e.process(&mut io, &mut NoopIoEventBackend::new(),
204-
&mut NoopStatusBackend::new(), "etrip.fmt.gz", "etrip").unwrap();
200+
TexEngine::new()
201+
.halt_on_error_mode(false)
202+
.initex_mode(false)
203+
.process(&mut io, &mut NoopIoEventBackend::new(),
204+
&mut NoopStatusBackend::new(), "etrip.fmt.gz", "etrip").unwrap();
205205
}
206206

207207
// Check that outputs match expectations.

0 commit comments

Comments
 (0)