2019-05-15 18:14:51 +00:00
|
|
|
use crate::errors::ShellError;
|
|
|
|
use crate::object::base::select;
|
2019-05-15 22:58:44 +00:00
|
|
|
use crate::object::Value;
|
2019-05-15 18:14:51 +00:00
|
|
|
use crate::prelude::*;
|
|
|
|
use derive_new::new;
|
|
|
|
|
|
|
|
#[derive(new)]
|
2019-05-16 00:21:46 +00:00
|
|
|
pub struct Select;
|
2019-05-15 18:14:51 +00:00
|
|
|
|
2019-05-16 00:21:46 +00:00
|
|
|
impl crate::Command for Select {
|
|
|
|
fn run(&self, args: CommandArgs<'caller>) -> Result<VecDeque<ReturnValue>, ShellError> {
|
|
|
|
if args.args.is_empty() {
|
|
|
|
return Err(ShellError::string("select requires a field"));
|
2019-05-15 18:14:51 +00:00
|
|
|
}
|
|
|
|
|
2019-05-16 00:21:46 +00:00
|
|
|
let fields: Result<Vec<String>, _> = args.args.iter().map(|a| a.as_string()).collect();
|
|
|
|
let fields = fields?;
|
2019-05-15 18:14:51 +00:00
|
|
|
|
2019-05-16 00:21:46 +00:00
|
|
|
let objects = args
|
|
|
|
.input
|
2019-05-15 18:14:51 +00:00
|
|
|
.iter()
|
2019-05-16 00:21:46 +00:00
|
|
|
.map(|item| Value::Object(select(item, &fields)))
|
2019-05-15 18:14:51 +00:00
|
|
|
.map(|item| ReturnValue::Value(item))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
Ok(objects)
|
|
|
|
}
|
|
|
|
}
|