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:
Ian Manske
2024-04-06 14:04:56 +00:00
committed by GitHub
parent 75fedcc8dd
commit 7a7d43344e
27 changed files with 1126 additions and 798 deletions

View File

@ -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)