[][src]Function wasmer_interface_types_fl::decoders::binary::parse

pub fn parse<'input, E: ParseError<&'input [u8]>>(
    bytes: &'input [u8]
) -> IResult<&'input [u8], Interfaces<'_>, E>

Parse a sequence of bytes, expecting it to be a valid WIT binary representation, into an Interfaces structure.

Example

use wasmer_interface_types::{
    ast::{Adapter, Export, Implementation, Import, Interfaces, Type},
    decoders::binary::parse,
    interpreter::Instruction,
    types::IType,
};

let input = &[
    0x00, // type section
    0x01, // 1 type
    0x00, // function type
    0x01, // list of 1 item
    0x00, // S8
    0x01, // list of 1 item
    0x01, // S16
    //
    0x01, // import section
    0x01, // 1 import
    0x02, // string of 2 bytes
    0x61, 0x62, // "a", "b"
    0x01, // string of 1 byte
    0x63, // "c"
    0x00, // signature type
    //
    0x02, // adapter section
    0x01, // 1 adapter
    0x00, // function type
    0x01, // list of 1 item
    0x00, 0x01, // ArgumentGet { index: 1 }
    //
    0x03, // export section
    0x01, // 1 export
    0x02, // string of 2 bytes
    0x61, 0x62, // "a", "b"
    0x01, // function type
    //
    0x04, // implementation section
    0x01, // 1 implementation
    0x02, // core function type
    0x03, // adapter function type
];
let output = Ok((
    &[] as &[u8],
    Interfaces {
        types: vec![Type::Function {
            inputs: vec![IType::S8],
            outputs: vec![IType::S16],
        }],
        imports: vec![Import {
            namespace: "ab",
            name: "c",
            function_type: 0,
        }],
        adapters: vec![Adapter {
            function_type: 0,
            instructions: vec![Instruction::ArgumentGet { index: 1 }],
        }],
        exports: vec![Export {
            name: "ab",
            function_type: 1,
        }],
        implementations: vec![Implementation {
            core_function_type: 2,
            adapter_function_type: 3,
        }],
    },
));

assert_eq!(parse::<()>(input), output);