Skip to main content

ComponentLoader

Trait ComponentLoader 

Source
pub trait ComponentLoader: Send + Sync {
    // Required method
    fn load_from_script(
        &self,
        script: &str,
        id: Option<&str>,
        globals: Option<&Map<String, Value>>,
    ) -> Result<Box<dyn Component>, SpawnError>;

    // Provided method
    fn resolve_builtin(&self, _name: &str) -> Option<String> { ... }
}
Expand description

Loader for creating Components from scripts.

This trait allows runtime implementations to create Components without depending on specific implementations (e.g., LuaComponent).

Required Methods§

Source

fn load_from_script( &self, script: &str, id: Option<&str>, globals: Option<&Map<String, Value>>, ) -> Result<Box<dyn Component>, SpawnError>

Creates a Component from inline script content.

§Arguments
  • script - Inline script content

  • id - Optional component ID (extracted from script if None)

  • globals - Optional key-value pairs to inject into the VM before script execution. Each entry becomes a global variable in the new VM, enabling structured data passing without string-based code injection.

    The type is Map<String, Value> (a JSON object) rather than a generic serde_json::Value so that the compiler enforces the “must be an object” invariant at the call site. This follows the parse, don’t validate principle — callers convert to Map once, and downstream code never needs to re-check or use unreachable!() branches.

§Returns

A boxed Component, or an error if loading failed.

Provided Methods§

Source

fn resolve_builtin(&self, _name: &str) -> Option<String>

Resolves a builtin component name to its script content.

§Default Implementation

Returns None (no builtins available).

Implementors§