From eb63743714115b560366289c2b47b9359e1ec086 Mon Sep 17 00:00:00 2001 From: Akulij Date: Mon, 19 May 2025 23:55:44 +0500 Subject: [PATCH] create js_into for JsValue reason: wil be way less boilerplate for deserialization of JsValue to defined structs --- src/botscript.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/botscript.rs b/src/botscript.rs index 4f6430b..c871ab2 100644 --- a/src/botscript.rs +++ b/src/botscript.rs @@ -27,6 +27,19 @@ impl BotFunction { 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) {