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
use wast::parser::{Parse, Parser, Result};

use crate::{Index, TypeUse};

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ImportDesc<'a> {
    Func(ImportDescFunc<'a>),
}

impl<'a> Parse<'a> for ImportDesc<'a> {
    fn parse(parser: Parser<'a>) -> Result<Self> {
        let mut l = parser.lookahead1();

        if l.peek::<wast::kw::func>() {
            Ok(Self::Func(parser.parse::<ImportDescFunc>()?))
        } else {
            Err(l.error())
        }
    }
}

/// https://webassembly.github.io/spec/core/text/modules.html#text-importdesc
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ImportDescFunc<'a> {
    pub id: Option<Index<'a>>,
    pub type_use: TypeUse<'a>,
}

impl<'a> Parse<'a> for ImportDescFunc<'a> {
    fn parse(parser: Parser<'a>) -> Result<Self> {
        parser.parse::<wast::kw::func>()?;

        let id = parser.parse::<Option<Index>>()?;
        let type_use = parser.parse::<TypeUse>()?;

        Ok(Self { id, type_use })
    }
}