nushell/crates/nu-cli/src/commands/rename.rs

129 lines
3.8 KiB
Rust
Raw Normal View History

2020-03-03 22:01:24 +01:00
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
2020-10-02 03:13:42 +02:00
use indexmap::indexmap;
2020-03-03 22:01:24 +01:00
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
use nu_source::Tagged;
pub struct Rename;
#[derive(Deserialize)]
pub struct Arguments {
column_name: Tagged<String>,
rest: Vec<Tagged<String>>,
}
2020-05-29 10:22:52 +02:00
#[async_trait]
2020-03-03 22:01:24 +01:00
impl WholeStreamCommand for Rename {
fn name(&self) -> &str {
"rename"
}
fn signature(&self) -> Signature {
Signature::build("rename")
.required(
"column_name",
SyntaxShape::String,
"the new name for the first column",
2020-03-03 22:01:24 +01:00
)
.rest(SyntaxShape::String, "the new name for additional columns")
2020-03-03 22:01:24 +01:00
}
fn usage(&self) -> &str {
"Creates a new table with columns renamed."
}
2020-05-29 10:22:52 +02:00
async fn run(
2020-03-03 22:01:24 +01:00
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
rename(args, registry).await
2020-03-03 22:01:24 +01:00
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Rename a column",
2020-10-02 03:13:42 +02:00
example: "echo [[a, b]; [1, 2]] | rename my_column",
result: Some(vec![UntaggedValue::row(indexmap! {
"my_column".to_string() => UntaggedValue::int(1).into(),
"b".to_string() => UntaggedValue::int(2).into(),
})
.into()]),
},
Example {
description: "Rename many columns",
2020-10-02 03:13:42 +02:00
example: "echo [[a, b, c]; [1, 2, 3]] | rename eggs ham bacon",
result: Some(vec![UntaggedValue::row(indexmap! {
"eggs".to_string() => UntaggedValue::int(1).into(),
"ham".to_string() => UntaggedValue::int(2).into(),
"bacon".to_string() => UntaggedValue::int(3).into(),
})
.into()]),
},
]
}
2020-03-03 22:01:24 +01:00
}
pub async fn rename(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let name = args.call_info.name_tag.clone();
let (Arguments { column_name, rest }, input) = args.process(&registry).await?;
let mut new_column_names = vec![vec![column_name]];
new_column_names.push(rest);
2020-03-03 22:01:24 +01:00
let new_column_names = new_column_names.into_iter().flatten().collect::<Vec<_>>();
2020-03-03 22:01:24 +01:00
Ok(input
.map(move |item| {
2020-03-03 22:01:24 +01:00
if let Value {
value: UntaggedValue::Row(row),
tag,
} = item
{
let mut renamed_row = IndexMap::new();
for (idx, (key, value)) in row.entries.iter().enumerate() {
let key = if idx < new_column_names.len() {
&new_column_names[idx].item
} else {
key
};
renamed_row.insert(key.clone(), value.clone());
}
let out = UntaggedValue::Row(renamed_row.into()).into_value(tag);
ReturnSuccess::value(out)
2020-03-03 22:01:24 +01:00
} else {
ReturnSuccess::value(
2020-03-03 22:01:24 +01:00
UntaggedValue::Error(ShellError::labeled_error(
"no column names available",
"can't rename",
&name,
))
.into_untagged_value(),
)
2020-03-03 22:01:24 +01:00
}
})
.to_output_stream())
2020-03-03 22:01:24 +01:00
}
#[cfg(test)]
mod tests {
use super::Rename;
use super::ShellError;
#[test]
fn examples_work_as_expected() -> Result<(), ShellError> {
use crate::examples::test as test_examples;
Ok(test_examples(Rename {})?)
}
}