pepl_stdlib/modules/
notifications.rs1use crate::capability::{CAP_NOTIFICATIONS, NOTIFICATIONS_SEND};
16use crate::error::StdlibError;
17use crate::module::StdlibModule;
18use crate::value::Value;
19
20pub 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 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
73fn 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}