use nu_engine::command_prelude::*;
use nu_protocol::engine::CommandType;
#[derive(Clone)]
pub struct While;
impl Command for While {
fn name(&self) -> &str {
"while"
}
fn description(&self) -> &str {
"Conditionally run a block in a loop."
}
fn signature(&self) -> nu_protocol::Signature {
Signature::build("while")
.input_output_types(vec![(Type::Nothing, Type::Nothing)])
.allow_variants_without_examples(true)
.required("cond", SyntaxShape::MathExpression, "Condition to check.")
.required(
"block",
SyntaxShape::Block,
"Block to loop if check succeeds.",
)
.category(Category::Core)
}
fn search_terms(&self) -> Vec<&str> {
vec!["loop"]
}
fn extra_description(&self) -> &str {
"This command is a parser keyword. For details, check:
https://www.nushell.sh/book/thinking_in_nu.html"
}
fn command_type(&self) -> CommandType {
CommandType::Keyword
}
fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
_call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
eprintln!(
"Tried to execute 'run' for the 'while' command: this code path should never be reached in IR mode"
);
unreachable!()
}
fn examples(&self) -> Vec<Example<'_>> {
vec![Example {
description: "Loop while a condition is true.",
example: "mut x = 0; while $x < 10 { $x = $x + 1 }",
result: None,
}]
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() -> nu_test_support::Result {
nu_test_support::test().examples(While)
}
}