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

87 lines
2.5 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::{Engine, EvalAltResult, Module, INT};
use std::mem;
use std::sync::Arc;
use heromodels::models::core::comment::Comment;
type RhaiComment = Comment;
use heromodels::db::hero::OurDB;
use heromodels::db::Collection;
#[export_module]
mod rhai_comment_module {
use super::{RhaiComment, INT};
#[rhai_fn(name = "new_comment", return_raw)]
pub fn new_comment() -> Result<RhaiComment, Box<EvalAltResult>> {
Ok(Comment::new())
}
#[rhai_fn(name = "user_id", return_raw)]
pub fn set_user_id(
comment: &mut RhaiComment,
user_id: i64,
) -> Result<RhaiComment, Box<EvalAltResult>> {
let owned = std::mem::take(comment);
*comment = owned.user_id(user_id as u32);
Ok(comment.clone())
}
#[rhai_fn(name = "content", return_raw)]
pub fn set_content(
comment: &mut RhaiComment,
content: String,
) -> Result<RhaiComment, Box<EvalAltResult>> {
let owned = std::mem::take(comment);
*comment = owned.content(content);
Ok(comment.clone())
}
#[rhai_fn(name = "get_comment_id")]
pub fn get_comment_id(comment: &mut RhaiComment) -> i64 {
comment.id() as i64
}
#[rhai_fn(name = "get_comment_user_id")]
pub fn get_comment_user_id(comment: &mut RhaiComment) -> i64 {
comment.user_id() as i64
}
#[rhai_fn(name = "get_comment_content")]
pub fn get_comment_content(comment: &mut RhaiComment) -> String {
comment.content().clone()
}
}
pub fn register_comment_rhai_module(engine: &mut Engine) {
let mut module = exported_module!(rhai_comment_module);
register_authorized_create_by_id_fn!(
module: &mut module,
rhai_fn_name: "save_comment",
resource_type_str: "Comment",
rhai_return_rust_type: heromodels::models::core::comment::Comment
);
register_authorized_get_by_id_fn!(
module: &mut module,
rhai_fn_name: "get_comment",
resource_type_str: "Comment",
rhai_return_rust_type: heromodels::models::core::comment::Comment
);
register_authorized_delete_by_id_fn!(
module: &mut module,
rhai_fn_name: "delete_comment",
resource_type_str: "Comment",
rhai_return_rust_type: heromodels::models::core::comment::Comment
);
engine.register_global_module(module.into());
}