2019-05-16 04:42:44 +02:00
|
|
|
use crate::errors::ShellError;
|
2019-06-22 05:43:37 +02:00
|
|
|
use crate::parser::registry::{CommandConfig, PositionalType};
|
2019-05-16 04:42:44 +02:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2019-05-28 08:45:18 +02:00
|
|
|
pub struct Where;
|
|
|
|
|
|
|
|
impl Command for Where {
|
|
|
|
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
r#where(args)
|
|
|
|
}
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"where"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn config(&self) -> CommandConfig {
|
|
|
|
CommandConfig {
|
|
|
|
name: self.name().to_string(),
|
|
|
|
mandatory_positional: vec![PositionalType::Block("condition".to_string())],
|
|
|
|
optional_positional: vec![],
|
|
|
|
rest_positional: false,
|
|
|
|
named: indexmap::IndexMap::new(),
|
2019-07-02 09:56:20 +02:00
|
|
|
is_filter: true,
|
|
|
|
is_sink: false,
|
|
|
|
can_load: vec![],
|
|
|
|
can_save: vec![],
|
2019-05-28 08:45:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 09:23:06 +02:00
|
|
|
pub fn r#where(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(
|
|
|
|
"Where requires a condition",
|
|
|
|
"needs condition",
|
|
|
|
args.name_span,
|
|
|
|
));
|
2019-05-22 09:12:03 +02:00
|
|
|
}
|
2019-05-16 04:42:44 +02:00
|
|
|
|
2019-06-22 05:43:37 +02:00
|
|
|
let block = args.expect_nth(0)?.as_block()?;
|
2019-05-23 09:23:06 +02:00
|
|
|
let input = args.input;
|
2019-05-16 04:42:44 +02:00
|
|
|
|
2019-05-28 08:45:18 +02:00
|
|
|
let objects = input.filter_map(move |item| {
|
|
|
|
let result = block.invoke(&item);
|
|
|
|
|
|
|
|
let return_value = match result {
|
|
|
|
Err(err) => Some(ReturnValue::Value(Value::Error(Box::new(err)))),
|
|
|
|
Ok(v) if v.is_true() => Some(ReturnValue::Value(item.copy())),
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
futures::future::ready(return_value)
|
|
|
|
});
|
2019-05-16 04:42:44 +02:00
|
|
|
|
2019-05-26 08:54:41 +02:00
|
|
|
Ok(objects.boxed())
|
2019-05-16 04:42:44 +02:00
|
|
|
}
|