This repository has been archived on 2025-11-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
osiris_old/core/src/objects/legal/rhai.rs

151 lines
4.6 KiB
Rust

/// Rhai bindings for Legal objects (Contract)
use ::rhai::plugin::*;
use ::rhai::{CustomType, Dynamic, Engine, EvalAltResult, Module, TypeBuilder};
use super::{Contract, ContractStatus};
/// Register legal modules with the Rhai engine
pub fn register_legal_modules(parent_module: &mut Module) {
// Register custom types
parent_module.set_custom_type::<Contract>("Contract");
parent_module.set_custom_type::<ContractStatus>("ContractStatus");
// Merge contract functions
let contract_module = exported_module!(rhai_contract_module);
parent_module.merge(&contract_module);
}
// ============================================================================
// Contract Module
// ============================================================================
type RhaiContract = Contract;
type RhaiContractStatus = ContractStatus;
#[export_module]
mod rhai_contract_module {
use super::{RhaiContract, RhaiContractStatus};
use super::super::{Contract, ContractStatus};
use ::rhai::EvalAltResult;
// Contract constructor
#[rhai_fn(name = "new_contract", return_raw)]
pub fn new_contract(id: i64) -> Result<RhaiContract, Box<EvalAltResult>> {
Ok(Contract::new(id as u32))
}
// Builder methods
#[rhai_fn(name = "title", return_raw)]
pub fn set_title(
contract: RhaiContract,
title: String,
) -> Result<RhaiContract, Box<EvalAltResult>> {
Ok(contract.title(title))
}
#[rhai_fn(name = "content", return_raw)]
pub fn set_content(
contract: RhaiContract,
content: String,
) -> Result<RhaiContract, Box<EvalAltResult>> {
Ok(contract.content(content))
}
#[rhai_fn(name = "creator_id", return_raw)]
pub fn set_creator_id(
contract: RhaiContract,
creator_id: i64,
) -> Result<RhaiContract, Box<EvalAltResult>> {
Ok(contract.creator_id(creator_id as u32))
}
#[rhai_fn(name = "expires_at", return_raw)]
pub fn set_expires_at(
contract: RhaiContract,
expires_at: i64,
) -> Result<RhaiContract, Box<EvalAltResult>> {
Ok(contract.expires_at(expires_at as u64))
}
#[rhai_fn(name = "add_signature", return_raw)]
pub fn add_signature(
contract: RhaiContract,
signature_id: i64,
) -> Result<RhaiContract, Box<EvalAltResult>> {
Ok(contract.add_signature(signature_id as u32))
}
#[rhai_fn(name = "remove_signature", return_raw)]
pub fn remove_signature(
contract: RhaiContract,
signature_id: i64,
) -> Result<RhaiContract, Box<EvalAltResult>> {
Ok(contract.remove_signature(signature_id as u32))
}
// State management methods
#[rhai_fn(name = "activate", return_raw)]
pub fn activate(contract: RhaiContract) -> Result<RhaiContract, Box<EvalAltResult>> {
Ok(contract.activate())
}
#[rhai_fn(name = "complete", return_raw)]
pub fn complete(contract: RhaiContract) -> Result<RhaiContract, Box<EvalAltResult>> {
Ok(contract.complete())
}
#[rhai_fn(name = "cancel", return_raw)]
pub fn cancel(contract: RhaiContract) -> Result<RhaiContract, Box<EvalAltResult>> {
Ok(contract.cancel())
}
// Query methods
#[rhai_fn(name = "is_fully_signed", pure)]
pub fn is_fully_signed(contract: &mut RhaiContract, required_count: i64) -> bool {
contract.is_fully_signed(required_count as usize)
}
// Getters
#[rhai_fn(name = "title", pure)]
pub fn get_title(contract: &mut RhaiContract) -> String {
contract.title.clone()
}
#[rhai_fn(name = "content", pure)]
pub fn get_content(contract: &mut RhaiContract) -> String {
contract.content.clone()
}
#[rhai_fn(name = "status", pure)]
pub fn get_status(contract: &mut RhaiContract) -> String {
format!("{:?}", contract.status)
}
#[rhai_fn(name = "creator_id", pure)]
pub fn get_creator_id(contract: &mut RhaiContract) -> i64 {
contract.creator_id as i64
}
#[rhai_fn(name = "signature_count", pure)]
pub fn get_signature_count(contract: &mut RhaiContract) -> i64 {
contract.signatures.len() as i64
}
}
// ============================================================================
// CustomType Implementations
// ============================================================================
impl CustomType for Contract {
fn build(mut builder: TypeBuilder<Self>) {
builder.with_name("Contract");
}
}
impl CustomType for ContractStatus {
fn build(mut builder: TypeBuilder<Self>) {
builder.with_name("ContractStatus");
}
}