nushell/src/commands/pick.rs

30 lines
882 B
Rust
Raw Normal View History

use crate::errors::ShellError;
2019-05-22 07:12:03 +00:00
use crate::object::base::select_fields;
2019-05-15 22:58:44 +00:00
use crate::object::Value;
use crate::prelude::*;
2019-06-02 18:53:30 +00:00
pub fn pick(args: CommandArgs) -> Result<OutputStream, ShellError> {
2019-06-07 22:35:07 +00:00
if args.positional.len() == 0 {
if let Some(span) = args.name_span {
return Err(ShellError::labeled_error(
"Pick requires fields",
"needs parameter",
span,
));
} else {
return Err(ShellError::string("pick requires fields."));
}
2019-05-22 07:12:03 +00:00
}
let fields: Result<Vec<String>, _> = args.positional.iter().map(|a| a.as_string()).collect();
2019-05-22 07:12:03 +00:00
let fields = fields?;
2019-05-22 07:12:03 +00: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)
}