armature_rhai/
lib.rs

1//! # Armature Rhai
2//!
3//! Rhai scripting language integration for Armature applications.
4//!
5//! Write HTTP handlers, middleware, and business logic in Rhai scripts while
6//! leveraging Armature's high-performance Rust core.
7//!
8//! ## Features
9//!
10//! - **Script Handlers**: Define HTTP handlers in `.rhai` files
11//! - **Hot Reload**: Automatic script reloading during development
12//! - **Type-Safe Bindings**: Access HTTP requests, responses, and Armature features
13//! - **Sandboxed Execution**: Safe script execution with configurable limits
14//! - **Performance**: Compiled scripts with caching for production
15//!
16//! ## Quick Start
17//!
18//! ```rust,ignore
19//! use armature_rhai::{RhaiEngine, ScriptRouter};
20//!
21//! #[tokio::main]
22//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
23//!     // Create Rhai engine with Armature bindings
24//!     let engine = RhaiEngine::new()
25//!         .with_max_operations(100_000)
26//!         .with_scripts_dir("./scripts")
27//!         .build()?;
28//!
29//!     // Create router with script-based handlers
30//!     let router = ScriptRouter::new(engine)
31//!         .route("/", "handlers/index.rhai")
32//!         .route("/users/:id", "handlers/users.rhai")
33//!         .route("/api/*", "handlers/api.rhai");
34//!
35//!     // Run server
36//!     router.serve("0.0.0.0:8080").await?;
37//!     Ok(())
38//! }
39//! ```
40//!
41//! ## Script Example
42//!
43//! ```rhai
44//! // handlers/users.rhai
45//!
46//! // Access request data
47//! let user_id = request.param("id");
48//! let method = request.method();
49//!
50//! // Handle different methods
51//! if method == "GET" {
52//!     // Return JSON response
53//!     response.json(#{
54//!         id: user_id,
55//!         name: "Alice",
56//!         email: "alice@example.com"
57//!     })
58//! } else if method == "PUT" {
59//!     let body = request.json();
60//!     // Process update...
61//!     response.ok()
62//! } else {
63//!     response.method_not_allowed()
64//! }
65//! ```
66//!
67//! ## Hot Reload (Development)
68//!
69//! Enable the `hot-reload` feature for automatic script reloading:
70//!
71//! ```rust,ignore
72//! let engine = RhaiEngine::new()
73//!     .with_hot_reload(true)
74//!     .build()?;
75//! ```
76
77mod bindings;
78mod context;
79mod engine;
80mod error;
81mod handler;
82mod router;
83mod script;
84
85#[cfg(feature = "hot-reload")]
86mod watcher;
87
88pub use bindings::{register_armature_api, RequestBinding, ResponseBinding};
89pub use context::ScriptContext;
90pub use engine::{RhaiEngine, RhaiEngineBuilder};
91pub use error::{RhaiError, Result};
92pub use handler::{ScriptHandler, ScriptMiddleware};
93pub use router::ScriptRouter;
94pub use script::{CompiledScript, ScriptCache, ScriptLoader};
95
96#[cfg(feature = "hot-reload")]
97pub use watcher::ScriptWatcher;
98
99// Re-export rhai for advanced usage
100pub use rhai;
101