use std::collections::HashMap; use quickjs_rusty::serde::from_js; use quickjs_rusty::Context; use quickjs_rusty::ContextError; use quickjs_rusty::ExecutionError; use quickjs_rusty::OwnedJsValue as JsValue; use serde::Deserialize; use serde::Serialize; #[derive(thiserror::Error, Debug)] pub enum ScriptError { #[error("error context: {0:?}")] ContextError(#[from] ContextError), #[error("error running: {0:?}")] ExecutionError(#[from] ExecutionError), #[error("error from anyhow: {0:?}")] SerdeError(#[from] quickjs_rusty::serde::Error), } pub type ScriptResult = Result; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct BotFunction(String); // temporal workaround impl BotFunction { pub fn call_context(&self, runner: &Runner) -> ScriptResult { let func_name = &self.0; runner.run_script(&format!("{func_name}()")) } } pub trait DeserializeJS { fn js_into<'a, T: Deserialize<'a>>(&'a self) -> ScriptResult; } impl DeserializeJS for JsValue { fn js_into<'a, T: Deserialize<'a>>(&'a self) -> ScriptResult { let rc = from_js(self.context(), &self)?; Ok(rc) } } // TODO: remove this function since it is suitable only for early development #[allow(clippy::print_stdout)] fn print(s: String) { println!("{s}"); } #[derive(Serialize, Deserialize, Debug)] pub struct BotConfig { version: f64, } #[derive(Serialize, Deserialize, Debug)] pub struct Button { name: String, } #[derive(Serialize, Deserialize, Debug, Clone)] pub struct BotMessage { // buttons: Vec