nushell/crates/nu-command/src/commands/wrap.rs
Michael Angerman d06f457b2a
nu-cli refactor moving commands into their own crate nu-command (#2910)
* move commands, futures.rs, script.rs, utils

* move over maybe_print_errors

* add nu_command crate references to nu_cli

* in commands.rs open up to pub mod from pub(crate)

* nu-cli, nu-command, and nu tests are now passing

* cargo fmt

* clean up nu-cli/src/prelude.rs

* code cleanup

* for some reason lex.rs was not formatted, may be causing my error

* remove mod completion from lib.rs which was not being used along with quickcheck macros

* add in allow unused imports

* comment out one failing external test; comment out one failing internal test

* revert commenting out failing tests; something else might be going on; someone with a windows machine should check and see what is going on with these failing windows tests

* Update Cargo.toml

Extend the optional features to nu-command

Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
2021-01-12 17:59:53 +13:00

143 lines
4.2 KiB
Rust

use crate::prelude::*;
use indexmap::{indexmap, IndexMap};
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
use nu_source::Tagged;
const DEFAULT_COLUMN_NAME: &str = "Column";
pub struct Wrap;
#[derive(Deserialize)]
struct WrapArgs {
column: Option<Tagged<String>>,
}
#[async_trait]
impl WholeStreamCommand for Wrap {
fn name(&self) -> &str {
"wrap"
}
fn signature(&self) -> Signature {
Signature::build("wrap").optional(
"column",
SyntaxShape::String,
"the name of the new column",
)
}
fn usage(&self) -> &str {
"Wraps the given data in a table."
}
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
wrap(args).await
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Wrap a list into a table with the default column name",
example: "echo [1 2 3] | wrap",
result: Some(vec![
UntaggedValue::row(indexmap! {
DEFAULT_COLUMN_NAME.to_string() => UntaggedValue::int(1).into(),
})
.into(),
UntaggedValue::row(indexmap! {
DEFAULT_COLUMN_NAME.to_string() => UntaggedValue::int(2).into(),
})
.into(),
UntaggedValue::row(indexmap! {
DEFAULT_COLUMN_NAME.to_string() => UntaggedValue::int(3).into(),
})
.into(),
]),
},
Example {
description: "Wrap a list into a table with a given column name",
example: "echo [1 2 3] | wrap MyColumn",
result: Some(vec![
UntaggedValue::row(indexmap! {
"MyColumn".to_string() => UntaggedValue::int(1).into(),
})
.into(),
UntaggedValue::row(indexmap! {
"MyColumn".to_string() => UntaggedValue::int(2).into(),
})
.into(),
UntaggedValue::row(indexmap! {
"MyColumn".to_string() => UntaggedValue::int(3).into(),
})
.into(),
]),
},
]
}
}
async fn wrap(args: CommandArgs) -> Result<OutputStream, ShellError> {
let (WrapArgs { column }, mut input) = args.process().await?;
let mut result_table = vec![];
let mut are_all_rows = true;
while let Some(value) = input.next().await {
match value {
Value {
value: UntaggedValue::Row(_),
..
} => {
result_table.push(value);
}
_ => {
are_all_rows = false;
let mut index_map = IndexMap::new();
index_map.insert(
match &column {
Some(key) => key.item.clone(),
None => DEFAULT_COLUMN_NAME.to_string(),
},
value,
);
result_table.push(UntaggedValue::row(index_map).into_value(Tag::unknown()));
}
}
}
if are_all_rows {
let mut index_map = IndexMap::new();
index_map.insert(
match &column {
Some(key) => key.item.clone(),
None => DEFAULT_COLUMN_NAME.to_string(),
},
UntaggedValue::table(&result_table).into_value(Tag::unknown()),
);
let row = UntaggedValue::row(index_map).into_untagged_value();
Ok(OutputStream::one(ReturnSuccess::value(row)))
} else {
Ok(
futures::stream::iter(result_table.into_iter().map(ReturnSuccess::value))
.to_output_stream(),
)
}
}
#[cfg(test)]
mod tests {
use super::ShellError;
use super::Wrap;
#[test]
fn examples_work_as_expected() -> Result<(), ShellError> {
use crate::examples::test as test_examples;
Ok(test_examples(Wrap {})?)
}
}