Files
db/heromodels/src/models/finance/rhai.rs
2025-08-21 14:05:01 +02:00

81 lines
2.3 KiB
Rust

use heromodels::db::Db;
use macros::{
register_authorized_create_by_id_fn, register_authorized_delete_by_id_fn,
register_authorized_get_by_id_fn,
};
use rhai::plugin::*;
use rhai::{Array, Engine, EvalAltResult, Module, INT};
use std::mem;
use std::sync::Arc;
use heromodels::db::hero::OurDB;
use heromodels::db::Collection;
use heromodels::models::finance::account::Account;
type RhaiAccount = Account;
#[export_module]
mod rhai_account_module {
use super::{Array, RhaiAccount, INT};
#[rhai_fn(name = "new_account", return_raw)]
pub fn new_account() -> Result<RhaiAccount, Box<EvalAltResult>> {
Ok(Account::new())
}
#[rhai_fn(name = "name", return_raw)]
pub fn set_name(
account: &mut RhaiAccount,
name: String,
) -> Result<RhaiAccount, Box<EvalAltResult>> {
let owned = std::mem::take(account);
*account = owned.name(name);
Ok(account.clone())
}
#[rhai_fn(name = "user_id", return_raw)]
pub fn set_user_id(
account: &mut RhaiAccount,
user_id: INT,
) -> Result<RhaiAccount, Box<EvalAltResult>> {
let owned = std::mem::take(account);
*account = owned.user_id(user_id as u32);
Ok(account.clone())
}
#[rhai_fn(name = "get_account_id")]
pub fn get_account_id(account: &mut RhaiAccount) -> i64 {
account.id() as i64
}
#[rhai_fn(name = "get_account_name")]
pub fn get_account_name(account: &mut RhaiAccount) -> String {
account.name().clone()
}
#[rhai_fn(name = "get_account_user_id")]
pub fn get_account_user_id(account: &mut RhaiAccount) -> INT {
account.user_id() as INT
}
}
pub fn register_account_rhai_module(engine: &mut Engine) {
let mut module = exported_module!(rhai_account_module);
register_authorized_create_by_id_fn!(
module: &mut module,
rhai_fn_name: "save_account",
resource_type_str: "Account",
rhai_return_rust_type: heromodels::models::finance::account::Account
);
register_authorized_get_by_id_fn!(
module: &mut module,
rhai_fn_name: "get_account",
resource_type_str: "Account",
rhai_return_rust_type: heromodels::models::finance::account::Account
);
engine.register_global_module(module.into());
}