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

@ -2,8 +2,9 @@ use core::fmt::Write;
use fancy_regex::Regex;
use nu_engine::{command_prelude::*, get_columns};
use nu_parser::escape_quote_string;
use nu_protocol::ast::RangeInclusion;
use nu_protocol::Range;
use once_cell::sync::Lazy;
use std::ops::Bound;
#[derive(Clone)]
pub struct ToNuon;
@ -234,16 +235,26 @@ pub fn value_to_string(
}
}
Value::Nothing { .. } => Ok("null".to_string()),
Value::Range { val, .. } => Ok(format!(
"{}..{}{}",
value_to_string(&val.from, span, depth + 1, indent)?,
if val.inclusion == RangeInclusion::RightExclusive {
"<"
} else {
""
},
value_to_string(&val.to, span, depth + 1, indent)?
)),
Value::Range { val, .. } => match val {
Range::IntRange(range) => Ok(range.to_string()),
Range::FloatRange(range) => {
let start =
value_to_string(&Value::float(range.start(), span), span, depth + 1, indent)?;
match range.end() {
Bound::Included(end) => Ok(format!(
"{}..{}",
start,
value_to_string(&Value::float(end, span), span, depth + 1, indent)?
)),
Bound::Excluded(end) => Ok(format!(
"{}..<{}",
start,
value_to_string(&Value::float(end, span), span, depth + 1, indent)?
)),
Bound::Unbounded => Ok(format!("{start}..",)),
}
}
},
Value::Record { val, .. } => {
let mut collection = vec![];
for (col, val) in &**val {

View File

@ -116,13 +116,7 @@ fn local_into_string(value: Value, separator: &str, config: &Config) -> String {
Value::Date { val, .. } => {
format!("{} ({})", val.to_rfc2822(), HumanTime::from(val))
}
Value::Range { val, .. } => {
format!(
"{}..{}",
local_into_string(val.from, ", ", config),
local_into_string(val.to, ", ", config)
)
}
Value::Range { val, .. } => val.to_string(),
Value::String { val, .. } => val,
Value::Glob { val, .. } => val,
Value::List { vals: val, .. } => val