forked from extern/nushell
# 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 😄
72 lines
2.2 KiB
Rust
72 lines
2.2 KiB
Rust
use crate::{second_custom_value::SecondCustomValue, CustomValuePlugin};
|
|
use nu_plugin::{EngineInterface, EvaluatedCall, SimplePluginCommand};
|
|
use nu_protocol::{Category, Example, LabeledError, Signature, Span, SyntaxShape, Value};
|
|
|
|
pub struct Generate2;
|
|
|
|
impl SimplePluginCommand for Generate2 {
|
|
type Plugin = CustomValuePlugin;
|
|
|
|
fn name(&self) -> &str {
|
|
"custom-value generate2"
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"PluginSignature for a plugin that generates a different custom value"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build(self.name())
|
|
.optional(
|
|
"closure",
|
|
SyntaxShape::Closure(Some(vec![SyntaxShape::Any])),
|
|
"An optional closure to pass the custom value to",
|
|
)
|
|
.category(Category::Experimental)
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![
|
|
Example {
|
|
example: "custom-value generate2",
|
|
description: "Generate a new SecondCustomValue",
|
|
result: Some(SecondCustomValue::new("xyz").into_value(Span::test_data())),
|
|
},
|
|
Example {
|
|
example: "custom-value generate2 { print }",
|
|
description: "Generate a new SecondCustomValue and pass it to a closure",
|
|
result: None,
|
|
},
|
|
]
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
_plugin: &CustomValuePlugin,
|
|
engine: &EngineInterface,
|
|
call: &EvaluatedCall,
|
|
_input: &Value,
|
|
) -> Result<Value, LabeledError> {
|
|
let second_custom_value = SecondCustomValue::new("xyz").into_value(call.head);
|
|
// If we were passed a closure, execute that instead
|
|
if let Some(closure) = call.opt(0)? {
|
|
let result = engine.eval_closure(
|
|
&closure,
|
|
vec![second_custom_value.clone()],
|
|
Some(second_custom_value),
|
|
)?;
|
|
Ok(result)
|
|
} else {
|
|
Ok(second_custom_value)
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn test_examples() -> Result<(), nu_protocol::ShellError> {
|
|
use nu_plugin_test_support::PluginTest;
|
|
|
|
PluginTest::new("custom_values", crate::CustomValuePlugin.into())?
|
|
.test_command_examples(&Generate2)
|
|
}
|