use crate::capability::{CAP_NOTIFICATIONS, NOTIFICATIONS_SEND};
use crate::error::StdlibError;
use crate::module::StdlibModule;
use crate::value::Value;
pub struct NotificationsModule;
impl NotificationsModule {
pub fn new() -> Self {
Self
}
}
impl Default for NotificationsModule {
fn default() -> Self {
Self::new()
}
}
impl StdlibModule for NotificationsModule {
fn name(&self) -> &'static str {
"notifications"
}
fn has_function(&self, function: &str) -> bool {
matches!(function, "send")
}
fn call(&self, function: &str, args: Vec<Value>) -> Result<Value, StdlibError> {
match function {
"send" => self.send(args),
_ => Err(StdlibError::unknown_function("notifications", function)),
}
}
}
impl NotificationsModule {
fn send(&self, args: Vec<Value>) -> Result<Value, StdlibError> {
if args.len() != 2 {
return Err(StdlibError::wrong_args("notifications.send", 2, args.len()));
}
validate_string("notifications.send", &args[0], 1)?;
validate_string("notifications.send", &args[1], 2)?;
Err(StdlibError::capability_call(
"notifications",
"send",
CAP_NOTIFICATIONS,
NOTIFICATIONS_SEND,
args,
))
}
}
fn validate_string(func: &str, val: &Value, pos: usize) -> Result<(), StdlibError> {
match val {
Value::String(_) => Ok(()),
_ => Err(StdlibError::type_mismatch(
func,
pos,
"string",
val.type_name(),
)),
}
}