add heroledger models
This commit is contained in:
53
heromodels/examples/heroledger/heroledger.rhai
Normal file
53
heromodels/examples/heroledger/heroledger.rhai
Normal file
@@ -0,0 +1,53 @@
|
||||
// heroledger.rhai - Demonstration of HeroLedger models in Rhai
|
||||
|
||||
print("=== HeroLedger Models Demo ===");
|
||||
|
||||
// Create a new user
|
||||
print("\n--- Creating User ---");
|
||||
let new_user = new_user()
|
||||
.name("Alice Johnson")
|
||||
.email("alice@herocode.com")
|
||||
.pubkey("0x1234567890abcdef")
|
||||
.status("Active")
|
||||
.save_user();
|
||||
|
||||
print("Created user: " + new_user.get_name());
|
||||
print("User ID: " + new_user.get_id());
|
||||
print("User email: " + new_user.get_email());
|
||||
print("User pubkey: " + new_user.get_pubkey());
|
||||
|
||||
// Create a new group
|
||||
print("\n--- Creating Group ---");
|
||||
let new_group = new_group()
|
||||
.name("HeroCode Developers")
|
||||
.description("A group for HeroCode development team members")
|
||||
.visibility("Public")
|
||||
.save_group();
|
||||
|
||||
print("Created group: " + new_group.get_name());
|
||||
print("Group ID: " + new_group.get_id());
|
||||
print("Group description: " + new_group.get_description());
|
||||
|
||||
// Create a new account
|
||||
print("\n--- Creating Account ---");
|
||||
let new_account = new_account()
|
||||
.name("Alice's Main Account")
|
||||
.description("Primary account for Alice Johnson")
|
||||
.currency("USD")
|
||||
.save_account();
|
||||
|
||||
print("Created account: " + new_account.get_name());
|
||||
print("Account ID: " + new_account.get_id());
|
||||
print("Account currency: " + new_account.get_currency());
|
||||
|
||||
// Create a new DNS zone
|
||||
print("\n--- Creating DNS Zone ---");
|
||||
let new_dns_zone = new_dns_zone()
|
||||
.name("herocode.com")
|
||||
.description("Main domain for HeroCode")
|
||||
.save_dns_zone();
|
||||
|
||||
print("Created DNS zone: " + new_dns_zone.get_name());
|
||||
print("DNS zone ID: " + new_dns_zone.get_id());
|
||||
|
||||
print("\n=== Demo Complete ===");
|
50
heromodels/examples/heroledger/main.rs
Normal file
50
heromodels/examples/heroledger/main.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
use heromodels_core::db::hero::OurDB;
|
||||
use rhai::{Dynamic, Engine};
|
||||
use heromodels::models::heroledger::rhai::register_heroledger_rhai_modules;
|
||||
use std::sync::Arc;
|
||||
use std::{fs, path::Path};
|
||||
|
||||
const CALLER_ID: &str = "example_caller";
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Initialize Rhai engine
|
||||
let mut engine = Engine::new();
|
||||
|
||||
// Initialize database with OurDB
|
||||
let db_path = "temp_heroledger_db";
|
||||
// Clean up previous database file if it exists
|
||||
if Path::new(db_path).exists() {
|
||||
fs::remove_dir_all(db_path)?;
|
||||
}
|
||||
let _db = Arc::new(OurDB::new(db_path, true).expect("Failed to create database"));
|
||||
|
||||
// Register the heroledger modules with Rhai
|
||||
register_heroledger_rhai_modules(&mut engine);
|
||||
|
||||
let mut db_config = rhai::Map::new();
|
||||
db_config.insert("DB_PATH".into(), db_path.into());
|
||||
db_config.insert("CALLER_ID".into(), CALLER_ID.into());
|
||||
db_config.insert("CONTEXT_ID".into(), CALLER_ID.into());
|
||||
engine.set_default_tag(Dynamic::from(db_config)); // Or pass via CallFnOptions
|
||||
|
||||
// Load and evaluate the Rhai script
|
||||
let manifest_dir = env!("CARGO_MANIFEST_DIR");
|
||||
let script_path = Path::new(manifest_dir)
|
||||
.join("examples")
|
||||
.join("heroledger")
|
||||
.join("heroledger.rhai");
|
||||
println!("Script path: {}", script_path.display());
|
||||
let script = fs::read_to_string(&script_path)?;
|
||||
|
||||
println!("--- Running HeroLedger Rhai Script ---");
|
||||
match engine.eval::<()>(&script) {
|
||||
Ok(_) => println!("\n--- Script executed successfully! ---"),
|
||||
Err(e) => eprintln!("\n--- Script execution failed: {} ---", e),
|
||||
}
|
||||
|
||||
// Clean up the database file
|
||||
fs::remove_dir_all(db_path)?;
|
||||
println!("--- Cleaned up temporary database. ---");
|
||||
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user