forked from bytecodealliance/wasm-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
60 lines (46 loc) · 1.47 KB
/
Copy pathlib.rs
File metadata and controls
60 lines (46 loc) · 1.47 KB
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
wit_bindgen::generate!();
struct Component;
struct StringWriter(pub Vec<PrintPart>);
impl wasmprinter::Print for StringWriter {
fn write_str(&mut self, s: &str) -> std::io::Result<()> {
self.0.push(PrintPart::Str(s.to_string()));
Ok(())
}
fn start_name(&mut self) -> std::io::Result<()> {
self.0.push(PrintPart::Name);
Ok(())
}
fn start_literal(&mut self) -> std::io::Result<()> {
self.0.push(PrintPart::Literal);
Ok(())
}
fn start_keyword(&mut self) -> std::io::Result<()> {
self.0.push(PrintPart::Keyword);
Ok(())
}
fn start_type(&mut self) -> std::io::Result<()> {
self.0.push(PrintPart::Type);
Ok(())
}
fn start_comment(&mut self) -> std::io::Result<()> {
self.0.push(PrintPart::Comment);
Ok(())
}
fn reset_color(&mut self) -> std::io::Result<()> {
self.0.push(PrintPart::Reset);
Ok(())
}
}
impl Guest for Component {
fn parse(contents: String) -> Result<Vec<u8>, String> {
wat::parse_str(contents).map_err(|e| e.to_string())
}
fn print(contents: Vec<u8>, skeleton: bool) -> Result<Vec<PrintPart>, String> {
let mut config = wasmprinter::Config::new();
config.print_skeleton(skeleton);
let mut writer = StringWriter(Vec::new());
let result = config.print(&contents, &mut writer);
result.map(|_| writer.0).map_err(|e| e.to_string())
}
}
export!(Component);