2019-08-19 07:16:39 +02:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-05-15 18:12:38 +02:00
|
|
|
use crate::errors::ShellError;
|
2019-07-24 00:22:11 +02:00
|
|
|
use crate::parser::CommandRegistry;
|
2019-05-15 18:12:38 +02:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2019-08-19 07:16:39 +02:00
|
|
|
pub struct First;
|
|
|
|
|
|
|
|
impl WholeStreamCommand for First {
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
first(args, registry)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"first"
|
|
|
|
}
|
2019-07-24 00:22:11 +02:00
|
|
|
|
2019-08-19 07:16:39 +02:00
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("first").required("amount", SyntaxType::Literal)
|
2019-06-08 00:35:07 +02:00
|
|
|
}
|
2019-08-19 07:16:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn first(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
|
|
|
let args = args.evaluate_once(registry)?;
|
2019-06-08 00:35:07 +02:00
|
|
|
|
2019-06-22 05:43:37 +02:00
|
|
|
let amount = args.expect_nth(0)?.as_i64();
|
2019-06-08 00:35:07 +02:00
|
|
|
|
|
|
|
let amount = match amount {
|
|
|
|
Ok(o) => o,
|
|
|
|
Err(_) => {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"Value is not a number",
|
|
|
|
"expected integer",
|
2019-08-01 03:58:42 +02:00
|
|
|
args.expect_nth(0)?.span(),
|
2019-06-08 00:35:07 +02:00
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
2019-05-16 02:21:46 +02:00
|
|
|
|
2019-07-24 00:22:11 +02:00
|
|
|
Ok(OutputStream::from_input(
|
|
|
|
args.input.values.take(amount as u64),
|
|
|
|
))
|
2019-05-15 18:12:38 +02:00
|
|
|
}
|