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::{Expr, SExpr, Section};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Module {
    sections: Vec<Section>,
}

impl Module {
    pub fn with_sections(sections: Vec<Section>) -> Self {
        Self { sections }
    }
}

impl Parse<'_> for Module {
    fn parse(parser: Parser<'_>) -> Result<Self> {
        parser.parse::<wast::kw::module>()?;

        let mut sections = Vec::new();

        while !parser.is_empty() {
            sections.push(parser.parse::<Section>()?)
        }

        Ok(Self { sections })
    }
}

impl SExpr for Module {
    fn car(&self) -> String {
        "module".to_owned()
    }

    fn cdr(&self) -> Vec<Expr> {
        self.sections.iter().flat_map(|s| s.exprs()).collect()
    }
}