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
use js_sys::Error;
use wasm_bindgen::prelude::*;
pub struct Browser {
pre: Element,
}
#[wasm_bindgen]
extern "C" {
type HTMLDocument;
static document: HTMLDocument;
#[wasm_bindgen(method, structural)]
fn getElementById(this: &HTMLDocument, id: &str) -> Element;
type Element;
#[wasm_bindgen(method, getter = textContent, structural)]
fn text_content(this: &Element) -> String;
#[wasm_bindgen(method, setter = textContent, structural)]
fn set_text_content(this: &Element, text: &str);
type BrowserError;
#[wasm_bindgen(method, getter, structural)]
fn stack(this: &BrowserError) -> JsValue;
}
impl Browser {
pub fn new() -> Browser {
let pre = document.getElementById("output");
pre.set_text_content("");
Browser { pre }
}
}
impl super::Formatter for Browser {
fn writeln(&self, line: &str) {
let mut html = self.pre.text_content();
html.extend(line.chars().chain(Some('\n')));
self.pre.set_text_content(&html);
}
fn log_test(&self, name: &str, result: &Result<(), JsValue>) {
let s = if result.is_ok() { "ok" } else { "FAIL" };
self.writeln(&format!("test {} ... {}", name, s));
}
fn stringify_error(&self, err: &JsValue) -> String {
let err = Error::from(err.clone());
let name = String::from(err.name());
let message = String::from(err.message());
let err = BrowserError::from(JsValue::from(err));
let stack = err.stack();
let header = format!("{}: {}", name, message);
let stack = match stack.as_string() {
Some(stack) => stack,
None => return header,
};
if stack.contains(&header) {
return stack;
}
format!("{}\n{}", header, stack)
}
}