nushell/src/commands/where_.rs

76 lines
2.2 KiB
Rust
Raw Normal View History

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;
use crate::parser::registry::{self, CommandConfig, PositionalType};
2019-05-16 04:42:44 +02:00
use crate::prelude::*;
2019-07-24 00:22:11 +02:00
use futures::future::ready;
2019-07-24 00:22:11 +02:00
use indexmap::IndexMap;
use log::trace;
2019-05-16 04:42:44 +02:00
2019-07-24 00:22:11 +02:00
pub struct Where;
impl Command for Where {
fn run(
&self,
args: CommandArgs,
registry: &registry::CommandRegistry,
) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once(registry)?;
let condition = value::Block::extract(args.expect_nth(0)?)?;
let input = args.input;
let input: InputStream =
trace_stream!(target: "nu::trace_stream::where", "where input" = input);
Ok(input
.values
.filter_map(move |item| {
let result = condition.invoke(&item);
2019-05-28 08:45:18 +02:00
2019-07-24 00:22:11 +02:00
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())
}
2019-05-28 08:45:18 +02:00
2019-07-24 00:22:11 +02:00
fn name(&self) -> &str {
"where"
}
2019-05-28 08:45:18 +02:00
2019-07-24 00:22:11 +02:00
fn config(&self) -> CommandConfig {
CommandConfig {
name: self.name().to_string(),
positional: vec![PositionalType::mandatory("condition", SyntaxType::Block)],
rest_positional: false,
named: IndexMap::default(),
is_sink: true,
is_filter: false,
}
2019-05-22 09:12:03 +02:00
}
2019-05-16 04:42:44 +02:00
}
2019-07-24 00:22:11 +02:00
// command! {
// Where as where(args, condition: Block,) {
// let input = args.input;
// let input: InputStream = trace_stream!(target: "nu::trace_stream::where", "where input" = input);
// 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)
// })
// }
// }