Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Rhai Scripts

Runtime plugins using the Rhai scripting language.

Overview

Rhai is an embedded scripting language that enables:

  • Hot-reloadable plugins
  • Dynamic business logic
  • Safe sandboxing

Basic Script

// plugins/greet.rhai

fn process(input) {
    let name = input["name"];
    `Hello, ${name}!`
}

fn on_init() {
    print("Greeting plugin loaded!");
}

API Reference

Built-in Functions

// JSON
let data = json::parse(input);
let text = json::stringify(data);

// String
let upper = text.to_upper_case();
let parts = text.split(",");
let trimmed = text.trim();

// Collections
let list = [];
list.push(item);
let first = list[0];
let len = list.len();

// Math
let result = math::sqrt(16);
let rounded = math::round(3.7);

// Time
let now = time::now();
let formatted = time::format(now, "%Y-%m-%d");

HTTP (when enabled)

let response = http::get("https://api.example.com/data");
let json = json::parse(response.body);

Loading Scripts

#![allow(unused)]
fn main() {
use mofa_plugins::{RhaiPlugin, RhaiPluginManager};

let mut manager = RhaiPluginManager::new();

// Load from file
let plugin = RhaiPlugin::from_file("./plugins/my_plugin.rhai").await?;
manager.register(plugin).await?;

// Call function
let result = manager.call("process", json!({"name": "World"})).await?;
}

Hot Reloading

#![allow(unused)]
fn main() {
use mofa_plugins::HotReloadWatcher;

let watcher = HotReloadWatcher::new("./plugins/")?;

watcher.on_change(|path| async move {
    println!("Reloading: {:?}", path);
    manager.reload(&path).await?;
    Ok(())
});
}

See Also