nushell/src/commands/where_.rs
Andrés N. Robalino ca0c6eaf58 This commit introduces a basic help feature. We can go to it
with the `help` command to explore and list all commands available.

Enter will also try to see if the location to be entered is an existing
Nu command, if it is it will let you inspect the command under `help`.

This provides baseline needed so we can iterate on it.
2019-08-31 19:06:11 -05:00

63 lines
1.7 KiB
Rust

use crate::commands::PerItemCommand;
use crate::errors::ShellError;
use crate::parser::hir::SyntaxType;
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", SyntaxType::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.span,
))
}
};
Ok(stream.to_output_stream())
}
}