2019-08-02 21:15:07 +02:00
|
|
|
use crate::commands::StaticCommand;
|
2019-05-16 04:42:44 +02:00
|
|
|
use crate::errors::ShellError;
|
2019-07-24 00:22:11 +02:00
|
|
|
use crate::object::base as value;
|
|
|
|
use crate::parser::hir::SyntaxType;
|
2019-08-02 21:15:07 +02:00
|
|
|
use crate::parser::registry;
|
2019-05-16 04:42:44 +02:00
|
|
|
use crate::prelude::*;
|
2019-07-24 00:22:11 +02:00
|
|
|
|
2019-07-03 22:31:15 +02:00
|
|
|
use futures::future::ready;
|
2019-08-02 21:15:07 +02:00
|
|
|
use serde::Deserialize;
|
2019-05-16 04:42:44 +02:00
|
|
|
|
2019-07-24 00:22:11 +02:00
|
|
|
pub struct Where;
|
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct WhereArgs {
|
|
|
|
condition: value::Block,
|
|
|
|
}
|
2019-05-28 08:45:18 +02:00
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
impl StaticCommand for Where {
|
2019-07-24 00:22:11 +02:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
"where"
|
|
|
|
}
|
2019-05-28 08:45:18 +02:00
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
fn signature(&self) -> registry::Signature {
|
|
|
|
Signature::build("where")
|
|
|
|
.required("condition", SyntaxType::Block)
|
|
|
|
.sink()
|
2019-05-22 09:12:03 +02:00
|
|
|
}
|
2019-07-24 00:22:11 +02:00
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: ®istry::CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
args.process(registry, run)?.run()
|
|
|
|
}
|
|
|
|
}
|
2019-07-24 00:22:11 +02:00
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
fn run(
|
|
|
|
WhereArgs { condition }: WhereArgs,
|
|
|
|
context: RunnableContext,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
Ok(context
|
|
|
|
.input
|
|
|
|
.values
|
|
|
|
.filter_map(move |item| {
|
|
|
|
let result = condition.invoke(&item);
|
|
|
|
|
|
|
|
let return_value = match result {
|
|
|
|
Err(err) => Some(Err(err)),
|
|
|
|
Ok(v) if v.is_true() => Some(Ok(ReturnSuccess::Value(item.clone()))),
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
ready(return_value)
|
|
|
|
})
|
|
|
|
.boxed()
|
|
|
|
.to_output_stream())
|
|
|
|
}
|