Skip to main content

triblespace_core/blob/schemas/
wasmcode.rs

1use anybytes::Bytes;
2
3use crate::blob::Blob;
4use crate::blob::BlobSchema;
5use crate::blob::ToBlob;
6use crate::id::ExclusiveId;
7use crate::id::Id;
8use crate::id_hex;
9use crate::macros::entity;
10use crate::metadata;
11use crate::metadata::{ConstDescribe, ConstId};
12use crate::repo::BlobStore;
13use crate::trible::Fragment;
14use crate::value::schemas::hash::Blake3;
15
16/// A blob schema for WebAssembly bytecode.
17///
18/// This schema is intended for sandboxed helper modules such as value formatters
19/// (see `metadata::value_formatter`).
20pub struct WasmCode;
21
22impl BlobSchema for WasmCode {}
23
24impl ConstId for WasmCode {
25    const ID: Id = id_hex!("DEE50FAD0CFFA4F8FD542DD18D9B7E52");
26}
27
28impl ConstDescribe for WasmCode {
29    fn describe<B>(blobs: &mut B) -> Result<Fragment, B::PutError>
30    where
31        B: BlobStore<Blake3>,
32    {
33        let id = Self::ID;
34        let description = blobs.put(
35            "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.",
36        )?;
37        Ok(entity! {
38            ExclusiveId::force_ref(&id) @
39                metadata::name: blobs.put("wasmcode")?,
40                metadata::description: description,
41                metadata::tag: metadata::KIND_BLOB_SCHEMA,
42        })
43    }
44}
45
46impl ToBlob<WasmCode> for Bytes {
47    fn to_blob(self) -> Blob<WasmCode> {
48        Blob::new(self)
49    }
50}
51
52impl ToBlob<WasmCode> for Vec<u8> {
53    fn to_blob(self) -> Blob<WasmCode> {
54        Blob::new(Bytes::from_source(self))
55    }
56}
57
58impl ToBlob<WasmCode> for &[u8] {
59    fn to_blob(self) -> Blob<WasmCode> {
60        Blob::new(Bytes::from_source(self.to_vec()))
61    }
62}
63
64#[cfg(feature = "wasm")]
65/// Runtime support for executing [`WasmCode`] modules.
66pub mod runtime;