mirror of
https://github.com/nushell/nushell.git
synced 2025-07-01 07:00:37 +02:00
Range
refactor (#12405)
# Description Currently, `Range` is a struct with a `from`, `to`, and `incr` field, which are all type `Value`. This PR changes `Range` to be an enum over `IntRange` and `FloatRange` for better type safety / stronger compile time guarantees. Fixes: #11778 Fixes: #11777 Fixes: #11776 Fixes: #11775 Fixes: #11774 Fixes: #11773 Fixes: #11769. # User-Facing Changes Hopefully none, besides bug fixes. Although, the `serde` representation might have changed.
This commit is contained in:
@ -69,7 +69,7 @@ impl Command for For {
|
||||
let eval_expression = get_eval_expression(engine_state);
|
||||
let eval_block = get_eval_block(engine_state);
|
||||
|
||||
let values = eval_expression(engine_state, stack, keyword_expr)?;
|
||||
let value = eval_expression(engine_state, stack, keyword_expr)?;
|
||||
|
||||
let block: Block = call.req(engine_state, stack, 2)?;
|
||||
|
||||
@ -81,7 +81,8 @@ impl Command for For {
|
||||
|
||||
let stack = &mut stack.push_redirection(None, None);
|
||||
|
||||
match values {
|
||||
let span = value.span();
|
||||
match value {
|
||||
Value::List { vals, .. } => {
|
||||
for (idx, x) in ListStream::from_stream(vals.into_iter(), ctrlc).enumerate() {
|
||||
// with_env() is used here to ensure that each iteration uses
|
||||
@ -125,7 +126,7 @@ impl Command for For {
|
||||
}
|
||||
}
|
||||
Value::Range { val, .. } => {
|
||||
for (idx, x) in val.into_range_iter(ctrlc)?.enumerate() {
|
||||
for (idx, x) in val.into_range_iter(span, ctrlc).enumerate() {
|
||||
stack.add_var(
|
||||
var_id,
|
||||
if numbered {
|
||||
|
@ -1,11 +1,15 @@
|
||||
use itertools::Itertools;
|
||||
use nu_engine::command_prelude::*;
|
||||
use nu_protocol::{
|
||||
ast::{Block, RangeInclusion},
|
||||
ast::Block,
|
||||
debugger::WithoutDebug,
|
||||
engine::{StateDelta, StateWorkingSet},
|
||||
Range,
|
||||
};
|
||||
use std::{
|
||||
sync::Arc,
|
||||
{collections::HashSet, ops::Bound},
|
||||
};
|
||||
use std::{collections::HashSet, sync::Arc};
|
||||
|
||||
pub fn check_example_input_and_output_types_match_command_signature(
|
||||
example: &Example,
|
||||
@ -219,17 +223,45 @@ impl<'a> std::fmt::Debug for DebuggableValue<'a> {
|
||||
Value::Date { val, .. } => {
|
||||
write!(f, "Date({:?})", val)
|
||||
}
|
||||
Value::Range { val, .. } => match val.inclusion {
|
||||
RangeInclusion::Inclusive => write!(
|
||||
f,
|
||||
"Range({:?}..{:?}, step: {:?})",
|
||||
val.from, val.to, val.incr
|
||||
),
|
||||
RangeInclusion::RightExclusive => write!(
|
||||
f,
|
||||
"Range({:?}..<{:?}, step: {:?})",
|
||||
val.from, val.to, val.incr
|
||||
),
|
||||
Value::Range { val, .. } => match val {
|
||||
Range::IntRange(range) => match range.end() {
|
||||
Bound::Included(end) => write!(
|
||||
f,
|
||||
"Range({:?}..{:?}, step: {:?})",
|
||||
range.start(),
|
||||
end,
|
||||
range.step(),
|
||||
),
|
||||
Bound::Excluded(end) => write!(
|
||||
f,
|
||||
"Range({:?}..<{:?}, step: {:?})",
|
||||
range.start(),
|
||||
end,
|
||||
range.step(),
|
||||
),
|
||||
Bound::Unbounded => {
|
||||
write!(f, "Range({:?}.., step: {:?})", range.start(), range.step())
|
||||
}
|
||||
},
|
||||
Range::FloatRange(range) => match range.end() {
|
||||
Bound::Included(end) => write!(
|
||||
f,
|
||||
"Range({:?}..{:?}, step: {:?})",
|
||||
range.start(),
|
||||
end,
|
||||
range.step(),
|
||||
),
|
||||
Bound::Excluded(end) => write!(
|
||||
f,
|
||||
"Range({:?}..<{:?}, step: {:?})",
|
||||
range.start(),
|
||||
end,
|
||||
range.step(),
|
||||
),
|
||||
Bound::Unbounded => {
|
||||
write!(f, "Range({:?}.., step: {:?})", range.start(), range.step())
|
||||
}
|
||||
},
|
||||
},
|
||||
Value::String { val, .. } | Value::Glob { val, .. } => {
|
||||
write!(f, "{:?}", val)
|
||||
|
Reference in New Issue
Block a user