added an example using IntoValue

This commit is contained in:
Tim 'Piepmatz' Hesse 2024-06-25 13:36:12 +02:00
parent 4f6c0cd8a2
commit 3fc25cc215

View File

@ -1,5 +1,5 @@
use nu_plugin::{EngineInterface, EvaluatedCall, SimplePluginCommand}; use nu_plugin::{EngineInterface, EvaluatedCall, SimplePluginCommand};
use nu_protocol::{record, Category, LabeledError, Signature, SyntaxShape, Value}; use nu_protocol::{Category, IntoValue, LabeledError, Signature, SyntaxShape, Value};
use crate::ExamplePlugin; use crate::ExamplePlugin;
@ -38,14 +38,22 @@ impl SimplePluginCommand for Two {
) -> Result<Value, LabeledError> { ) -> Result<Value, LabeledError> {
plugin.print_values(2, call, input)?; plugin.print_values(2, call, input)?;
// Use the IntoValue derive macro and trait to easily design output data.
#[derive(IntoValue)]
struct Output {
one: i64,
two: i64,
three: i64,
}
let vals = (0..10i64) let vals = (0..10i64)
.map(|i| { .map(|i| {
let record = record! { Output {
"one" => Value::int(i, call.head), one: i,
"two" => Value::int(2 * i, call.head), two: 2 * i,
"three" => Value::int(3 * i, call.head), three: 3 * i,
}; }
Value::record(record, call.head) .into_value(call.head)
}) })
.collect(); .collect();