# Description
This is something that was discussed in the core team meeting last
Wednesday. @ayax79 is building `nu-plugin-polars` with all of the
dataframe commands into a plugin, and there are a lot of them, so it
would help to make the API more similar. At the same time, I think the
`Command` API is just better anyway. I don't think the difference is
justified, and the types for core commands have the benefit of requiring
less `.into()` because they often don't own their data
- Broke `signature()` up into `name()`, `usage()`, `extra_usage()`,
`search_terms()`, `examples()`
- `signature()` returns `nu_protocol::Signature`
- `examples()` returns `Vec<nu_protocol::Example>`
- `PluginSignature` and `PluginExample` no longer need to be used by
plugin developers
# User-Facing Changes
Breaking API for plugins yet again 😄
108 lines
2.9 KiB
Rust
108 lines
2.9 KiB
Rust
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
|
|
use nu_protocol::{Category, Example, LabeledError, PipelineData, Signature, Span, Type, Value};
|
|
|
|
use crate::ExamplePlugin;
|
|
|
|
/// `<list> | example sum`
|
|
pub struct Sum;
|
|
|
|
impl PluginCommand for Sum {
|
|
type Plugin = ExamplePlugin;
|
|
|
|
fn name(&self) -> &str {
|
|
"example sum"
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Example stream consumer for a list of values"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build(self.name())
|
|
.input_output_types(vec![
|
|
(Type::List(Type::Int.into()), Type::Int),
|
|
(Type::List(Type::Float.into()), Type::Float),
|
|
])
|
|
.category(Category::Experimental)
|
|
}
|
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
|
vec!["example"]
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![Example {
|
|
example: "example seq 1 5 | example sum",
|
|
description: "sum values from 1 to 5",
|
|
result: Some(Value::test_int(15)),
|
|
}]
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
_plugin: &ExamplePlugin,
|
|
_engine: &EngineInterface,
|
|
call: &EvaluatedCall,
|
|
input: PipelineData,
|
|
) -> Result<PipelineData, LabeledError> {
|
|
let mut acc = IntOrFloat::Int(0);
|
|
for value in input {
|
|
if let Ok(n) = value.as_i64() {
|
|
acc.add_i64(n);
|
|
} else if let Ok(n) = value.as_f64() {
|
|
acc.add_f64(n);
|
|
} else {
|
|
return Err(LabeledError::new("Sum only accepts ints and floats")
|
|
.with_label(format!("found {} in input", value.get_type()), value.span())
|
|
.with_label("can't be used here", call.head));
|
|
}
|
|
}
|
|
Ok(PipelineData::Value(acc.to_value(call.head), None))
|
|
}
|
|
}
|
|
|
|
/// Accumulates numbers into either an int or a float. Changes type to float on the first
|
|
/// float received.
|
|
#[derive(Clone, Copy)]
|
|
enum IntOrFloat {
|
|
Int(i64),
|
|
Float(f64),
|
|
}
|
|
|
|
impl IntOrFloat {
|
|
pub(crate) fn add_i64(&mut self, n: i64) {
|
|
match self {
|
|
IntOrFloat::Int(ref mut v) => {
|
|
*v += n;
|
|
}
|
|
IntOrFloat::Float(ref mut v) => {
|
|
*v += n as f64;
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(crate) fn add_f64(&mut self, n: f64) {
|
|
match self {
|
|
IntOrFloat::Int(v) => {
|
|
*self = IntOrFloat::Float(*v as f64 + n);
|
|
}
|
|
IntOrFloat::Float(ref mut v) => {
|
|
*v += n;
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(crate) fn to_value(self, span: Span) -> Value {
|
|
match self {
|
|
IntOrFloat::Int(v) => Value::int(v, span),
|
|
IntOrFloat::Float(v) => Value::float(v, span),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn test_examples() -> Result<(), nu_protocol::ShellError> {
|
|
use nu_plugin_test_support::PluginTest;
|
|
PluginTest::new("example", ExamplePlugin.into())?.test_command_examples(&Sum)
|
|
}
|