From 31e78be68fc5f5c07ce5d802ffc9ec9ecf9013d1 Mon Sep 17 00:00:00 2001 From: Akulij Date: Fri, 23 May 2025 16:26:35 +0500 Subject: [PATCH] change interface for ResolveValue --- src/botscript.rs | 50 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/src/botscript.rs b/src/botscript.rs index 7027025..81dc3ac 100644 --- a/src/botscript.rs +++ b/src/botscript.rs @@ -257,7 +257,8 @@ pub struct BotConfig { pub trait ResolveValue { type Value; - fn resolve(self, runner: &Runner) -> ScriptResult; + fn resolve(self) -> ScriptResult; + fn resolve_with(self, runner: &Runner) -> ScriptResult; } #[derive(Serialize, Deserialize, Debug, Clone)] @@ -285,11 +286,22 @@ impl Parcelable for KeyboardDefinition { impl ResolveValue for KeyboardDefinition { type Value = Vec<::Value>; - fn resolve(self, runner: &Runner) -> ScriptResult { + fn resolve(self) -> ScriptResult { match self { - KeyboardDefinition::Rows(rows) => rows.into_iter().map(|r| r.resolve(runner)).collect(), + KeyboardDefinition::Rows(rows) => rows.into_iter().map(|r| r.resolve()).collect(), KeyboardDefinition::Function(f) => { - ::resolve(f.call_context(runner)?.js_into()?, runner) + ::resolve(f.call()?.js_into()?) + } + } + } + + fn resolve_with(self, runner: &Runner) -> ScriptResult { + match self { + KeyboardDefinition::Rows(rows) => { + rows.into_iter().map(|r| r.resolve_with(runner)).collect() + } + KeyboardDefinition::Function(f) => { + ::resolve_with(f.call_context(runner)?.js_into()?, runner) } } } @@ -320,13 +332,21 @@ impl Parcelable for RowDefinition { impl ResolveValue for RowDefinition { type Value = Vec<::Value>; - fn resolve(self, runner: &Runner) -> ScriptResult { + fn resolve(self) -> ScriptResult { match self { - RowDefinition::Buttons(buttons) => { - buttons.into_iter().map(|b| b.resolve(runner)).collect() - } + RowDefinition::Buttons(buttons) => buttons.into_iter().map(|b| b.resolve()).collect(), + RowDefinition::Function(f) => ::resolve(f.call()?.js_into()?), + } + } + + fn resolve_with(self, runner: &Runner) -> ScriptResult { + match self { + RowDefinition::Buttons(buttons) => buttons + .into_iter() + .map(|b| b.resolve_with(runner)) + .collect(), RowDefinition::Function(f) => { - ::resolve(f.call_context(runner)?.js_into()?, runner) + ::resolve_with(f.call_context(runner)?.js_into()?, runner) } } } @@ -343,12 +363,20 @@ pub enum ButtonDefinition { impl ResolveValue for ButtonDefinition { type Value = ButtonRaw; - fn resolve(self, runner: &Runner) -> ScriptResult { + fn resolve(self) -> ScriptResult { + match self { + ButtonDefinition::Button(button) => Ok(button), + ButtonDefinition::ButtonLiteral(l) => Ok(ButtonRaw::from_literal(l)), + ButtonDefinition::Function(f) => ::resolve(f.call()?.js_into()?), + } + } + + fn resolve_with(self, runner: &Runner) -> ScriptResult { match self { ButtonDefinition::Button(button) => Ok(button), ButtonDefinition::ButtonLiteral(l) => Ok(ButtonRaw::from_literal(l)), ButtonDefinition::Function(f) => { - ::resolve(f.call_context(runner)?.js_into()?, runner) + ::resolve_with(f.call_context(runner)?.js_into()?, runner) } } }