implement more models and rhai examples

This commit is contained in:
timurgordon
2025-05-22 03:57:03 +03:00
parent aa8ef90f9f
commit 56ec505874
79 changed files with 4546 additions and 182 deletions

View File

@@ -0,0 +1,36 @@
use heromodels::db::hero::OurDB;
use heromodels::models::flow::register_flow_rhai_module;
use rhai::Engine;
use std::sync::Arc;
use std::{fs, path::Path};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize Rhai engine
let mut engine = Engine::new();
// Initialize database with OurDB
// Using a temporary/in-memory database for the example
let db = Arc::new(OurDB::new("temp_flow_rhai_db", true).expect("Failed to create database"));
// Register flow Rhai module functions
register_flow_rhai_module(&mut engine, db.clone());
// Load and evaluate the Rhai script
let script_path_str = "examples/flow_rhai/flow.rhai";
let script_path = Path::new(script_path_str);
if !script_path.exists() {
eprintln!("Error: Rhai script not found at {}", script_path_str);
eprintln!("Please ensure the script 'flow.rhai' exists in the 'examples/flow_rhai/' directory.");
return Err(Box::new(std::io::Error::new(std::io::ErrorKind::NotFound, format!("Rhai script not found: {}", script_path_str))));
}
println!("Executing Rhai script: {}", script_path_str);
let script = fs::read_to_string(script_path)?;
match engine.eval::<()>(&script) {
Ok(_) => println!("\nRhai script executed successfully!"),
Err(e) => eprintln!("\nRhai script execution failed: {}\nDetails: {:#?}", e, e),
}
Ok(())
}

View File

@@ -0,0 +1,107 @@
// Hero Models - Flow Rhai Example
print("Hero Models - Flow Rhai Example");
print("=============================");
// Helper to format Option<String> (Dynamic in Rhai: String or ()) for printing
fn format_optional(val, placeholder) {
if val == () {
placeholder
} else {
val
}
}
// The database instance is now implicitly passed to DB functions.
print("DB instance will be implicitly passed.");
// --- Test Flow Model ---
print("\n--- Testing Flow Model ---");
// Create a new flow using the constructor and builder methods
print("Creating a new flow (ID: 1, UUID: flow-uuid-001)...");
let flow1 = new_flow(1, "flow-uuid-001")
.name("Document Approval Workflow")
.status("Active");
print("Flow object created: " + flow1);
print("Flow ID: " + flow1.id);
print("Flow UUID: " + flow1.flow_uuid);
print("Flow Name: " + flow1.name);
print("Flow Status: " + flow1.status);
// Save the flow to the database
set_flow(flow1);
print("Flow saved to database.");
// Retrieve the flow
let retrieved_flow = get_flow_by_id(1);
print("Retrieved Flow by ID (1): " + retrieved_flow.name + ", Status: " + retrieved_flow.status);
// --- Test FlowStep Model (as part of Flow) ---
print("\n--- Testing FlowStep Model (as part of Flow) ---");
// Create FlowSteps
print("Creating flow steps and adding to flow...");
let step1 = new_flow_step(101, 1) // id, step_order
.description("Initial Review by Manager")
.status("Pending");
let step2 = new_flow_step(102, 2) // id, step_order. Note: FlowStep ID 102 will be used for sig_req1 & sig_req2
.description("Legal Team Sign-off")
.status("Pending");
// Add steps to the flow created earlier
flow1 = flow1.add_step(step1);
flow1 = flow1.add_step(step2);
print("Flow now has " + flow1.steps.len() + " steps.");
print("First step description: " + format_optional(flow1.steps[0].description, "[No Description]"));
// Re-save the flow with its steps
set_flow(flow1);
print("Flow with steps saved to database.");
// Retrieve the flow and check its steps
let retrieved_flow_with_steps = get_flow_by_id(1);
print("Retrieved Flow by ID (1) has " + retrieved_flow_with_steps.steps.len() + " step(s).");
if retrieved_flow_with_steps.steps.len() > 0 {
print("First step of retrieved flow: " + format_optional(retrieved_flow_with_steps.steps[0].description, "[No Description]"));
}
// --- Test SignatureRequirement Model ---
print("\n--- Testing SignatureRequirement Model ---");
// Create SignatureRequirements (referencing FlowStep ID 102, which is step2)
print("Creating signature requirements for step with ID 102...");
let sig_req1 = new_signature_requirement(201, 102, "pubkey_legal_lead", "Legal Lead: Approve terms.")
.status("Required");
let sig_req2 = new_signature_requirement(202, 102, "pubkey_general_counsel", "General Counsel: Final Approval.")
.status("Required"); // signed_by and signature will default to None (Rust) / () (Rhai)
print("SigReq 1: " + sig_req1.message + " for PubKey: " + sig_req1.public_key + " (Status: " + sig_req1.status + ")");
if sig_req2.signed_by == () {
print("SigReq 2: " + sig_req2.message + " for PubKey: " + sig_req2.public_key + " (Status: " + sig_req2.status + ", Not signed yet)");
} else {
print("SigReq 2: " + sig_req2.message + " for PubKey: " + sig_req2.public_key + " (Status: " + sig_req2.status + ", Signed by: " + format_optional(sig_req2.signed_by, "[Not Signed Yet]") + ")");
}
// Save signature requirements
set_signature_requirement(sig_req1);
set_signature_requirement(sig_req2);
print("SignatureRequirements saved to database.");
// Retrieve a signature requirement
let retrieved_sig_req = get_signature_requirement_by_id(201);
print("Retrieved SignatureRequirement by ID (201): " + retrieved_sig_req.message);
// --- Test updating a SignatureRequirement ---
print("\n--- Testing Update for SignatureRequirement ---");
let updated_sig_req = retrieved_sig_req
.status("Signed")
.signed_by("pubkey_legal_lead_actual_signer_id")
.signature("base64_encoded_signature_data_here");
print("Updated SigReq 1 - Status: " + updated_sig_req.status + ", Signed By: " + format_optional(updated_sig_req.signed_by, "[Not Signed Yet]") + ", Signature: " + format_optional(updated_sig_req.signature, "[No Signature]"));
set_signature_requirement(updated_sig_req); // Save updated
print("Updated SignatureRequirement saved.");
print("\nFlow Rhai example script finished.");