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
use crate::component::Component;
use crate::core::{Module, ModuleField, ModuleKind};
use crate::kw;
use crate::parser::{Parse, Parser, Result};
use crate::token::Span;
#[derive(Debug)]
#[allow(missing_docs)]
pub enum Wat<'a> {
Module(Module<'a>),
Component(Component<'a>),
}
impl Wat<'_> {
fn validate(&self, parser: Parser<'_>) -> Result<()> {
match self {
Wat::Module(m) => m.validate(parser),
Wat::Component(c) => c.validate(parser),
}
}
pub fn encode(&mut self) -> std::result::Result<Vec<u8>, crate::Error> {
match self {
Wat::Module(m) => m.encode(),
Wat::Component(c) => c.encode(),
}
}
}
impl<'a> Parse<'a> for Wat<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
if !parser.has_meaningful_tokens() {
return Err(parser.error("expected at least one module field"));
}
let _r = parser.register_annotation("custom");
let wat = if parser.peek2::<kw::module>() {
Wat::Module(parser.parens(|parser| parser.parse())?)
} else if parser.peek2::<kw::component>() {
Wat::Component(parser.parens(|parser| parser.parse())?)
} else {
let fields = ModuleField::parse_remaining(parser)?;
Wat::Module(Module {
span: Span { offset: 0 },
id: None,
name: None,
kind: ModuleKind::Text(fields),
})
};
wat.validate(parser)?;
Ok(wat)
}
}