pepl_stdlib/capability.rs
1//! Capability ID mappings for `env.host_call` dispatch.
2//!
3//! Each capability module has a unique `cap_id` and each function within
4//! it has a unique `fn_id`. These constants are used by:
5//! - The stdlib capability modules (to return `CapabilityCall` errors)
6//! - The WASM code generator (to emit `env.host_call(cap_id, fn_id, ...)` instructions)
7
8// ── Capability IDs ───────────────────────────────────────────────────────────
9
10/// HTTP capability (get, post, put, patch, delete).
11pub const CAP_HTTP: u32 = 1;
12
13/// Persistent storage capability (get, set, delete, keys).
14pub const CAP_STORAGE: u32 = 2;
15
16/// Location/GPS capability (current).
17pub const CAP_LOCATION: u32 = 3;
18
19/// Push notifications capability (send).
20pub const CAP_NOTIFICATIONS: u32 = 4;
21
22/// Credential resolution (internal — PEPL code does not call directly).
23pub const CAP_CREDENTIAL: u32 = 5;
24
25// ── Function IDs: http ───────────────────────────────────────────────────────
26
27pub const HTTP_GET: u32 = 1;
28pub const HTTP_POST: u32 = 2;
29pub const HTTP_PUT: u32 = 3;
30pub const HTTP_PATCH: u32 = 4;
31pub const HTTP_DELETE: u32 = 5;
32
33// ── Function IDs: storage ────────────────────────────────────────────────────
34
35pub const STORAGE_GET: u32 = 1;
36pub const STORAGE_SET: u32 = 2;
37pub const STORAGE_DELETE: u32 = 3;
38pub const STORAGE_KEYS: u32 = 4;
39
40// ── Function IDs: location ───────────────────────────────────────────────────
41
42pub const LOCATION_CURRENT: u32 = 1;
43
44// ── Function IDs: notifications ──────────────────────────────────────────────
45
46pub const NOTIFICATIONS_SEND: u32 = 1;
47
48// ── Function IDs: credential ─────────────────────────────────────────────────
49
50pub const CREDENTIAL_GET: u32 = 1;
51
52// ── Lookup ───────────────────────────────────────────────────────────────────
53
54/// Resolve a capability module name + function name to `(cap_id, fn_id)`.
55///
56/// Returns `None` if the module/function combination is not a capability call.
57///
58/// # Example
59/// ```
60/// use pepl_stdlib::capability::resolve_ids;
61/// assert_eq!(resolve_ids("http", "get"), Some((1, 1)));
62/// assert_eq!(resolve_ids("storage", "keys"), Some((2, 4)));
63/// assert_eq!(resolve_ids("math", "abs"), None);
64/// ```
65pub fn resolve_ids(module: &str, function: &str) -> Option<(u32, u32)> {
66 match (module, function) {
67 ("http", "get") => Some((CAP_HTTP, HTTP_GET)),
68 ("http", "post") => Some((CAP_HTTP, HTTP_POST)),
69 ("http", "put") => Some((CAP_HTTP, HTTP_PUT)),
70 ("http", "patch") => Some((CAP_HTTP, HTTP_PATCH)),
71 ("http", "delete") => Some((CAP_HTTP, HTTP_DELETE)),
72
73 ("storage", "get") => Some((CAP_STORAGE, STORAGE_GET)),
74 ("storage", "set") => Some((CAP_STORAGE, STORAGE_SET)),
75 ("storage", "delete") => Some((CAP_STORAGE, STORAGE_DELETE)),
76 ("storage", "keys") => Some((CAP_STORAGE, STORAGE_KEYS)),
77
78 ("location", "current") => Some((CAP_LOCATION, LOCATION_CURRENT)),
79
80 ("notifications", "send") => Some((CAP_NOTIFICATIONS, NOTIFICATIONS_SEND)),
81
82 _ => None,
83 }
84}
85
86/// Returns `true` if the given module name is a capability module.
87pub fn is_capability_module(module: &str) -> bool {
88 matches!(module, "http" | "storage" | "location" | "notifications")
89}
90
91/// Returns all capability module names.
92pub fn capability_module_names() -> &'static [&'static str] {
93 &["http", "storage", "location", "notifications"]
94}