nushell/src/commands/pick.rs

35 lines
878 B
Rust
Raw Normal View History

2019-07-24 00:22:11 +02:00
use crate::context::CommandRegistry;
use crate::errors::ShellError;
2019-05-22 09:12:03 +02:00
use crate::object::base::select_fields;
use crate::prelude::*;
2019-07-24 00:22:11 +02:00
pub fn pick(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once(registry)?;
let len = args.len();
let span = args.name_span();
let (input, args) = args.parts();
if len == 0 {
return Err(ShellError::labeled_error(
2019-06-15 20:36:17 +02:00
"Pick requires fields",
"needs parameter",
2019-07-24 00:22:11 +02:00
span,
2019-06-15 20:36:17 +02:00
));
2019-05-22 09:12:03 +02:00
}
2019-07-24 00:22:11 +02:00
let fields: Result<Vec<String>, _> = args
.positional
.iter()
.flatten()
.map(|a| a.as_string())
.collect();
2019-05-22 09:12:03 +02:00
let fields = fields?;
2019-07-08 18:44:53 +02:00
let objects = input
.values
.map(move |value| select_fields(&value.item, &fields, value.tag()));
Ok(objects.from_input_stream())
}