2019-05-16 02:42:44 +00:00
|
|
|
use crate::errors::ShellError;
|
|
|
|
use crate::object::base::find;
|
|
|
|
use crate::prelude::*;
|
|
|
|
|
2019-05-23 07:23:06 +00:00
|
|
|
pub fn r#where(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
2019-05-22 07:12:03 +00:00
|
|
|
if args.args.is_empty() {
|
|
|
|
return Err(ShellError::string("select requires a field"));
|
|
|
|
}
|
2019-05-16 02:42:44 +00:00
|
|
|
|
2019-05-22 07:12:03 +00:00
|
|
|
let field: Result<String, _> = args.args[0].as_string();
|
|
|
|
let field = field?;
|
2019-05-23 07:23:06 +00:00
|
|
|
let input = args.input;
|
|
|
|
let operator = args.args[1].copy();
|
2019-05-16 02:42:44 +00:00
|
|
|
|
2019-05-23 07:23:06 +00:00
|
|
|
match operator {
|
|
|
|
Value::Primitive(Primitive::Operator(operator)) => {
|
|
|
|
let right = args.args[2].copy();
|
2019-05-16 02:42:44 +00:00
|
|
|
|
2019-05-23 07:23:06 +00:00
|
|
|
let objects = input
|
|
|
|
.filter(move |item| futures::future::ready(find(&item, &field, &operator, &right)))
|
|
|
|
.map(|item| ReturnValue::Value(item.copy()));
|
|
|
|
|
|
|
|
Ok(objects.boxed())
|
2019-05-22 07:12:03 +00:00
|
|
|
}
|
2019-05-23 07:23:06 +00:00
|
|
|
x => {
|
2019-05-22 07:12:03 +00:00
|
|
|
println!("{:?}", x);
|
|
|
|
Err(ShellError::string("expected a comparison operator"))
|
2019-05-18 01:27:31 +00:00
|
|
|
}
|
2019-05-16 02:42:44 +00:00
|
|
|
}
|
|
|
|
}
|