implement more models and rhai examples
This commit is contained in:
		
							
								
								
									
										44
									
								
								heromodels/examples/legal_rhai/example.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								heromodels/examples/legal_rhai/example.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,44 @@
 | 
			
		||||
use heromodels::db::hero::OurDB;
 | 
			
		||||
use heromodels::models::legal::register_legal_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 (creates files in current dir)
 | 
			
		||||
    let db_name = "temp_legal_rhai_db";
 | 
			
		||||
    let db = Arc::new(OurDB::new(db_name, true).expect("Failed to create database"));
 | 
			
		||||
 | 
			
		||||
    // Register legal Rhai module functions
 | 
			
		||||
    register_legal_rhai_module(&mut engine, db.clone());
 | 
			
		||||
 | 
			
		||||
    // Load and evaluate the Rhai script
 | 
			
		||||
    let script_path_str = "examples/legal_rhai/legal.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 'legal.rhai' exists in the 'examples/legal_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);
 | 
			
		||||
            // No explicit cleanup in this example, similar to flow_rhai_example
 | 
			
		||||
            return Err(e.into()); // Propagate the Rhai error
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // No explicit cleanup in this example, similar to flow_rhai_example
 | 
			
		||||
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										119
									
								
								heromodels/examples/legal_rhai/legal.rhai
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										119
									
								
								heromodels/examples/legal_rhai/legal.rhai
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,119 @@
 | 
			
		||||
// heromodels - Legal Module Rhai Example
 | 
			
		||||
print("Hero Models - Legal Rhai Example");
 | 
			
		||||
print("===============================");
 | 
			
		||||
 | 
			
		||||
// Helper to format Option<String> (Dynamic in Rhai: String or ()) for printing
 | 
			
		||||
fn format_optional_string(val, placeholder) {
 | 
			
		||||
    if val == () {
 | 
			
		||||
        placeholder
 | 
			
		||||
    } else {
 | 
			
		||||
        val
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Helper to format Option<i64> (Dynamic in Rhai: i64 or ()) for printing
 | 
			
		||||
fn format_optional_int(val, placeholder) {
 | 
			
		||||
    if val == () {
 | 
			
		||||
        placeholder
 | 
			
		||||
    } else {
 | 
			
		||||
        "" + val // Convert int to string for concatenation
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
print("DB instance will be implicitly passed to DB functions.");
 | 
			
		||||
 | 
			
		||||
// --- Using Enum Constants ---
 | 
			
		||||
print(`\n--- Enum Constants ---`);
 | 
			
		||||
print(`ContractStatus Draft: ${ContractStatusConstants::Draft}`);
 | 
			
		||||
print(`ContractStatus Active: ${ContractStatusConstants::Active}`);
 | 
			
		||||
print(`SignerStatus Pending: ${SignerStatusConstants::Pending}`);
 | 
			
		||||
print(`SignerStatus Signed: ${SignerStatusConstants::Signed}`);
 | 
			
		||||
 | 
			
		||||
// --- Test ContractSigner Model ---
 | 
			
		||||
print("\n--- Testing ContractSigner Model ---");
 | 
			
		||||
let signer1_id = "signer-uuid-001";
 | 
			
		||||
let signer1 = new_contract_signer(signer1_id, "Alice Wonderland", "alice@example.com")
 | 
			
		||||
    .status(SignerStatusConstants::Pending)
 | 
			
		||||
    .comments("Alice is the primary signatory.");
 | 
			
		||||
 | 
			
		||||
print(`Signer 1 ID: ${signer1.id}, Name: ${signer1.name}, Email: ${signer1.email}`);
 | 
			
		||||
print(`Signer 1 Status: ${signer1.status}, Comments: ${format_optional_string(signer1.comments, "N/A")}`);
 | 
			
		||||
print(`Signer 1 Signed At: ${format_optional_int(signer1.signed_at, "Not signed")}`);
 | 
			
		||||
 | 
			
		||||
let signer2_id = "signer-uuid-002";
 | 
			
		||||
let signer2 = new_contract_signer(signer2_id, "Bob The Builder", "bob@example.com")
 | 
			
		||||
    .status(SignerStatusConstants::Signed)
 | 
			
		||||
    .signed_at(1678886400) // Example timestamp
 | 
			
		||||
    .comments("Bob has already signed.");
 | 
			
		||||
 | 
			
		||||
print(`Signer 2 ID: ${signer2.id}, Name: ${signer2.name}, Status: ${signer2.status}, Signed At: ${format_optional_int(signer2.signed_at, "N/A")}`);
 | 
			
		||||
 | 
			
		||||
// --- Test ContractRevision Model ---
 | 
			
		||||
print("\n--- Testing ContractRevision Model ---");
 | 
			
		||||
let revision1_version = 1;
 | 
			
		||||
let revision1 = new_contract_revision(revision1_version, "Initial draft content for the agreement, version 1.", 1678880000, "user-admin-01")
 | 
			
		||||
    .comments("First version of the contract.");
 | 
			
		||||
 | 
			
		||||
print(`Revision 1 Version: ${revision1.version}, Content: '${revision1.content}', Created At: ${revision1.created_at}, By: ${revision1.created_by}`);
 | 
			
		||||
print(`Revision 1 Comments: ${format_optional_string(revision1.comments, "N/A")}`);
 | 
			
		||||
 | 
			
		||||
let revision2_version = 2;
 | 
			
		||||
let revision2 = new_contract_revision(revision2_version, "Updated content with new clauses, version 2.", 1678882200, "user-legal-02");
 | 
			
		||||
 | 
			
		||||
// --- Test Contract Model ---
 | 
			
		||||
print("\n--- Testing Contract Model ---");
 | 
			
		||||
let contract1_base_id = 101;
 | 
			
		||||
let contract1_uuid = "contract-uuid-xyz-001";
 | 
			
		||||
 | 
			
		||||
print(`Creating a new contract (ID: ${contract1_base_id}, UUID: ${contract1_uuid})...`);
 | 
			
		||||
let contract1 = new_contract(contract1_base_id, contract1_uuid)
 | 
			
		||||
    .title("Master Service Agreement")
 | 
			
		||||
    .description("MSA between ACME Corp and Client Inc.")
 | 
			
		||||
    .contract_type("Services")
 | 
			
		||||
    .status(ContractStatusConstants::Draft)
 | 
			
		||||
    .created_by("user-admin-01")
 | 
			
		||||
    .terms_and_conditions("Standard terms and conditions apply. See Appendix A.")
 | 
			
		||||
    .start_date(1678900000)
 | 
			
		||||
    .current_version(revision1.version)
 | 
			
		||||
    .add_signer(signer1)
 | 
			
		||||
    .add_revision(revision1);
 | 
			
		||||
 | 
			
		||||
print(`Contract 1 Title: ${contract1.title}, Status: ${contract1.status}`);
 | 
			
		||||
print(`Contract 1 Signers: ${contract1.signers.len()}, Revisions: ${contract1.revisions.len()}`);
 | 
			
		||||
 | 
			
		||||
// Add more data
 | 
			
		||||
contract1 = contract1.add_signer(signer2).add_revision(revision2).current_version(revision2.version);
 | 
			
		||||
print(`Contract 1 Updated Signers: ${contract1.signers.len()}, Revisions: ${contract1.revisions.len()}, Current Version: ${contract1.current_version}`);
 | 
			
		||||
 | 
			
		||||
// Save the contract to the database
 | 
			
		||||
print("Saving contract1 to database...");
 | 
			
		||||
set_contract(contract1);
 | 
			
		||||
print("Contract1 saved.");
 | 
			
		||||
 | 
			
		||||
// Retrieve the contract
 | 
			
		||||
print(`Retrieving contract by ID (${contract1_base_id})...`);
 | 
			
		||||
let retrieved_contract = get_contract_by_id(contract1_base_id);
 | 
			
		||||
print(`Retrieved Contract: ${retrieved_contract.title}, Status: ${retrieved_contract.status}`);
 | 
			
		||||
print(`Retrieved Contract Signers: ${retrieved_contract.signers.len()}, Revisions: ${retrieved_contract.revisions.len()}`);
 | 
			
		||||
if retrieved_contract.signers.len() > 0 {
 | 
			
		||||
    print(`First signer of retrieved contract: ${retrieved_contract.signers[0].name}`);
 | 
			
		||||
}
 | 
			
		||||
if retrieved_contract.revisions.len() > 0 {
 | 
			
		||||
    print(`First revision content of retrieved contract: '${retrieved_contract.revisions[0].content}'`);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// --- Test updating a Contract ---
 | 
			
		||||
print("\n--- Testing Update for Contract ---");
 | 
			
		||||
let updated_contract = retrieved_contract
 | 
			
		||||
    .status(ContractStatusConstants::Active)
 | 
			
		||||
    .end_date(1700000000)
 | 
			
		||||
    .description("MSA (Active) between ACME Corp and Client Inc. with new addendum.");
 | 
			
		||||
 | 
			
		||||
print(`Updated Contract - Title: ${updated_contract.title}, Status: ${updated_contract.status}, End Date: ${format_optional_int(updated_contract.end_date, "N/A")}`);
 | 
			
		||||
set_contract(updated_contract); // Save updated
 | 
			
		||||
print("Updated Contract saved.");
 | 
			
		||||
 | 
			
		||||
let final_retrieved_contract = get_contract_by_id(contract1_base_id);
 | 
			
		||||
print(`Final Retrieved Contract - Status: ${final_retrieved_contract.status}, Description: '${final_retrieved_contract.description}'`);
 | 
			
		||||
 | 
			
		||||
print("\nLegal Rhai example script finished.");
 | 
			
		||||
		Reference in New Issue
	
	Block a user