pub struct LuaComponent { /* private fields */ }Expand description
A component implemented in Lua.
Loads a Lua script and delegates Component trait methods to Lua functions.
§Script Format
The Lua script must return a table with the following structure:
return {
id = "component-id", -- Required: unique identifier
subscriptions = {"Echo"}, -- Required: event categories
on_request = function(req) -- Required: handle requests
return { success = true, data = ... }
end,
on_signal = function(sig) -- Required: handle signals
return "Handled" | "Ignored" | "Abort"
end,
init = function() -- Optional: initialization
end,
shutdown = function() -- Optional: cleanup
end,
}Implementations§
Source§impl LuaComponent
impl LuaComponent
Sourcepub fn from_file<P: AsRef<Path>>(
path: P,
sandbox: Arc<dyn SandboxPolicy>,
) -> Result<Self, LuaError>
pub fn from_file<P: AsRef<Path>>( path: P, sandbox: Arc<dyn SandboxPolicy>, ) -> Result<Self, LuaError>
Sourcepub fn from_dir<P: AsRef<Path>>(
dir: P,
sandbox: Arc<dyn SandboxPolicy>,
) -> Result<Self, LuaError>
pub fn from_dir<P: AsRef<Path>>( dir: P, sandbox: Arc<dyn SandboxPolicy>, ) -> Result<Self, LuaError>
Creates a new LuaComponent from a directory containing init.lua.
The directory is added to Lua’s package.path, enabling standard
require() for co-located modules (e.g. require("lib.my_module")).
§Directory Structure
components/my_component/
init.lua -- entry point (must return component table)
lib/
helper.lua -- require("lib.helper")
vendor/
lua_solver/init.lua -- require("vendor.lua_solver")§Errors
Returns error if init.lua not found or script is invalid.
Sourcepub fn from_script(
script: &str,
sandbox: Arc<dyn SandboxPolicy>,
) -> Result<Self, LuaError>
pub fn from_script( script: &str, sandbox: Arc<dyn SandboxPolicy>, ) -> Result<Self, LuaError>
Sourcepub fn from_script_with_globals(
script: &str,
sandbox: Arc<dyn SandboxPolicy>,
globals: Option<&Map<String, Value>>,
) -> Result<Self, LuaError>
pub fn from_script_with_globals( script: &str, sandbox: Arc<dyn SandboxPolicy>, globals: Option<&Map<String, Value>>, ) -> Result<Self, LuaError>
Creates a new LuaComponent from a script string with pre-injected globals.
Each top-level key in globals is set as a Lua global variable before
the script executes, enabling structured data passing without string
serialization.
§Design: Map<String, Value> instead of serde_json::Value
The parameter accepts Map<String, Value> (a JSON object) rather than
an arbitrary serde_json::Value. This follows the parse, don’t validate
principle: callers must produce a Map at the boundary, so this function
never encounters non-object values and needs no unreachable!() fallback.
See ComponentLoader::load_from_script for the trait-level rationale.
Sourcepub fn script_path(&self) -> Option<&str>
pub fn script_path(&self) -> Option<&str>
Returns the script path if loaded from file.
Sourcepub fn has_emitter(&self) -> bool
pub fn has_emitter(&self) -> bool
Returns whether this component has an emitter set.
When true, the component can emit events via orcs.output().
Sourcepub fn has_child_context(&self) -> bool
pub fn has_child_context(&self) -> bool
Returns whether this component has a child context set.
When true, the component can spawn children via orcs.spawn_child().
Sourcepub fn set_child_context(&mut self, ctx: Box<dyn ChildContext>)
pub fn set_child_context(&mut self, ctx: Box<dyn ChildContext>)
Sets the child context for spawning and managing children.
Once set, the Lua script can use:
orcs.spawn_child(config)- Spawn a childorcs.child_count()- Get current child countorcs.max_children()- Get max allowed children
§Arguments
ctx- The child context
Trait Implementations§
Source§impl Component for LuaComponent
impl Component for LuaComponent
Source§fn init(&mut self, config: &Value) -> Result<(), ComponentError>
fn init(&mut self, config: &Value) -> Result<(), ComponentError>
Calls the Lua init(cfg) callback with per-component settings.
config contains [components.settings.<name>] from config.toml,
plus _global (injected by builder) with global config fields.
Null or empty objects are passed as nil to Lua.
Source§fn id(&self) -> &ComponentId
fn id(&self) -> &ComponentId
Source§fn subscriptions(&self) -> &[EventCategory]
fn subscriptions(&self) -> &[EventCategory]
Source§fn subscription_entries(&self) -> Vec<SubscriptionEntry>
fn subscription_entries(&self) -> Vec<SubscriptionEntry>
Source§fn runtime_hints(&self) -> RuntimeHints
fn runtime_hints(&self) -> RuntimeHints
Source§fn on_request(&mut self, request: &Request) -> Result<JsonValue, ComponentError>
fn on_request(&mut self, request: &Request) -> Result<JsonValue, ComponentError>
Source§fn on_signal(&mut self, signal: &Signal) -> SignalResponse
fn on_signal(&mut self, signal: &Signal) -> SignalResponse
Source§fn snapshot(&self) -> Result<ComponentSnapshot, SnapshotError>
fn snapshot(&self) -> Result<ComponentSnapshot, SnapshotError>
Source§fn restore(&mut self, snapshot: &ComponentSnapshot) -> Result<(), SnapshotError>
fn restore(&mut self, snapshot: &ComponentSnapshot) -> Result<(), SnapshotError>
Source§fn set_emitter(&mut self, emitter: Box<dyn Emitter>)
fn set_emitter(&mut self, emitter: Box<dyn Emitter>)
Source§fn set_child_context(&mut self, ctx: Box<dyn ChildContext>)
fn set_child_context(&mut self, ctx: Box<dyn ChildContext>)
Source§fn status_detail(&self) -> Option<StatusDetail>
fn status_detail(&self) -> Option<StatusDetail>
Source§fn as_packageable(&self) -> Option<&dyn Packageable>
fn as_packageable(&self) -> Option<&dyn Packageable>
Packageable if supported. Read moreSource§fn as_packageable_mut(&mut self) -> Option<&mut dyn Packageable>
fn as_packageable_mut(&mut self) -> Option<&mut dyn Packageable>
Packageable if supported. Read moreimpl Send for LuaComponent
impl Sync for LuaComponent
Auto Trait Implementations§
impl !Freeze for LuaComponent
impl !RefUnwindSafe for LuaComponent
impl Unpin for LuaComponent
impl UnsafeUnpin for LuaComponent
impl !UnwindSafe for LuaComponent
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more