nushell/crates/nu-cli/src/commands/where_.rs
Jason Gedge b2c5af457e
Move most of the root package into a subcrate. (#1445)
This improves incremental build time when working on what was previously
the root package. For example, previously all plugins would be rebuilt
with a change to `src/commands/classified/external.rs`, but now only
`nu-cli` will have to be rebuilt (and anything that depends on it).
2020-03-04 13:58:20 -05:00

63 lines
1.7 KiB
Rust

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