[][src]Function wat::parse_str

pub fn parse_str(wat: impl AsRef<str>) -> Result<Vec<u8>>

Parses an in-memory string as the WebAssembly Text format, returning the file as a binary WebAssembly file.

This function is intended to be a stable convenience function for parsing a wat file into a WebAssembly binary file. This is a high-level operation which does not expose any parsing internals, for that you'll want to use the wast crate.

Errors

This function can fail for a number of reasons, including (but not limited to):

  • The wat input may fail to lex, such as having invalid tokens or syntax
  • The wat input may fail to parse, such as having incorrect syntactical structure
  • The wat input may contain names that could not be resolved

Examples

assert_eq!(wat::parse_str("(module)")?, b"\0asm\x01\0\0\0");
assert!(wat::parse_str("module").is_err());

let wat = r#"
    (module
        (func $foo)

        (func (export "bar")
            call $foo
        )
    )
"#;

let binary = wat::parse_str(wat)?;
// ...