Trait nu_plugin::Plugin

source ·
pub trait Plugin {
    // Required methods
    fn signature(&self) -> Vec<PluginSignature>;
    fn run(
        &mut self,
        name: &str,
        call: &EvaluatedCall,
        input: &Value
    ) -> Result<Value, LabeledError>;
}
Expand description

The basic API for a Nushell plugin

This is the trait that Nushell plugins must implement. The methods defined on Plugin are invoked by serve_plugin during plugin registration and execution.

Examples

Basic usage:

struct HelloPlugin;

impl Plugin for HelloPlugin {
    fn signature(&self) -> Vec<PluginSignature> {
        let sig = PluginSignature::build("hello")
            .input_output_type(Type::Nothing, Type::String);

        vec![sig]
    }

    fn run(
        &mut self,
        name: &str,
        call: &EvaluatedCall,
        input: &Value,
    ) -> Result<Value, LabeledError> {
        Ok(Value::String {
            val: "Hello, World!".to_owned(),
            span: call.head,
        })
    }
}

Required Methods§

source

fn signature(&self) -> Vec<PluginSignature>

The signature of the plugin

This method returns the PluginSignatures that describe the capabilities of this plugin. Since a single plugin executable can support multiple invocation patterns we return a Vec of signatures.

source

fn run( &mut self, name: &str, call: &EvaluatedCall, input: &Value ) -> Result<Value, LabeledError>

Perform the actual behavior of the plugin

The behavior of the plugin is defined by the implementation of this method. When Nushell invoked the plugin serve_plugin will call this method and print the serialized returned value or error to stdout, which Nushell will interpret.

The name is only relevant for plugins that implement multiple commands as the invoked command will be passed in via this argument. The call contains metadata describing how the plugin was invoked and input contains the structured data passed to the command implemented by this Plugin.

Implementors§