pub trait Plugin {
// Required methods
fn signature(&self) -> Vec<PluginSignature>;
fn run(
&mut self,
name: &str,
config: &Option<Value>,
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.
If large amounts of data are expected to need to be received or produced, it may be more appropriate to implement StreamingPlugin instead.
§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,
config: &Option<Value>,
call: &EvaluatedCall,
input: &Value,
) -> Result<Value, LabeledError> {
Ok(Value::string("Hello, World!".to_owned(), call.head))
}
}
Required Methods§
sourcefn signature(&self) -> Vec<PluginSignature>
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.
sourcefn run(
&mut self,
name: &str,
config: &Option<Value>,
call: &EvaluatedCall,
input: &Value
) -> Result<Value, LabeledError>
fn run( &mut self, name: &str, config: &Option<Value>, 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.
This variant does not support streaming. Consider implementing StreamingPlugin instead if streaming is desired.