wit_text/ast/
external.rs

1use crate::ast::{self, kw};
2use wast::parser::{Parse, Parser, Result};
3
4/// An imported function declaration using wasm interface types.
5pub struct Import<'a> {
6    /// Where this `import` was defined.
7    pub span: wast::Span,
8    /// The name of this import to refer to
9    pub id: Option<wast::Id<'a>>,
10    /// Where this was imported from
11    pub module: &'a str,
12    /// What is being imported
13    pub name: &'a str,
14    /// The type signature of the function being imported.
15    pub ty: ast::TypeUse<'a>,
16}
17
18impl<'a> Parse<'a> for Import<'a> {
19    fn parse(parser: Parser<'a>) -> Result<Import<'a>> {
20        let span = parser.parse::<kw::import>()?.0;
21        let module = parser.parse()?;
22        let name = parser.parse()?;
23        let (id, ty) = parser.parens(|parser| {
24            parser.parse::<kw::func>()?;
25            Ok((parser.parse()?, parser.parse()?))
26        })?;
27        Ok(Import {
28            span,
29            module,
30            name,
31            id,
32            ty,
33        })
34    }
35}
36
37/// An exported wasm interface types function
38pub struct Export<'a> {
39    /// The function being exported
40    pub func: wast::Index<'a>,
41    /// The name we're exporting under
42    pub name: &'a str,
43}
44
45impl<'a> Parse<'a> for Export<'a> {
46    fn parse(parser: Parser<'a>) -> Result<Export<'a>> {
47        parser.parse::<kw::export>()?;
48        let name = parser.parse()?;
49        let func = parser.parens(|parser| {
50            parser.parse::<kw::func>()?;
51            parser.parse()
52        })?;
53        Ok(Export { name, func })
54    }
55}