forked from extern/nushell
Use NthArgs to better convert to an integer
Using i64 as u64 deserialize isn't implemented yet.
This commit is contained in:
parent
82a315f06a
commit
9951691023
@ -3,6 +3,11 @@ use crate::errors::ShellError;
|
|||||||
use crate::parser::CommandRegistry;
|
use crate::parser::CommandRegistry;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct NthArgs {
|
||||||
|
position: Tagged<i64>,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Nth;
|
pub struct Nth;
|
||||||
|
|
||||||
impl WholeStreamCommand for Nth {
|
impl WholeStreamCommand for Nth {
|
||||||
@ -11,7 +16,7 @@ impl WholeStreamCommand for Nth {
|
|||||||
args: CommandArgs,
|
args: CommandArgs,
|
||||||
registry: &CommandRegistry,
|
registry: &CommandRegistry,
|
||||||
) -> Result<OutputStream, ShellError> {
|
) -> Result<OutputStream, ShellError> {
|
||||||
nth(args, registry)
|
args.process(registry, nth)?.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn name(&self) -> &str {
|
fn name(&self) -> &str {
|
||||||
@ -19,27 +24,15 @@ impl WholeStreamCommand for Nth {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn signature(&self) -> Signature {
|
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<OutputStream, ShellError> {
|
fn nth(
|
||||||
let args = args.evaluate_once(registry)?;
|
NthArgs { position: amount }: NthArgs,
|
||||||
|
RunnableContext { input, .. }: RunnableContext,
|
||||||
let amount = args.expect_nth(0)?.as_i64();
|
) -> Result<OutputStream, ShellError> {
|
||||||
|
|
||||||
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(),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(OutputStream::from_input(
|
Ok(OutputStream::from_input(
|
||||||
args.input.values.skip(amount as u64).take(1),
|
input.values.skip(amount.item as u64).take(1),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user