wasm_runner_sdk/
capabilities.rs

1//! Runtime capability queries for the current deployment.
2
3use crate::abi;
4use std::sync::OnceLock;
5
6/// Capability identifiers exposed by the host.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum Capability {
9    Network,
10    StreamingResponse,
11    StreamingRequest,
12    QueueSend,
13    QueueReceive,
14    StorageKv,
15    StorageBlobs,
16}
17
18impl Capability {
19    /// Returns the string representation of this capability.
20    pub const fn as_str(self) -> &'static str {
21        match self {
22            Self::Network => "network",
23            Self::StreamingResponse => "streaming.response",
24            Self::StreamingRequest => "streaming.request",
25            Self::QueueSend => "queue.send",
26            Self::QueueReceive => "queue.receive",
27            Self::StorageKv => "storage.kv",
28            Self::StorageBlobs => "storage.blobs",
29        }
30    }
31
32    /// Parses a capability string into a typed value.
33    pub fn from_str(value: &str) -> Option<Self> {
34        match value {
35            "network" => Some(Self::Network),
36            "streaming.response" => Some(Self::StreamingResponse),
37            "streaming.request" => Some(Self::StreamingRequest),
38            "queue.send" => Some(Self::QueueSend),
39            "queue.receive" => Some(Self::QueueReceive),
40            "storage.kv" => Some(Self::StorageKv),
41            "storage.blobs" => Some(Self::StorageBlobs),
42            _ => None,
43        }
44    }
45}
46
47/// Returns the list of enabled capabilities for this deployment (raw strings).
48pub fn available() -> Vec<String> {
49    static CACHE: OnceLock<Vec<String>> = OnceLock::new();
50    CACHE
51        .get_or_init(|| {
52            let data = abi::read_bytes_from_host(|ptr, len| unsafe {
53                abi::capabilities_list(ptr, len)
54            });
55            abi::deserialize_string_list(&data)
56        })
57        .clone()
58}
59
60/// Returns the list of enabled capabilities for this deployment (typed).
61pub fn available_typed() -> Vec<Capability> {
62    available()
63        .into_iter()
64        .filter_map(|cap| Capability::from_str(&cap))
65        .collect()
66}
67
68/// Returns true if the given capability is enabled.
69pub fn has(capability: Capability) -> bool {
70    available().iter().any(|cap| cap == capability.as_str())
71}
72
73/// Returns true if the given capability string is enabled.
74pub fn has_str(capability: &str) -> bool {
75    available().iter().any(|cap| cap == capability)
76}