nushell/src/commands/where_.rs
Jonathan Turner 193b00764b
Stream support (#812)
* Moves off of draining between filters. Instead, the sink will pull on the stream, and will drain element-wise. This moves the whole stream to being lazy.
* Adds ctrl-c support and connects it into some of the key points where we pull on the stream. If a ctrl-c is detect, we immediately halt pulling on the stream and return to the prompt.
* Moves away from having a SourceMap where anchor locations are stored. Now AnchorLocation is kept directly in the Tag.
* To make this possible, split tag and span. Span is largely used in the parser and is copyable. Tag is now no longer copyable.
2019-10-13 17:12:43 +13:00

60 lines
1.6 KiB
Rust

use crate::commands::PerItemCommand;
use crate::errors::ShellError;
use crate::parser::hir::SyntaxShape;
use crate::parser::registry;
use crate::prelude::*;
pub struct Where;
impl PerItemCommand for Where {
fn name(&self) -> &str {
"where"
}
fn signature(&self) -> registry::Signature {
Signature::build("where").required("condition", SyntaxShape::Block)
}
fn usage(&self) -> &str {
"Filter table to match the condition."
}
fn run(
&self,
call_info: &CallInfo,
_registry: &registry::CommandRegistry,
_raw_args: &RawCommandArgs,
input: Tagged<Value>,
) -> Result<OutputStream, ShellError> {
let input_clone = input.clone();
let condition = call_info.args.expect_nth(0)?;
let stream = match condition {
Tagged {
item: Value::Block(block),
..
} => {
let result = block.invoke(&input_clone);
match result {
Ok(v) => {
if v.is_true() {
VecDeque::from(vec![Ok(ReturnSuccess::Value(input_clone))])
} else {
VecDeque::new()
}
}
Err(e) => return Err(e),
}
}
Tagged { tag, .. } => {
return Err(ShellError::labeled_error(
"Expected a condition",
"where needs a condition",
tag,
))
}
};
Ok(stream.to_output_stream())
}
}