Add support for skip and where

This commit is contained in:
Jonathan Turner
2019-05-15 19:42:44 -07:00
parent 638093aad6
commit cbb86b0cab
6 changed files with 174 additions and 2 deletions

29
src/commands/skip.rs Normal file
View File

@@ -0,0 +1,29 @@
use crate::errors::ShellError;
use crate::prelude::*;
use derive_new::new;
#[derive(new)]
pub struct Skip;
// TODO: "Amount remaining" wrapper
impl crate::Command for Skip {
fn run(&self, args: CommandArgs<'caller>) -> Result<VecDeque<ReturnValue>, ShellError> {
let amount = args.args[0].as_int()?;
let amount = if args.input.len() > amount as usize {
amount as usize
} else {
args.input.len()
};
let out: VecDeque<ReturnValue> = args
.input
.into_iter()
.skip(amount)
.map(|v| ReturnValue::Value(v))
.collect();
Ok(out)
}
}

30
src/commands/where_.rs Normal file
View File

@@ -0,0 +1,30 @@
use crate::errors::ShellError;
use crate::object::base::find;
use crate::prelude::*;
use derive_new::new;
#[derive(new)]
pub struct Where;
impl crate::Command for Where {
fn run(&self, args: CommandArgs<'caller>) -> Result<VecDeque<ReturnValue>, ShellError> {
if args.args.is_empty() {
return Err(ShellError::string("select requires a field"));
}
let field: Result<String, _> = args.args[0].as_string();
let field = field?;
let op: Result<String, _> = args.args[1].as_string();
let op = op?;
let objects = args
.input
.iter()
.filter(|item| find(&item, &field, &op, &args.args[2]))
.map(|item| ReturnValue::Value(item.copy()))
.collect();
Ok(objects)
}
}