forked from extern/nushell
All of the dataframe commands ported over with no issues... ### 11 tests are commented out (for now) So 100 of the original 111 tests are passing with only 11 tests being ignored for now.. As per our conversation in the core team meeting on Wednesday I took @jntrnr suggestion and just commented out the tests dealing with [IntoDatetime](https://github.com/nushell/nushell/blob/main/crates/nu-command/src/conversions/into/mod.rs) Later on we can move this functionality out of nu-command if we decide it makes sense... ### The following tests were ignored... ```rust modified: crates/nu-cmd-dataframe/src/dataframe/series/date/get_day.rs modified: crates/nu-cmd-dataframe/src/dataframe/series/date/get_hour.rs modified: crates/nu-cmd-dataframe/src/dataframe/series/date/get_minute.rs modified: crates/nu-cmd-dataframe/src/dataframe/series/date/get_month.rs modified: crates/nu-cmd-dataframe/src/dataframe/series/date/get_nanosecond.rs modified: crates/nu-cmd-dataframe/src/dataframe/series/date/get_ordinal.rs modified: crates/nu-cmd-dataframe/src/dataframe/series/date/get_second.rs modified: crates/nu-cmd-dataframe/src/dataframe/series/date/get_week.rs modified: crates/nu-cmd-dataframe/src/dataframe/series/date/get_weekday.rs modified: crates/nu-cmd-dataframe/src/dataframe/series/date/get_year.rs modified: crates/nu-cmd-dataframe/src/dataframe/series/string/strftime.rs ```
101 lines
2.7 KiB
Rust
101 lines
2.7 KiB
Rust
use super::super::values::NuExpression;
|
|
|
|
use nu_engine::CallExt;
|
|
use nu_protocol::{
|
|
ast::Call,
|
|
engine::{Command, EngineState, Stack},
|
|
Category, Example, PipelineData, ShellError, Signature, Span, 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_type(Type::Custom("expression".into()))
|
|
.output_type(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 cols = vec!["expr".into(), "value".into()];
|
|
let expr = Value::test_string("column");
|
|
let value = Value::test_string("a");
|
|
let expr = Value::Record {
|
|
cols,
|
|
vals: vec![expr, value],
|
|
span: Span::test_data(),
|
|
};
|
|
|
|
let cols = vec!["expr".into(), "alias".into()];
|
|
let value = Value::test_string("new_a");
|
|
|
|
let record = Value::Record {
|
|
cols,
|
|
vals: vec![expr, value],
|
|
span: Span::test_data(),
|
|
};
|
|
|
|
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::expressions::ExprAsNu;
|
|
use crate::dataframe::expressions::ExprCol;
|
|
|
|
#[test]
|
|
fn test_examples() {
|
|
test_dataframe(vec![
|
|
Box::new(ExprAlias {}),
|
|
Box::new(ExprCol {}),
|
|
Box::new(ExprAsNu {}),
|
|
])
|
|
}
|
|
}
|