wasmer_wit_component/
lib.rs

1//! The WebAssembly component tooling.
2
3#![deny(missing_docs)]
4
5use anyhow::{bail, Result};
6use std::str::FromStr;
7use wasm_encoder::CanonicalOption;
8use wasmer_wit_parser::Interface;
9
10#[cfg(feature = "cli")]
11pub mod cli;
12mod decoding;
13mod encoding;
14mod printing;
15mod validation;
16
17pub use encoding::*;
18pub use printing::*;
19
20/// Supported string encoding formats.
21#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
22pub enum StringEncoding {
23    /// Strings are encoded with UTF-8.
24    UTF8,
25    /// Strings are encoded with UTF-16.
26    UTF16,
27    /// Strings are encoded with compact UTF-16 (i.e. Latin1+UTF-16).
28    CompactUTF16,
29}
30
31impl Default for StringEncoding {
32    fn default() -> Self {
33        StringEncoding::UTF8
34    }
35}
36
37impl FromStr for StringEncoding {
38    type Err = anyhow::Error;
39
40    fn from_str(s: &str) -> Result<Self> {
41        match s {
42            "utf8" => Ok(StringEncoding::UTF8),
43            "utf16" => Ok(StringEncoding::UTF16),
44            "compact-utf16" => Ok(StringEncoding::CompactUTF16),
45            _ => bail!("unknown string encoding `{}`", s),
46        }
47    }
48}
49
50impl From<StringEncoding> for wasm_encoder::CanonicalOption {
51    fn from(e: StringEncoding) -> wasm_encoder::CanonicalOption {
52        match e {
53            StringEncoding::UTF8 => CanonicalOption::UTF8,
54            StringEncoding::UTF16 => CanonicalOption::UTF16,
55            StringEncoding::CompactUTF16 => CanonicalOption::CompactUTF16,
56        }
57    }
58}
59
60/// Decode an "interface-only" component to a wit `Interface`.
61pub fn decode_interface_component(bytes: &[u8]) -> Result<Interface> {
62    decoding::InterfaceDecoder::new(&decoding::ComponentInfo::new(bytes)?).decode()
63}