Skip to main content

triblespace_core/blob/encodings/
wasmcode.rs

1use crate::inline::Encodes;
2use anybytes::Bytes;
3
4use crate::blob::Blob;
5use crate::blob::BlobEncoding;
6use crate::id::ExclusiveId;
7use crate::id::Id;
8use crate::id_hex;
9use crate::macros::entity;
10use crate::metadata;
11use crate::metadata::MetaDescribe;
12use crate::trible::Fragment;
13
14/// A blob encoding for WebAssembly bytecode.
15///
16/// This schema is intended for sandboxed helper modules such as value formatters
17/// (see `metadata::value_formatter`).
18pub struct WasmCode;
19
20impl BlobEncoding for WasmCode {}
21
22impl MetaDescribe for WasmCode {
23    fn describe() -> Fragment {
24        let id: Id = id_hex!("DEE50FAD0CFFA4F8FD542DD18D9B7E52");
25        entity! {
26            ExclusiveId::force_ref(&id) @
27                metadata::name: "wasmcode",
28                metadata::description: "WebAssembly bytecode blob for sandboxed helper modules. The modules are expected to be deterministic and import-free, intended for small utilities such as value formatters.\n\nUse when a schema references a formatter via metadata::value_formatter or similar tooling and you want portable, sandboxed code alongside the data. Avoid large or stateful modules; keep the bytecode focused on pure formatting or validation tasks.",
29                metadata::tag: metadata::KIND_BLOB_ENCODING,
30        }
31    }
32}
33
34impl Encodes<Bytes> for WasmCode
35where crate::inline::encodings::hash::Handle<WasmCode>: crate::inline::InlineEncoding,
36{
37    type Output = Blob<WasmCode>;
38    fn encode(source: Bytes) -> Blob<WasmCode> {
39        Blob::new(source)
40    }
41}
42
43impl Encodes<Vec<u8>> for WasmCode
44where crate::inline::encodings::hash::Handle<WasmCode>: crate::inline::InlineEncoding,
45{
46    type Output = Blob<WasmCode>;
47    fn encode(source: Vec<u8>) -> Blob<WasmCode> {
48        Blob::new(Bytes::from_source(source))
49    }
50}
51
52impl Encodes<&[u8]> for WasmCode
53where crate::inline::encodings::hash::Handle<WasmCode>: crate::inline::InlineEncoding,
54{
55    type Output = Blob<WasmCode>;
56    fn encode(source: &[u8]) -> Blob<WasmCode> {
57        Blob::new(Bytes::from_source(source.to_vec()))
58    }
59}
60
61#[cfg(feature = "wasm")]
62/// Runtime support for executing [`WasmCode`] modules.
63pub mod runtime;