nushell/src/commands/skip_while.rs

52 lines
1.3 KiB
Rust
Raw Normal View History

2019-06-18 02:39:57 +02:00
use crate::errors::ShellError;
2019-06-22 05:43:37 +02:00
use crate::parser::registry::CommandConfig;
2019-06-18 02:39:57 +02:00
use crate::parser::registry::PositionalType;
use crate::prelude::*;
pub struct SkipWhile;
impl Command for SkipWhile {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
skip_while(args)
}
fn name(&self) -> &str {
"skip-while"
}
fn config(&self) -> CommandConfig {
CommandConfig {
name: self.name().to_string(),
mandatory_positional: vec![PositionalType::Block("condition".to_string())],
optional_positional: vec![],
rest_positional: false,
named: indexmap::IndexMap::new(),
}
}
}
pub fn skip_while(args: CommandArgs) -> Result<OutputStream, ShellError> {
2019-06-22 05:43:37 +02:00
if args.len() == 0 {
2019-06-18 02:39:57 +02:00
return Err(ShellError::maybe_labeled_error(
"Where requires a condition",
"needs condition",
args.name_span,
));
}
2019-06-22 05:43:37 +02:00
let block = args.nth(0).unwrap().as_block()?;
2019-06-18 02:39:57 +02:00
let input = args.input;
let objects = input.skip_while(move |item| {
let result = block.invoke(&item);
let return_value = match result {
Ok(v) if v.is_true() => true,
_ => false,
};
futures::future::ready(return_value)
});
Ok(objects.map(|x| ReturnValue::Value(x)).boxed())
2019-06-22 05:43:37 +02:00
}