Skip to main content

LuaComponent

Struct LuaComponent 

Source
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

Source

pub fn from_file<P: AsRef<Path>>( path: P, sandbox: Arc<dyn SandboxPolicy>, ) -> Result<Self, LuaError>

Creates a new LuaComponent from a script file.

§Arguments
  • path - Path to the Lua script
  • sandbox - Sandbox policy for file operations and exec cwd
§Errors

Returns error if:

  • Script file not found
  • Script syntax error
  • Missing required fields/callbacks
Source

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.

Source

pub fn from_script( script: &str, sandbox: Arc<dyn SandboxPolicy>, ) -> Result<Self, LuaError>

Creates a new LuaComponent from a script string.

§Arguments
  • script - Lua script content
  • sandbox - Sandbox policy for file operations and exec cwd
§Errors

Returns error if script is invalid.

Source

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.

Source

pub fn script_path(&self) -> Option<&str>

Returns the script path if loaded from file.

Source

pub fn reload(&mut self) -> Result<(), LuaError>

Reloads the script from file.

§Errors

Returns error if reload fails.

Source

pub fn has_emitter(&self) -> bool

Returns whether this component has an emitter set.

When true, the component can emit events via orcs.output().

Source

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().

Source

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 child
  • orcs.child_count() - Get current child count
  • orcs.max_children() - Get max allowed children
§Arguments
  • ctx - The child context

Trait Implementations§

Source§

impl Component for LuaComponent

Source§

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

Returns the component’s identifier. Read more
Source§

fn subscriptions(&self) -> &[EventCategory]

Returns the event categories this component subscribes to. Read more
Source§

fn subscription_entries(&self) -> Vec<SubscriptionEntry>

Returns subscription entries with optional operation-level filtering. Read more
Source§

fn runtime_hints(&self) -> RuntimeHints

Returns runtime hints for this component. Read more
Source§

fn status(&self) -> Status

Returns the current execution status. Read more
Source§

fn on_request(&mut self, request: &Request) -> Result<JsonValue, ComponentError>

Handle an incoming request. Read more
Source§

fn on_signal(&mut self, signal: &Signal) -> SignalResponse

Handle an incoming signal. Read more
Source§

fn abort(&mut self)

Immediate abort. Read more
Source§

fn shutdown(&mut self)

Shutdown the component. Read more
Source§

fn snapshot(&self) -> Result<ComponentSnapshot, SnapshotError>

Captures the component’s current state as a snapshot. Read more
Source§

fn restore(&mut self, snapshot: &ComponentSnapshot) -> Result<(), SnapshotError>

Restores the component’s state from a snapshot. Read more
Source§

fn set_emitter(&mut self, emitter: Box<dyn Emitter>)

Sets the event emitter for this component. Read more
Source§

fn set_child_context(&mut self, ctx: Box<dyn ChildContext>)

Sets the child context for this component. Read more
Source§

fn status_detail(&self) -> Option<StatusDetail>

Returns detailed status information. Read more
Source§

fn as_packageable(&self) -> Option<&dyn Packageable>

Returns this component as a Packageable if supported. Read more
Source§

fn as_packageable_mut(&mut self) -> Option<&mut dyn Packageable>

Returns this component as a mutable Packageable if supported. Read more
Source§

impl Send for LuaComponent

Source§

impl Sync for LuaComponent

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> MaybeSend for T
where T: Send,