Refactor to use Rhai packages for efficient engine creation
- Created OsirisPackage with all OSIRIS types and functions registered in the package - Functions now registered directly in package module (Note, Event, get_context) - Created ZdfzPackage extending OsirisPackage - Engine factory pattern: creates Engine::new_raw() + registers package (very cheap) - Removed TypeRegistry (unused code) - Simplified runner to use factory functions instead of passing packages - Package is created once, then factory efficiently creates engines on demand
This commit is contained in:
@@ -13,7 +13,7 @@
|
||||
/// ```
|
||||
|
||||
use clap::Parser;
|
||||
use osiris::rhai::{OsirisEngineConfig, create_osiris_engine_with_config};
|
||||
use osiris::{create_osiris_engine, OsirisContext};
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(author, version, about = "OSIRIS Rhai Script Runner", long_about = None)]
|
||||
@@ -36,11 +36,6 @@ struct Args {
|
||||
/// Script file to execute
|
||||
#[arg(short = 'f', long)]
|
||||
script_file: Option<String>,
|
||||
|
||||
/// Predefined instances in format: name:url:db_id (can be repeated)
|
||||
/// Example: --instance freezone:redis://localhost:6379:1
|
||||
#[arg(short = 'i', long = "instance")]
|
||||
instances: Vec<String>,
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
@@ -52,39 +47,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("🚀 OSIRIS Runner");
|
||||
println!("Runner ID: {}", args.runner_id);
|
||||
println!("HeroDB: {} (DB {})", args.redis_url, args.db_id);
|
||||
|
||||
// Parse predefined instances
|
||||
let mut config = OsirisEngineConfig::new();
|
||||
|
||||
if args.instances.is_empty() {
|
||||
// No predefined instances, use default with runner_id as owner
|
||||
config.add_context("default", &args.runner_id, &args.redis_url, args.db_id);
|
||||
} else {
|
||||
// Parse instance definitions (format: name:url:db_id)
|
||||
// We need to split carefully since URL contains colons
|
||||
for instance_def in &args.instances {
|
||||
// Find the first colon (name separator)
|
||||
let first_colon = instance_def.find(':')
|
||||
.ok_or_else(|| format!("Invalid instance format: '{}'. Expected: name:url:db_id", instance_def))?;
|
||||
|
||||
let name = &instance_def[..first_colon];
|
||||
let rest = &instance_def[first_colon + 1..];
|
||||
|
||||
// Find the last colon (db_id separator)
|
||||
let last_colon = rest.rfind(':')
|
||||
.ok_or_else(|| format!("Invalid instance format: '{}'. Expected: name:url:db_id", instance_def))?;
|
||||
|
||||
let url = &rest[..last_colon];
|
||||
let db_id_str = &rest[last_colon + 1..];
|
||||
|
||||
let db_id: u16 = db_id_str.parse()
|
||||
.map_err(|_| format!("Invalid db_id in instance '{}': {}", instance_def, db_id_str))?;
|
||||
|
||||
config.add_context(name, &args.runner_id, url, db_id);
|
||||
println!(" Context: {} → {} (DB {})", name, url, db_id);
|
||||
}
|
||||
}
|
||||
|
||||
println!();
|
||||
|
||||
// Determine script source
|
||||
@@ -100,8 +62,18 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("📝 Executing script...\n");
|
||||
println!("─────────────────────────────────────");
|
||||
|
||||
// Create engine with predefined contexts
|
||||
let (engine, mut scope) = create_osiris_engine_with_config(config)?;
|
||||
// Create engine
|
||||
let mut engine = create_osiris_engine()?;
|
||||
|
||||
// Set up context tags with SIGNATORIES
|
||||
let mut tag_map = rhai::Map::new();
|
||||
let signatories: rhai::Array = vec![rhai::Dynamic::from(args.runner_id.clone())];
|
||||
tag_map.insert("SIGNATORIES".into(), rhai::Dynamic::from(signatories));
|
||||
tag_map.insert("HERODB_URL".into(), rhai::Dynamic::from(args.redis_url.clone()));
|
||||
tag_map.insert("DB_ID".into(), rhai::Dynamic::from(args.db_id as i64));
|
||||
engine.set_default_tag(rhai::Dynamic::from(tag_map));
|
||||
|
||||
let mut scope = rhai::Scope::new();
|
||||
|
||||
match engine.eval_with_scope::<rhai::Dynamic>(&mut scope, &script_content) {
|
||||
Ok(result) => {
|
||||
|
||||
Reference in New Issue
Block a user