Files
runner_rust/src/engine/osiris.rs
2025-10-20 22:22:34 +02:00

97 lines
2.7 KiB
Rust

/// OSIRIS Rhai Engine Integration
///
/// This module provides a Rhai engine configured with OSIRIS object support.
/// It allows Rhai scripts to create Notes, Events, and other OSIRIS objects
/// using a fluent builder pattern and store/retrieve them from HeroDB.
///
/// # Example Rhai Script
///
/// ```rhai
/// // Create a note with builder pattern
/// let note = note("notes")
/// .title("My First Note")
/// .content("This is the content of my note")
/// .tag("topic", "rust")
/// .tag("project", "osiris")
/// .mime("text/plain");
///
/// // Store the note
/// let id = put_note(note);
/// print(`Note stored with ID: ${id}`);
///
/// // Retrieve the note
/// let retrieved = get_note("notes", id);
/// print(`Retrieved: ${retrieved.get_title()}`);
///
/// // Query by tag
/// let ids = query("notes", "tags:tag", "project=osiris");
/// print(`Found ${ids.len()} notes`);
/// ```
use osiris::rhai_support::{register_note_api, register_event_api, OsirisInstance};
use rhai::Engine;
/// Create a new Rhai engine with OSIRIS support
///
/// # Arguments
/// * `herodb_url` - HeroDB connection URL (e.g., "redis://localhost:6379")
/// * `db_id` - Database ID to use
///
/// # Returns
/// A configured Rhai engine with OSIRIS objects and methods registered
pub fn create_osiris_engine(
herodb_url: &str,
db_id: u16,
) -> Result<Engine, Box<dyn std::error::Error>> {
let mut engine = Engine::new();
// Register Note and Event APIs
register_note_api(&mut engine);
register_event_api(&mut engine);
// Register OsirisInstance type
engine.build_type::<OsirisInstance>();
// Register a function to create OSIRIS instances
engine.register_fn("osiris", move |name: &str, url: &str, db_id: rhai::INT| -> Result<OsirisInstance, Box<rhai::EvalAltResult>> {
OsirisInstance::new(name, url, db_id as u16)
.map_err(|e| format!("Failed to create OSIRIS instance: {}", e).into())
});
Ok(engine)
}
/// Example: Run a Rhai script with OSIRIS support
pub fn run_osiris_script(
script: &str,
herodb_url: &str,
db_id: u16,
) -> Result<(), Box<dyn std::error::Error>> {
let engine = create_osiris_engine(herodb_url, db_id)?;
engine.run(script)?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_engine() {
let engine = create_osiris_engine("redis://localhost:6379", 1);
assert!(engine.is_ok());
}
#[test]
#[ignore] // Requires HeroDB running
fn test_run_script() {
let script = r#"
let note = note("notes");
print(note);
"#;
let result = run_osiris_script(script, "redis://localhost:6379", 1);
assert!(result.is_ok());
}
}