From 9951691023c30748ebfb91d6c31eb074844db688 Mon Sep 17 00:00:00 2001 From: Odin Dutton Date: Tue, 20 Aug 2019 13:12:31 +1000 Subject: [PATCH] Use NthArgs to better convert to an integer Using i64 as u64 deserialize isn't implemented yet. --- src/commands/nth.rs | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/src/commands/nth.rs b/src/commands/nth.rs index 46e78eb2c..17eb6dba7 100644 --- a/src/commands/nth.rs +++ b/src/commands/nth.rs @@ -3,6 +3,11 @@ use crate::errors::ShellError; use crate::parser::CommandRegistry; use crate::prelude::*; +#[derive(Deserialize)] +struct NthArgs { + position: Tagged, +} + pub struct Nth; impl WholeStreamCommand for Nth { @@ -11,7 +16,7 @@ impl WholeStreamCommand for Nth { args: CommandArgs, registry: &CommandRegistry, ) -> Result { - nth(args, registry) + args.process(registry, nth)?.run() } fn name(&self) -> &str { @@ -19,27 +24,15 @@ impl WholeStreamCommand for Nth { } fn signature(&self) -> Signature { - Signature::build("nth").required("amount", SyntaxType::Literal) + Signature::build("nth").optional("amount", SyntaxType::Any) } } -fn nth(args: CommandArgs, registry: &CommandRegistry) -> Result { - let args = args.evaluate_once(registry)?; - - let amount = args.expect_nth(0)?.as_i64(); - - let amount = match amount { - Ok(o) => o, - Err(_) => { - return Err(ShellError::labeled_error( - "Value is not a number", - "expected integer", - args.expect_nth(0)?.span(), - )) - } - }; - +fn nth( + NthArgs { position: amount }: NthArgs, + RunnableContext { input, .. }: RunnableContext, +) -> Result { Ok(OutputStream::from_input( - args.input.values.skip(amount as u64).take(1), + input.values.skip(amount.item as u64).take(1), )) }