diff --git a/src/botscript.rs b/src/botscript.rs index 7f34184..ad9bde0 100644 --- a/src/botscript.rs +++ b/src/botscript.rs @@ -128,7 +128,97 @@ pub struct BotConfig { version: f64, } -#[derive(Serialize, Deserialize, Debug)] +pub trait ResolveValue { + type Value; + + fn resolve(self, runner: &Runner) -> ScriptResult; +} + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub enum KeyboardDefinition { + Rows(Vec), + Function(BotFunction), +} + +impl ResolveValue for KeyboardDefinition { + type Value = Vec<::Value>; + + fn resolve(self, runner: &Runner) -> ScriptResult { + match self { + KeyboardDefinition::Rows(rows) => rows.into_iter().map(|r| r.resolve(runner)).collect(), + KeyboardDefinition::Function(f) => { + Self::resolve(f.call_context(runner)?.js_into()?, runner) + } + } + } +} + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub enum RowDefinition { + Buttons(Vec), + Function(BotFunction), +} + +impl ResolveValue for RowDefinition { + type Value = Vec<::Value>; + + fn resolve(self, runner: &Runner) -> ScriptResult { + match self { + RowDefinition::Buttons(buttons) => { + buttons.into_iter().map(|b| b.resolve(runner)).collect() + } + RowDefinition::Function(f) => Self::resolve(f.call_context(runner)?.js_into()?, runner), + } + } +} + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub enum ButtonDefinition { + Button(ButtonRaw), + ButtonLiteral(String), + Function(BotFunction), +} + +impl ResolveValue for ButtonDefinition { + type Value = ButtonRaw; + + fn resolve(self, runner: &Runner) -> ScriptResult { + match self { + ButtonDefinition::Button(button) => Ok(button), + ButtonDefinition::ButtonLiteral(l) => Ok(ButtonRaw::from_literal(l)), + ButtonDefinition::Function(f) => { + Self::resolve(f.call_context(runner)?.js_into()?, runner) + } + } + } +} + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct ButtonRaw { + name: ButtonName, + literal: Option, + callback_name: String, +} + +impl ButtonRaw { + pub fn from_literal(literal: String) -> Self { + ButtonRaw { + name: ButtonName::Literal { + literal: literal.clone(), + }, + literal: Some(literal.clone()), + callback_name: literal, + } + } +} + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub enum ButtonName { + Value { name: String }, + Literal { literal: String }, +} + +#[derive(Serialize, Deserialize, Debug, Clone)] pub struct Button { name: String, }