wasm_webidl_bindings/binary/
mod.rs

1mod decode;
2mod encode;
3
4use self::decode::{Decode, DecodeContext};
5use self::encode::{Encode, EncodeContext};
6use crate::ast::WebidlBindings;
7use std::io;
8
9/// Encode the given Web IDL bindings section into the given write-able.
10pub fn encode<W>(
11    section: &WebidlBindings,
12    indices: &walrus::IdsToIndices,
13    into: &mut W,
14) -> io::Result<()>
15where
16    W: io::Write,
17{
18    let cx = &mut EncodeContext::new(indices);
19    section.encode(cx, into)
20}
21
22/// Decode the Web IDL bindings custom section data from the given input stream.
23///
24/// This does *not* parse the custom section discriminant and "webidl-bindings"
25/// custom section name, just the inner data.
26pub fn decode(ids: &walrus::IndicesToIds, from: &[u8]) -> anyhow::Result<WebidlBindings> {
27    let mut cx = DecodeContext::new(ids);
28    let mut from = from;
29    WebidlBindings::decode(&mut cx, &mut from)?;
30    Ok(cx.webidl_bindings)
31}
32
33/// Callback for `walrus::ModuleConfig::on_parse` to parse the webidl bindings
34/// custom section if one is found.
35pub fn on_parse(module: &mut walrus::Module, ids: &walrus::IndicesToIds) -> anyhow::Result<()> {
36    let section = match module.customs.remove_raw("webidl-bindings") {
37        Some(s) => s,
38        None => return Ok(()),
39    };
40    let bindings = decode(ids, &section.data)?;
41    module.customs.add(bindings);
42    Ok(())
43}