Port range to engine-p (#3377)

* Removes arg serialization
* action stream -> output stream
* uses nu_protocol::Range instead of NumericRange
* random missing newline I found in the code
This commit is contained in:
Jakub Žádník 2021-05-02 22:47:59 +03:00 committed by GitHub
parent cc4616f25b
commit 4fc05cac56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 22 deletions

View File

@ -1,13 +1,10 @@
use crate::prelude::*; use crate::prelude::*;
use nu_engine::deserializer::NumericRange;
use nu_engine::WholeStreamCommand; use nu_engine::WholeStreamCommand;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{RangeInclusion, ReturnSuccess, Signature, SyntaxShape}; use nu_protocol::{RangeInclusion, Signature, SyntaxShape, Value};
use nu_source::Tagged;
#[derive(Deserialize)]
struct RangeArgs { struct RangeArgs {
area: Tagged<NumericRange>, range: nu_protocol::Range,
} }
pub struct Range; pub struct Range;
@ -19,7 +16,7 @@ impl WholeStreamCommand for Range {
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build("range").required( Signature::build("range").required(
"rows ", "rows",
SyntaxShape::Range, SyntaxShape::Range,
"range of rows to return: Eg) 4..7 (=> from 4 to 7)", "range of rows to return: Eg) 4..7 (=> from 4 to 7)",
) )
@ -29,25 +26,35 @@ impl WholeStreamCommand for Range {
"Return only the selected rows." "Return only the selected rows."
} }
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
range(args) range(args)
} }
} }
fn range(args: CommandArgs) -> Result<ActionStream, ShellError> { fn range(args: CommandArgs) -> Result<OutputStream, ShellError> {
let (RangeArgs { area }, input) = args.process()?; let args = args.evaluate_once()?;
let range = area.item; let cmd_args = RangeArgs {
let (from, left_inclusive) = range.from; range: args.req(0)?,
let (to, right_inclusive) = range.to; };
let from = from.map(|from| *from as usize).unwrap_or(0).saturating_add(
if left_inclusive == RangeInclusion::Inclusive { let (from, left_inclusive) = cmd_args.range.from;
let (to, right_inclusive) = cmd_args.range.to;
let from_span = from.span;
let to_span = to.span;
let from = from
.map(|from| from.as_usize(from_span))
.item
.unwrap_or(0)
.saturating_add(if left_inclusive == RangeInclusion::Inclusive {
0 0
} else { } else {
1 1
}, });
);
let to = to let to = to
.map(|to| *to as usize) .map(|to| to.as_usize(to_span))
.item
.unwrap_or(usize::MAX) .unwrap_or(usize::MAX)
.saturating_sub(if right_inclusive == RangeInclusion::Inclusive { .saturating_sub(if right_inclusive == RangeInclusion::Inclusive {
0 0
@ -55,11 +62,11 @@ fn range(args: CommandArgs) -> Result<ActionStream, ShellError> {
1 1
}); });
Ok(input if from > to {
.skip(from) Ok(OutputStream::one(Value::nothing()))
.take(to - from + 1) } else {
.map(ReturnSuccess::value) Ok(args.input.skip(from).take(to - from + 1).to_output_stream())
.to_action_stream()) }
} }
#[cfg(test)] #[cfg(test)]

View File

@ -40,6 +40,7 @@ impl FromValue for num_bigint::BigInt {
} }
} }
} }
impl FromValue for Tagged<u64> { impl FromValue for Tagged<u64> {
fn from_value(v: &Value) -> Result<Self, ShellError> { fn from_value(v: &Value) -> Result<Self, ShellError> {
let tag = v.tag.clone(); let tag = v.tag.clone();