nushell/src/commands/where_.rs

24 lines
660 B
Rust
Raw Normal View History

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