2020-06-27 07:38:19 +02:00
|
|
|
use crate::prelude::*;
|
2021-01-10 03:50:49 +01:00
|
|
|
use nu_engine::WholeStreamCommand;
|
2020-06-27 07:38:19 +02:00
|
|
|
use nu_errors::ShellError;
|
2020-08-02 09:29:29 +02:00
|
|
|
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
|
|
|
|
use nu_source::Tagged;
|
2020-06-27 07:38:19 +02:00
|
|
|
|
|
|
|
pub struct SubCommand;
|
|
|
|
|
|
|
|
impl WholeStreamCommand for SubCommand {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"str collect"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2020-08-02 09:29:29 +02:00
|
|
|
Signature::build("str collect").desc(self.usage()).optional(
|
|
|
|
"separator",
|
|
|
|
SyntaxShape::String,
|
|
|
|
"the separator to put between the different values",
|
|
|
|
)
|
2020-06-27 07:38:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"collects a list of strings into a string"
|
|
|
|
}
|
|
|
|
|
2021-04-06 18:19:43 +02:00
|
|
|
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
collect(args)
|
2020-06-27 07:38:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![Example {
|
|
|
|
description: "Collect a list of string",
|
|
|
|
example: "echo ['a' 'b' 'c'] | str collect",
|
|
|
|
result: Some(vec![Value::from("abc")]),
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 18:19:43 +02:00
|
|
|
pub fn collect(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
2020-08-02 09:29:29 +02:00
|
|
|
let tag = args.call_info.name_tag.clone();
|
2021-04-08 10:15:36 +02:00
|
|
|
//let (SubCommandArgs { separator }, input) = args.process()?;
|
|
|
|
let args = args.evaluate_once()?;
|
|
|
|
let separator: Option<Result<Tagged<String>, ShellError>> = args.opt(0);
|
|
|
|
let input = args.input;
|
|
|
|
let separator = if let Some(separator) = separator {
|
|
|
|
separator?.item
|
|
|
|
} else {
|
|
|
|
"".into()
|
|
|
|
};
|
2020-08-02 09:29:29 +02:00
|
|
|
|
2021-04-06 18:19:43 +02:00
|
|
|
let strings: Vec<Result<String, ShellError>> = input.map(|value| value.as_string()).collect();
|
2021-01-03 02:22:44 +01:00
|
|
|
let strings: Result<Vec<_>, _> = strings.into_iter().collect::<Result<_, _>>();
|
2020-08-02 09:29:29 +02:00
|
|
|
|
2021-01-03 02:22:44 +01:00
|
|
|
match strings {
|
|
|
|
Ok(strings) => {
|
|
|
|
let output = strings.join(&separator);
|
|
|
|
|
|
|
|
Ok(OutputStream::one(ReturnSuccess::value(
|
|
|
|
UntaggedValue::string(output).into_value(tag),
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
Err(err) => match err.error {
|
|
|
|
nu_errors::ProximateShellError::TypeError { actual, .. } => {
|
|
|
|
if let Some(item) = actual.item {
|
|
|
|
Err(ShellError::labeled_error_with_secondary(
|
|
|
|
"could not convert to string",
|
|
|
|
format!("tried to convert '{}' in input to a string", item),
|
|
|
|
tag.span,
|
|
|
|
format!("'{}' value originated here", item),
|
|
|
|
actual.span,
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
Err(ShellError::labeled_error_with_secondary(
|
|
|
|
"could not convert to string",
|
|
|
|
"failed to convert input to strings",
|
|
|
|
tag.span,
|
|
|
|
"non-string found here",
|
|
|
|
actual.span,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => Err(err),
|
|
|
|
},
|
|
|
|
}
|
2020-08-02 09:29:29 +02:00
|
|
|
}
|
|
|
|
|
2020-06-27 07:38:19 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-10-03 16:06:02 +02:00
|
|
|
use super::ShellError;
|
2020-06-27 07:38:19 +02:00
|
|
|
use super::SubCommand;
|
|
|
|
|
|
|
|
#[test]
|
2020-10-03 16:06:02 +02:00
|
|
|
fn examples_work_as_expected() -> Result<(), ShellError> {
|
2020-06-27 07:38:19 +02:00
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
2021-02-12 11:13:14 +01:00
|
|
|
test_examples(SubCommand {})
|
2020-06-27 07:38:19 +02:00
|
|
|
}
|
|
|
|
}
|