2019-05-15 18:12:38 +02:00
|
|
|
use crate::errors::ShellError;
|
2019-05-16 00:58:44 +02:00
|
|
|
use crate::object::Value;
|
2019-05-15 18:12:38 +02:00
|
|
|
use crate::prelude::*;
|
|
|
|
use derive_new::new;
|
|
|
|
|
|
|
|
#[derive(new)]
|
|
|
|
pub struct TakeBlueprint;
|
|
|
|
|
|
|
|
impl crate::CommandBlueprint for TakeBlueprint {
|
|
|
|
fn create(
|
|
|
|
&self,
|
|
|
|
args: Vec<Value>,
|
2019-05-16 00:58:44 +02:00
|
|
|
_host: &dyn Host,
|
|
|
|
_env: &mut Environment,
|
2019-05-15 18:12:38 +02:00
|
|
|
) -> Result<Box<dyn Command>, ShellError> {
|
|
|
|
if args.is_empty() {
|
|
|
|
return Err(ShellError::string("take requires an integer"));
|
|
|
|
}
|
|
|
|
|
|
|
|
let amount = args[0].as_int()?;
|
|
|
|
|
|
|
|
Ok(Box::new(Take { amount }))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(new)]
|
|
|
|
pub struct Take {
|
|
|
|
amount: i64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl crate::Command for Take {
|
|
|
|
fn run(&mut self, stream: VecDeque<Value>) -> Result<VecDeque<ReturnValue>, ShellError> {
|
|
|
|
let amount = if stream.len() > self.amount as usize {
|
|
|
|
self.amount as usize
|
|
|
|
} else {
|
|
|
|
stream.len()
|
|
|
|
};
|
|
|
|
|
|
|
|
let out: VecDeque<ReturnValue> = stream
|
|
|
|
.into_iter()
|
|
|
|
.take(amount)
|
|
|
|
.map(|v| ReturnValue::Value(v))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
Ok(out)
|
|
|
|
}
|
|
|
|
}
|