Debian Codegraph @

playground/component/src/lib.rs

raw
1 #[allow(warnings)]
2 mod bindings;
3
4 use bindings::{Guest, PrintPart};
5
6 struct Component;
7
8 struct StringWriter(pub Vec<PrintPart>);
9
10 impl wasmprinter::Print for StringWriter {
11 fn write_str(&mut self, s: &str) -> std::io::Result<()> {
12 self.0.push(PrintPart::Str(s.to_string()));
13 Ok(())
14 }
15
16 fn start_name(&mut self) -> std::io::Result<()> {
17 self.0.push(PrintPart::Name);
18 Ok(())
19 }
20
21 fn start_literal(&mut self) -> std::io::Result<()> {
22 self.0.push(PrintPart::Literal);
23 Ok(())
24 }
25
26 fn start_keyword(&mut self) -> std::io::Result<()> {
27 self.0.push(PrintPart::Keyword);
28 Ok(())
29 }
30
31 fn start_type(&mut self) -> std::io::Result<()> {
32 self.0.push(PrintPart::Type);
33 Ok(())
34 }
35
36 fn start_comment(&mut self) -> std::io::Result<()> {
37 self.0.push(PrintPart::Comment);
38 Ok(())
39 }
40
41 fn reset_color(&mut self) -> std::io::Result<()> {
42 self.0.push(PrintPart::Reset);
43 Ok(())
44 }
45 }
46
47 impl Guest for Component {
48 fn parse(contents: String) -> Result<Vec<u8>, String> {
49 wat::parse_str(contents).map_err(|e| e.to_string())
50 }
51
52 fn print(contents: Vec<u8>, skeleton: bool) -> Result<Vec<PrintPart>, String> {
53 let mut config = wasmprinter::Config::new();
54 config.print_skeleton(skeleton);
55
56 let mut writer = StringWriter(Vec::new());
57 let result = config.print(&contents, &mut writer);
58
59 result.map(|_| writer.0).map_err(|e| e.to_string())
60 }
61 }
62
63 bindings::export!(Component with_types_in bindings);