forked from extern/nushell
# Description Use `record!` macro instead of defining two separate `vec!` for `cols` and `vals` when appropriate. This visually aligns the key with the value. Further more you don't have to deal with the construction of `Record { cols, vals }` so we can hide the implementation details in the future. ## State Not covering all possible commands yet, also some tests/examples are better expressed by creating cols and vals separately. # User/Developer-Facing Changes The examples and tests should read more natural. No relevant functional change # Bycatch Where I noticed it I replaced usage of `Value` constructors with `Span::test_data()` or `Span::unknown()` to the `Value::test_...` constructors. This should make things more readable and also simplify changes to the `Span` system in the future.
93 lines
2.4 KiB
Rust
93 lines
2.4 KiB
Rust
use super::super::values::NuExpression;
|
|
|
|
use nu_engine::CallExt;
|
|
use nu_protocol::{
|
|
ast::Call,
|
|
engine::{Command, EngineState, Stack},
|
|
record, Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
|
|
};
|
|
|
|
#[derive(Clone)]
|
|
pub struct ExprAlias;
|
|
|
|
impl Command for ExprAlias {
|
|
fn name(&self) -> &str {
|
|
"dfr as"
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Creates an alias expression."
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build(self.name())
|
|
.required(
|
|
"Alias name",
|
|
SyntaxShape::String,
|
|
"Alias name for the expression",
|
|
)
|
|
.input_output_type(
|
|
Type::Custom("expression".into()),
|
|
Type::Custom("expression".into()),
|
|
)
|
|
.category(Category::Custom("expression".into()))
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![Example {
|
|
description: "Creates and alias expression",
|
|
example: "dfr col a | dfr as new_a | dfr into-nu",
|
|
result: {
|
|
let record = Value::test_record(record! {
|
|
"expr" => Value::test_record(record! {
|
|
"expr" => Value::test_string("column"),
|
|
"value" => Value::test_string("a"),
|
|
}),
|
|
"alias" => Value::test_string("new_a"),
|
|
});
|
|
|
|
Some(record)
|
|
},
|
|
}]
|
|
}
|
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
|
vec!["aka", "abbr", "otherwise"]
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
let alias: String = call.req(engine_state, stack, 0)?;
|
|
|
|
let expr = NuExpression::try_from_pipeline(input, call.head)?;
|
|
let expr: NuExpression = expr.into_polars().alias(alias.as_str()).into();
|
|
|
|
Ok(PipelineData::Value(
|
|
NuExpression::into_value(expr, call.head),
|
|
None,
|
|
))
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::super::super::test_dataframe::test_dataframe;
|
|
use super::*;
|
|
use crate::dataframe::eager::ToNu;
|
|
use crate::dataframe::expressions::ExprCol;
|
|
|
|
#[test]
|
|
fn test_examples() {
|
|
test_dataframe(vec![
|
|
Box::new(ExprAlias {}),
|
|
Box::new(ExprCol {}),
|
|
Box::new(ToNu {}),
|
|
])
|
|
}
|
|
}
|