forked from extern/nushell
# Description The meaning of the word usage is specific to describing how a command function is *used* and not a synonym for general description. Usage can be used to describe the SYNOPSIS or EXAMPLES sections of a man page where the permitted argument combinations are shown or example *uses* are given. Let's not confuse people and call it what it is a description. Our `help` command already creates its own *Usage* section based on the available arguments and doesn't refer to the description with usage. # User-Facing Changes `help commands` and `scope commands` will now use `description` or `extra_description` `usage`-> `description` `extra_usage` -> `extra_description` Breaking change in the plugin protocol: In the signature record communicated with the engine. `usage`-> `description` `extra_usage` -> `extra_description` The same rename also takes place for the methods on `SimplePluginCommand` and `PluginCommand` # Tests + Formatting - Updated plugin protocol specific changes # After Submitting - [ ] update plugin protocol doc
117 lines
3.4 KiB
Rust
117 lines
3.4 KiB
Rust
use crate::{values::CustomValueSupport, PolarsPlugin};
|
|
|
|
use super::super::values::{Column, NuDataFrame};
|
|
|
|
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
|
|
use nu_protocol::{
|
|
Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, Type, Value,
|
|
};
|
|
|
|
#[derive(Clone)]
|
|
pub struct AllTrue;
|
|
|
|
impl PluginCommand for AllTrue {
|
|
type Plugin = PolarsPlugin;
|
|
|
|
fn name(&self) -> &str {
|
|
"polars all-true"
|
|
}
|
|
|
|
fn description(&self) -> &str {
|
|
"Returns true if all values are true."
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build(self.name())
|
|
.input_output_type(
|
|
Type::Custom("dataframe".into()),
|
|
Type::Custom("dataframe".into()),
|
|
)
|
|
.category(Category::Custom("dataframe".into()))
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![
|
|
Example {
|
|
description: "Returns true if all values are true",
|
|
example: "[true true true] | polars into-df | polars all-true",
|
|
result: Some(
|
|
NuDataFrame::try_from_columns(
|
|
vec![Column::new(
|
|
"all_true".to_string(),
|
|
vec![Value::test_bool(true)],
|
|
)],
|
|
None,
|
|
)
|
|
.expect("simple df for test should not fail")
|
|
.into_value(Span::test_data()),
|
|
),
|
|
},
|
|
Example {
|
|
description: "Checks the result from a comparison",
|
|
example: r#"let s = ([5 6 2 8] | polars into-df);
|
|
let res = ($s > 9);
|
|
$res | polars all-true"#,
|
|
result: Some(
|
|
NuDataFrame::try_from_columns(
|
|
vec![Column::new(
|
|
"all_true".to_string(),
|
|
vec![Value::test_bool(false)],
|
|
)],
|
|
None,
|
|
)
|
|
.expect("simple df for test should not fail")
|
|
.into_value(Span::test_data()),
|
|
),
|
|
},
|
|
]
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
plugin: &Self::Plugin,
|
|
engine: &EngineInterface,
|
|
call: &EvaluatedCall,
|
|
input: PipelineData,
|
|
) -> Result<PipelineData, LabeledError> {
|
|
command(plugin, engine, call, input).map_err(LabeledError::from)
|
|
}
|
|
}
|
|
|
|
fn command(
|
|
plugin: &PolarsPlugin,
|
|
engine: &EngineInterface,
|
|
call: &EvaluatedCall,
|
|
input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
let df = NuDataFrame::try_from_pipeline_coerce(plugin, input, call.head)?;
|
|
|
|
let series = df.as_series(call.head)?;
|
|
let bool = series.bool().map_err(|_| ShellError::GenericError {
|
|
error: "Error converting to bool".into(),
|
|
msg: "all-false only works with series of type bool".into(),
|
|
span: Some(call.head),
|
|
help: None,
|
|
inner: vec![],
|
|
})?;
|
|
|
|
let value = Value::bool(bool.all(), call.head);
|
|
|
|
let df = NuDataFrame::try_from_columns(
|
|
vec![Column::new("all_true".to_string(), vec![value])],
|
|
None,
|
|
)?;
|
|
df.to_pipeline_data(plugin, engine, call.head)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::*;
|
|
use crate::test::test_polars_plugin_command;
|
|
|
|
#[test]
|
|
fn test_examples() -> Result<(), ShellError> {
|
|
test_polars_plugin_command(&AllTrue)
|
|
}
|
|
}
|