Skip to main content

Module plugin

Module plugin 

Source
Expand description

Plugin system for extending ccalc with new built-in functions. Plugin system for extending ccalc with new built-in functions.

Third-party crates implement the plugin::Plugin trait and register via plugin::register_plugin. The engine checks the registry before its own built-in table, so plugins can shadow any existing built-in if needed.

§Minimal plugin example

use ccalc_engine::env::{Env, Value};
use ccalc_engine::plugin::Plugin;

pub struct MyPlugin;

impl Plugin for MyPlugin {
    fn name(&self) -> &str { "myfunc" }

    fn call(&self, _name: &str, args: &[Value], _env: &Env) -> Result<Value, String> {
        if args.is_empty() {
            return Err("myfunc: at least one argument required".into());
        }
        Ok(args[0].clone())
    }
}

// In main.rs / startup:
ccalc_engine::plugin::register_plugin(Box::new(MyPlugin));

Plugins that expose several names (e.g. a plot plugin with plot, scatter, bar, …) should also override plugin::Plugin::exported_names and return a const-backed slice. The name argument to call identifies which exported name was invoked, enabling a single plugin to dispatch multiple functions:

const NAMES: &[&str] = &["plot", "scatter", "bar"];

fn exported_names(&self) -> &[&str] { NAMES }

fn call(&self, name: &str, args: &[Value], _env: &Env) -> Result<Value, String> {
    match name {
        "plot"    => { /* ... */ Ok(Value::Void) }
        "scatter" => { /* ... */ Ok(Value::Void) }
        _         => Err(format!("{name}: not implemented"))
    }
}

Structs§

PluginRegistry
Registry that maps exported names to their plugin implementations.

Traits§

Plugin
Trait that all ccalc plugins must implement.

Functions§

plugin_names
Returns all names exported by currently registered plugins.
register_plugin
Registers a plugin in the thread-local plugin registry.