nushell/src/commands/where_.rs

24 lines
626 B
Rust
Raw Normal View History

2019-05-16 04:42:44 +02:00
use crate::errors::ShellError;
2019-07-06 00:08:58 +02:00
use crate::object::types::*;
2019-05-16 04:42:44 +02:00
use crate::prelude::*;
use futures::future::ready;
use log::trace;
2019-05-16 04:42:44 +02:00
command! {
Where as where(args, condition: Block) {
let input: InputStream = trace_stream!("where input" = args.input);
2019-05-28 08:45:18 +02:00
input.values.filter_map(move |item| {
let result = condition.invoke(&item);
2019-05-28 08:45:18 +02:00
let return_value = match result {
Err(err) => Some(Err(err)),
Ok(v) if v.is_true() => Some(Ok(ReturnSuccess::Value(item.copy()))),
_ => None,
};
2019-05-28 08:45:18 +02:00
ready(return_value)
})
2019-05-22 09:12:03 +02:00
}
2019-05-16 04:42:44 +02:00
}