nushell/src/commands/pick.rs

26 lines
726 B
Rust
Raw Normal View History

use crate::errors::ShellError;
2019-05-22 09:12:03 +02:00
use crate::object::base::select_fields;
2019-05-16 00:58:44 +02:00
use crate::object::Value;
use crate::prelude::*;
2019-06-02 20:53:30 +02:00
pub fn pick(args: CommandArgs) -> Result<OutputStream, ShellError> {
2019-06-22 05:43:37 +02:00
if args.len() == 0 {
2019-06-15 20:36:17 +02:00
return Err(ShellError::maybe_labeled_error(
"Pick requires fields",
"needs parameter",
args.name_span,
));
2019-05-22 09:12:03 +02:00
}
2019-06-22 05:43:37 +02:00
let fields: Result<Vec<String>, _> = args.positional_iter().map(|a| a.as_string()).collect();
2019-05-22 09:12:03 +02:00
let fields = fields?;
2019-05-22 09:12:03 +02:00
let objects = args
.input
.map(move |item| Value::Object(select_fields(&item, &fields)))
.map(|item| ReturnValue::Value(item));
let stream = Pin::new(Box::new(objects));
Ok(stream)
}