Skip to content

Commit 92f866c

Browse files
committed
feat: Add detailed rant compiler messages to console log in case of compiler errors
1 parent 84d154a commit 92f866c

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

src/lib.rs

+35-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
use std::fmt;
22

3-
use rant::{compiler::CompilerErrorKind, runtime::RuntimeError, Rant, RantOptions, RantValue};
3+
use rant::{
4+
compiler::{CompilerErrorKind, CompilerMessage},
5+
runtime::RuntimeError,
6+
Rant, RantOptions, RantValue,
7+
};
48
use wasm_bindgen::prelude::*;
59

610
#[wasm_bindgen]
@@ -18,25 +22,51 @@ fn _rant(input: &str, seed: u32) -> Result<RantValue, RantError> {
1822
..Default::default()
1923
};
2024
let mut rant = Rant::with_options(options);
21-
let program = rant.compile_quiet(input).map_err(RantError::Compiler)?;
22-
rant.run(&program).map_err(RantError::Runtime)
25+
let mut msgs: Vec<CompilerMessage> = vec![];
26+
let program = rant.compile(input, &mut msgs);
27+
match program {
28+
Ok(p) => rant.run(&p).map_err(RantError::Runtime),
29+
Err(err) => Err(RantError::Compiler(CompilerErrorWithMsgs { err, msgs })),
30+
}
2331
}
2432

2533
#[derive(Debug)]
2634
enum RantError {
27-
Compiler(CompilerErrorKind),
35+
Compiler(CompilerErrorWithMsgs),
2836
Runtime(RuntimeError),
2937
}
3038

3139
impl fmt::Display for RantError {
3240
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3341
match *self {
34-
RantError::Compiler(ref err) => write!(f, "Rant Compile error: {}", err),
42+
RantError::Compiler(ref err) => write!(f, "{}", err),
3543
RantError::Runtime(ref err) => write!(f, "Rant Runtime error: {}", err),
3644
}
3745
}
3846
}
3947

48+
#[derive(Debug)]
49+
struct CompilerErrorWithMsgs {
50+
err: CompilerErrorKind,
51+
msgs: Vec<CompilerMessage>,
52+
}
53+
54+
impl fmt::Display for CompilerErrorWithMsgs {
55+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
56+
let msgs = self
57+
.msgs
58+
.iter()
59+
.map(|msg| format!("{:#?}", msg))
60+
.collect::<Vec<_>>()
61+
.join("\n");
62+
write!(
63+
f,
64+
"Rant Compiler error: {}\nCompiler messages:\n{}",
65+
self.err, msgs
66+
)
67+
}
68+
}
69+
4070
#[cfg(test)]
4171
pub mod tests {
4272
use super::_rant;

src/processor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export abstract class BaseRantProcessor extends MarkdownRenderChild {
2626
try {
2727
this.result = rant(input, seed);
2828
} catch (error) {
29-
this.result = error;
29+
this.result = error.split("\n")[0] + " (see console for details)";
3030
console.error(error);
3131
}
3232
}

0 commit comments

Comments
 (0)