From b76392d597a68b3f0e24eff2642015ebe67944fa Mon Sep 17 00:00:00 2001 From: Akulij Date: Wed, 21 May 2025 12:26:09 +0500 Subject: [PATCH] impl Parcelable for all RunnerConfig types --- src/botscript.rs | 107 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/src/botscript.rs b/src/botscript.rs index 66bd0bf..ac4aae7 100644 --- a/src/botscript.rs +++ b/src/botscript.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; +use crate::utils::parcelable::{ParcelType, Parcelable, ParcelableError, ParcelableResult}; use itertools::Itertools; use quickjs_rusty::serde::from_js; use quickjs_rusty::utils::create_null; @@ -63,6 +64,22 @@ impl FunctionMarker { } } +impl Parcelable for BotFunction { + fn get_field( + &mut self, + _name: &str, + ) -> crate::utils::parcelable::ParcelableResult> { + todo!() + } + + fn resolve(&mut self) -> ParcelableResult> + where + Self: Sized + 'static, + { + Ok(ParcelType::Function(self)) + } +} + impl BotFunction { pub fn by_name(name: String) -> Self { Self { @@ -189,6 +206,21 @@ pub enum KeyboardDefinition { Function(BotFunction), } +impl Parcelable for KeyboardDefinition { + fn get_field(&mut self, _name: &str) -> ParcelableResult> { + todo!() + } + fn resolve(&mut self) -> ParcelableResult> + where + Self: Sized + 'static, + { + match self { + KeyboardDefinition::Rows(rows) => Ok(rows.resolve()?), + KeyboardDefinition::Function(f) => Ok(f.resolve()?), + } + } +} + impl ResolveValue for KeyboardDefinition { type Value = Vec<::Value>; @@ -209,6 +241,21 @@ pub enum RowDefinition { Function(BotFunction), } +impl Parcelable for RowDefinition { + fn get_field(&mut self, _name: &str) -> ParcelableResult> { + todo!() + } + fn resolve(&mut self) -> ParcelableResult> + where + Self: Sized + 'static, + { + match self { + Self::Buttons(buttons) => Ok(buttons.resolve()?), + Self::Function(f) => Ok(f.resolve()?), + } + } +} + impl ResolveValue for RowDefinition { type Value = Vec<::Value>; @@ -246,12 +293,34 @@ impl ResolveValue for ButtonDefinition { } } +impl Parcelable for ButtonDefinition { + fn get_field(&mut self, _name: &str) -> ParcelableResult> { + todo!() + } + fn resolve(&mut self) -> ParcelableResult> + where + Self: Sized + 'static, + { + match self { + Self::Button(braw) => Ok(braw.resolve()?), + Self::ButtonLiteral(s) => Ok(s.resolve()?), + Self::Function(f) => Ok(f.resolve()?), + } + } +} + #[derive(Serialize, Deserialize, Debug, Clone)] pub struct ButtonRaw { name: ButtonName, callback_name: String, } +impl Parcelable for ButtonRaw { + fn get_field(&mut self, _name: &str) -> ParcelableResult> { + todo!() + } +} + impl ButtonRaw { pub fn from_literal(literal: String) -> Self { ButtonRaw { @@ -284,18 +353,56 @@ pub struct BotMessage { handler: Option, } +impl Parcelable for BotMessage { + fn get_field(&mut self, name: &str) -> ParcelableResult> { + match name { + "buttons" => Ok(self.buttons.resolve()?), + "state" => Ok(self.state.resolve()?), + "handler" => Ok(self.handler.resolve()?), + field => Err(ParcelableError::FieldError(format!( + "tried to get field {field}, but this field does not exists or private" + ))), + } + } +} + #[derive(Serialize, Deserialize, Debug)] pub struct BotDialog { pub commands: HashMap, stateful_msg_handlers: HashMap, } +impl Parcelable for BotDialog { + fn get_field(&mut self, name: &str) -> Result, ParcelableError> { + match name { + "commands" => Ok(ParcelType::Parcelable(&mut self.commands)), + "stateful_msg_handlersommands" => { + Ok(ParcelType::Parcelable(&mut self.stateful_msg_handlers)) + } + field => Err(ParcelableError::FieldError(format!( + "tried to get field {field}, but this field does not exists or private" + ))), + } + } +} + #[derive(Serialize, Deserialize, Debug)] pub struct RunnerConfig { config: BotConfig, pub dialog: BotDialog, } +impl Parcelable for RunnerConfig { + fn get_field(&mut self, name: &str) -> Result, ParcelableError> { + match name { + "dialog" => Ok(ParcelType::Parcelable(&mut self.dialog)), + field => Err(ParcelableError::FieldError(format!( + "tried to get field {field}, but this field does not exists or private" + ))), + } + } +} + pub struct Runner { context: Context, }