Skip to main content

pepl_stdlib/modules/
notifications.rs

1//! `notifications` capability module — push notifications (host-delegated).
2//!
3//! Functions: send.
4//! Notification delivery is host-delegated — the runtime host sends actual
5//! notifications via `env.host_call(cap_id=4, fn_id=1, payload)`. This module
6//! validates arguments and returns a `CapabilityCall` error to signal the
7//! caller to route the call to the host.
8//!
9//! # Cap ID / Fn ID Mapping
10//!
11//! | fn_id | Function |
12//! |-------|----------|
13//! | 1     | send     |
14
15use crate::capability::{CAP_NOTIFICATIONS, NOTIFICATIONS_SEND};
16use crate::error::StdlibError;
17use crate::module::StdlibModule;
18use crate::value::Value;
19
20/// The `notifications` capability module.
21pub struct NotificationsModule;
22
23impl NotificationsModule {
24    pub fn new() -> Self {
25        Self
26    }
27}
28
29impl Default for NotificationsModule {
30    fn default() -> Self {
31        Self::new()
32    }
33}
34
35impl StdlibModule for NotificationsModule {
36    fn name(&self) -> &'static str {
37        "notifications"
38    }
39
40    fn has_function(&self, function: &str) -> bool {
41        matches!(function, "send")
42    }
43
44    fn call(&self, function: &str, args: Vec<Value>) -> Result<Value, StdlibError> {
45        match function {
46            "send" => self.send(args),
47            _ => Err(StdlibError::unknown_function("notifications", function)),
48        }
49    }
50}
51
52impl NotificationsModule {
53    /// `notifications.send(title: string, body: string) -> Result<nil, NotificationError>`
54    ///
55    /// Validates: exactly 2 args, both must be strings.
56    /// Returns `CapabilityCall` with cap_id=4, fn_id=1.
57    fn send(&self, args: Vec<Value>) -> Result<Value, StdlibError> {
58        if args.len() != 2 {
59            return Err(StdlibError::wrong_args("notifications.send", 2, args.len()));
60        }
61        validate_string("notifications.send", &args[0], 1)?;
62        validate_string("notifications.send", &args[1], 2)?;
63        Err(StdlibError::capability_call(
64            "notifications",
65            "send",
66            CAP_NOTIFICATIONS,
67            NOTIFICATIONS_SEND,
68            args,
69        ))
70    }
71}
72
73// ── Helpers ──────────────────────────────────────────────────────────────────
74
75fn validate_string(func: &str, val: &Value, pos: usize) -> Result<(), StdlibError> {
76    match val {
77        Value::String(_) => Ok(()),
78        _ => Err(StdlibError::type_mismatch(
79            func,
80            pos,
81            "string",
82            val.type_name(),
83        )),
84    }
85}