Move Value to helpers, separate span call (#10121)

# Description

As part of the refactor to split spans off of Value, this moves to using
helper functions to create values, and using `.span()` instead of
matching span out of Value directly.

Hoping to get a few more helping hands to finish this, as there are a
lot of commands to update :)

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->

---------

Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
Co-authored-by: WindSoilder <windsoilder@outlook.com>
This commit is contained in:
JT
2023-09-04 02:27:29 +12:00
committed by GitHub
parent af79eb2943
commit 6cdfee3573
372 changed files with 5811 additions and 7448 deletions

View File

@@ -19,19 +19,11 @@ impl FromValue for Value {
impl FromValue for Spanned<i64> {
fn from_value(v: &Value) -> Result<Self, ShellError> {
let span = v.span();
match v {
Value::Int { val, span } => Ok(Spanned {
item: *val,
span: *span,
}),
Value::Filesize { val, span } => Ok(Spanned {
item: *val,
span: *span,
}),
Value::Duration { val, span } => Ok(Spanned {
item: *val,
span: *span,
}),
Value::Int { val, .. } => Ok(Spanned { item: *val, span }),
Value::Filesize { val, .. } => Ok(Spanned { item: *val, span }),
Value::Duration { val, .. } => Ok(Spanned { item: *val, span }),
v => Err(ShellError::CantConvert {
to_type: "integer".into(),
@@ -62,15 +54,13 @@ impl FromValue for i64 {
impl FromValue for Spanned<f64> {
fn from_value(v: &Value) -> Result<Self, ShellError> {
let span = v.span();
match v {
Value::Int { val, span } => Ok(Spanned {
Value::Int { val, .. } => Ok(Spanned {
item: *val as f64,
span: *span,
}),
Value::Float { val, span } => Ok(Spanned {
item: *val,
span: *span,
span,
}),
Value::Float { val, .. } => Ok(Spanned { item: *val, span }),
v => Err(ShellError::CantConvert {
to_type: "float".into(),
@@ -99,34 +89,35 @@ impl FromValue for f64 {
impl FromValue for Spanned<usize> {
fn from_value(v: &Value) -> Result<Self, ShellError> {
let span = v.span();
match v {
Value::Int { val, span } => {
Value::Int { val, .. } => {
if val.is_negative() {
Err(ShellError::NeedsPositiveValue(*span))
Err(ShellError::NeedsPositiveValue(span))
} else {
Ok(Spanned {
item: *val as usize,
span: *span,
span,
})
}
}
Value::Filesize { val, span } => {
Value::Filesize { val, .. } => {
if val.is_negative() {
Err(ShellError::NeedsPositiveValue(*span))
Err(ShellError::NeedsPositiveValue(span))
} else {
Ok(Spanned {
item: *val as usize,
span: *span,
span,
})
}
}
Value::Duration { val, span } => {
Value::Duration { val, .. } => {
if val.is_negative() {
Err(ShellError::NeedsPositiveValue(*span))
Err(ShellError::NeedsPositiveValue(span))
} else {
Ok(Spanned {
item: *val as usize,
span: *span,
span,
})
}
}
@@ -143,24 +134,25 @@ impl FromValue for Spanned<usize> {
impl FromValue for usize {
fn from_value(v: &Value) -> Result<Self, ShellError> {
let span = v.span();
match v {
Value::Int { val, span } => {
Value::Int { val, .. } => {
if val.is_negative() {
Err(ShellError::NeedsPositiveValue(*span))
Err(ShellError::NeedsPositiveValue(span))
} else {
Ok(*val as usize)
}
}
Value::Filesize { val, span } => {
Value::Filesize { val, .. } => {
if val.is_negative() {
Err(ShellError::NeedsPositiveValue(*span))
Err(ShellError::NeedsPositiveValue(span))
} else {
Ok(*val as usize)
}
}
Value::Duration { val, span } => {
Value::Duration { val, .. } => {
if val.is_negative() {
Err(ShellError::NeedsPositiveValue(*span))
Err(ShellError::NeedsPositiveValue(span))
} else {
Ok(*val as usize)
}
@@ -244,17 +236,20 @@ impl FromValue for Vec<Spanned<String>> {
match v {
Value::List { vals, .. } => vals
.iter()
.map(|val| match val {
Value::String { val, span } => Ok(Spanned {
item: val.clone(),
span: *span,
}),
c => Err(ShellError::CantConvert {
to_type: "string".into(),
from_type: c.get_type().to_string(),
span: c.span(),
help: None,
}),
.map(|val| {
let val_span = val.span();
match val {
Value::String { val, .. } => Ok(Spanned {
item: val.clone(),
span: val_span,
}),
c => Err(ShellError::CantConvert {
to_type: "string".into(),
from_type: c.get_type().to_string(),
span: c.span(),
help: None,
}),
}
})
.collect::<Result<Vec<Spanned<String>>, ShellError>>(),
v => Err(ShellError::CantConvert {
@@ -304,14 +299,14 @@ impl FromValue for CellPath {
optional: false,
}],
}),
Value::Int { val, span } => {
Value::Int { val, .. } => {
if val.is_negative() {
Err(ShellError::NeedsPositiveValue(*span))
Err(ShellError::NeedsPositiveValue(span))
} else {
Ok(CellPath {
members: vec![PathMember::Int {
val: *val as usize,
span: *span,
span,
optional: false,
}],
})
@@ -343,11 +338,9 @@ impl FromValue for bool {
impl FromValue for Spanned<bool> {
fn from_value(v: &Value) -> Result<Self, ShellError> {
let span = v.span();
match v {
Value::Bool { val, span } => Ok(Spanned {
item: *val,
span: *span,
}),
Value::Bool { val, .. } => Ok(Spanned { item: *val, span }),
v => Err(ShellError::CantConvert {
to_type: "bool".into(),
from_type: v.get_type().to_string(),
@@ -374,11 +367,9 @@ impl FromValue for DateTime<FixedOffset> {
impl FromValue for Spanned<DateTime<FixedOffset>> {
fn from_value(v: &Value) -> Result<Self, ShellError> {
let span = v.span();
match v {
Value::Date { val, span } => Ok(Spanned {
item: *val,
span: *span,
}),
Value::Date { val, .. } => Ok(Spanned { item: *val, span }),
v => Err(ShellError::CantConvert {
to_type: "date".into(),
from_type: v.get_type().to_string(),
@@ -405,10 +396,11 @@ impl FromValue for Range {
impl FromValue for Spanned<Range> {
fn from_value(v: &Value) -> Result<Self, ShellError> {
let span = v.span();
match v {
Value::Range { val, span } => Ok(Spanned {
Value::Range { val, .. } => Ok(Spanned {
item: (**val).clone(),
span: *span,
span,
}),
v => Err(ShellError::CantConvert {
to_type: "range".into(),
@@ -437,14 +429,15 @@ impl FromValue for Vec<u8> {
impl FromValue for Spanned<Vec<u8>> {
fn from_value(v: &Value) -> Result<Self, ShellError> {
let span = v.span();
match v {
Value::Binary { val, span } => Ok(Spanned {
Value::Binary { val, .. } => Ok(Spanned {
item: val.clone(),
span: *span,
span,
}),
Value::String { val, span } => Ok(Spanned {
Value::String { val, .. } => Ok(Spanned {
item: val.bytes().collect(),
span: *span,
span,
}),
v => Err(ShellError::CantConvert {
to_type: "binary data".into(),
@@ -458,11 +451,12 @@ impl FromValue for Spanned<Vec<u8>> {
impl FromValue for Spanned<PathBuf> {
fn from_value(v: &Value) -> Result<Self, ShellError> {
let span = v.span();
match v {
Value::String { val, span } => Ok(Spanned {
Value::String { val, .. } => Ok(Spanned {
item: PathBuf::from_str(val)
.map_err(|err| ShellError::FileNotFoundCustom(err.to_string(), *span))?,
span: *span,
.map_err(|err| ShellError::FileNotFoundCustom(err.to_string(), span))?,
span,
}),
v => Err(ShellError::CantConvert {
to_type: "range".into(),
@@ -540,17 +534,14 @@ impl FromValue for Block {
impl FromValue for Spanned<Closure> {
fn from_value(v: &Value) -> Result<Self, ShellError> {
let span = v.span();
match v {
Value::Closure {
val,
captures,
span,
} => Ok(Spanned {
Value::Closure { val, captures, .. } => Ok(Spanned {
item: Closure {
block_id: *val,
captures: captures.clone(),
},
span: *span,
span,
}),
v => Err(ShellError::CantConvert {
to_type: "Closure".into(),
@@ -564,10 +555,11 @@ impl FromValue for Spanned<Closure> {
impl FromValue for Spanned<MatchPattern> {
fn from_value(v: &Value) -> Result<Self, ShellError> {
let span = v.span();
match v {
Value::MatchPattern { val, span } => Ok(Spanned {
Value::MatchPattern { val, .. } => Ok(Spanned {
item: *val.clone(),
span: *span,
span,
}),
v => Err(ShellError::CantConvert {
to_type: "Match pattern".into(),

File diff suppressed because it is too large Load Diff

View File

@@ -171,15 +171,12 @@ impl RangeIterator {
let is_end_inclusive = range.is_end_inclusive();
let start = match range.from {
Value::Nothing { .. } => Value::Int { val: 0, span },
Value::Nothing { .. } => Value::int(0, span),
x => x,
};
let end = match range.to {
Value::Nothing { .. } => Value::Int {
val: i64::MAX,
span,
},
Value::Nothing { .. } => Value::int(i64::MAX, span),
x => x,
};
@@ -215,10 +212,9 @@ impl Iterator for RangeIterator {
let Some(ordering) = ordering else {
self.done = true;
return Some(Value::Error {
error: Box::new(ShellError::CannotCreateRange { span: self.span }),
span: self.span,
});
return Some(Value::error(ShellError::CannotCreateRange { span: self.span },
self.span,
));
};
let desired_ordering = if self.moves_up {
@@ -236,10 +232,7 @@ impl Iterator for RangeIterator {
Err(error) => {
self.done = true;
return Some(Value::Error {
error: Box::new(error),
span: self.span,
});
return Some(Value::error(error, self.span));
}
};
std::mem::swap(&mut self.curr, &mut next);

View File

@@ -108,10 +108,7 @@ impl Iterator for RawStream {
v.insert(0, b);
}
}
Value::Binary {
val: v,
span: self.span,
}
Value::binary(v, self.span)
})
})
} else {
@@ -129,10 +126,7 @@ impl Iterator for RawStream {
match String::from_utf8(v.clone()) {
Ok(s) => {
// Great, we have a complete string, let's output it
Some(Ok(Value::String {
val: s,
span: self.span,
}))
Some(Ok(Value::string(s, self.span)))
}
Err(err) => {
// Okay, we *might* have a string but we've also got some errors
@@ -146,10 +140,7 @@ impl Iterator for RawStream {
// that it's not just a character spanning two frames.
// We now know we are definitely binary, so switch to binary and stay there.
self.is_binary = true;
Some(Ok(Value::Binary {
val: v,
span: self.span,
}))
Some(Ok(Value::binary(v, self.span)))
} else {
// Okay, we have a tiny bit of error at the end of the buffer. This could very well be
// a character that spans two frames. Since this is the case, remove the error from
@@ -159,17 +150,11 @@ impl Iterator for RawStream {
let buf = v[0..err.utf8_error().valid_up_to()].to_vec();
match String::from_utf8(buf) {
Ok(s) => Some(Ok(Value::String {
val: s,
span: self.span,
})),
Ok(s) => Some(Ok(Value::string(s, self.span))),
Err(_) => {
// Something is definitely wrong. Switch to binary, and stay there
self.is_binary = true;
Some(Ok(Value::Binary {
val: v,
span: self.span,
}))
Some(Ok(Value::binary(v, self.span)))
}
}
}
@@ -179,10 +164,7 @@ impl Iterator for RawStream {
Err(e) => Some(Err(e)),
}
} else if !self.leftover.is_empty() {
let output = Ok(Value::Binary {
val: self.leftover.clone(),
span: self.span,
});
let output = Ok(Value::binary(self.leftover.clone(), self.span));
self.leftover.clear();
Some(output)

View File

@@ -34,72 +34,39 @@ pub enum Unit {
impl Unit {
pub fn to_value(&self, size: i64, span: Span) -> Result<Value, ShellError> {
match self {
Unit::Byte => Ok(Value::Filesize { val: size, span }),
Unit::Kilobyte => Ok(Value::Filesize {
val: size * 1000,
Unit::Byte => Ok(Value::filesize(size, span)),
Unit::Kilobyte => Ok(Value::filesize(size * 1000, span)),
Unit::Megabyte => Ok(Value::filesize(size * 1000 * 1000, span)),
Unit::Gigabyte => Ok(Value::filesize(size * 1000 * 1000 * 1000, span)),
Unit::Terabyte => Ok(Value::filesize(size * 1000 * 1000 * 1000 * 1000, span)),
Unit::Petabyte => Ok(Value::filesize(
size * 1000 * 1000 * 1000 * 1000 * 1000,
span,
}),
Unit::Megabyte => Ok(Value::Filesize {
val: size * 1000 * 1000,
)),
Unit::Exabyte => Ok(Value::filesize(
size * 1000 * 1000 * 1000 * 1000 * 1000 * 1000,
span,
}),
Unit::Gigabyte => Ok(Value::Filesize {
val: size * 1000 * 1000 * 1000,
span,
}),
Unit::Terabyte => Ok(Value::Filesize {
val: size * 1000 * 1000 * 1000 * 1000,
span,
}),
Unit::Petabyte => Ok(Value::Filesize {
val: size * 1000 * 1000 * 1000 * 1000 * 1000,
span,
}),
Unit::Exabyte => Ok(Value::Filesize {
val: size * 1000 * 1000 * 1000 * 1000 * 1000 * 1000,
span,
}),
)),
Unit::Kibibyte => Ok(Value::Filesize {
val: size * 1024,
Unit::Kibibyte => Ok(Value::filesize(size * 1024, span)),
Unit::Mebibyte => Ok(Value::filesize(size * 1024 * 1024, span)),
Unit::Gibibyte => Ok(Value::filesize(size * 1024 * 1024 * 1024, span)),
Unit::Tebibyte => Ok(Value::filesize(size * 1024 * 1024 * 1024 * 1024, span)),
Unit::Pebibyte => Ok(Value::filesize(
size * 1024 * 1024 * 1024 * 1024 * 1024,
span,
}),
Unit::Mebibyte => Ok(Value::Filesize {
val: size * 1024 * 1024,
)),
Unit::Exbibyte => Ok(Value::filesize(
size * 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
span,
}),
Unit::Gibibyte => Ok(Value::Filesize {
val: size * 1024 * 1024 * 1024,
span,
}),
Unit::Tebibyte => Ok(Value::Filesize {
val: size * 1024 * 1024 * 1024 * 1024,
span,
}),
Unit::Pebibyte => Ok(Value::Filesize {
val: size * 1024 * 1024 * 1024 * 1024 * 1024,
span,
}),
Unit::Exbibyte => Ok(Value::Filesize {
val: size * 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
span,
}),
)),
Unit::Nanosecond => Ok(Value::Duration { val: size, span }),
Unit::Microsecond => Ok(Value::Duration {
val: size * 1000,
span,
}),
Unit::Millisecond => Ok(Value::Duration {
val: size * 1000 * 1000,
span,
}),
Unit::Second => Ok(Value::Duration {
val: size * 1000 * 1000 * 1000,
span,
}),
Unit::Nanosecond => Ok(Value::duration(size, span)),
Unit::Microsecond => Ok(Value::duration(size * 1000, span)),
Unit::Millisecond => Ok(Value::duration(size * 1000 * 1000, span)),
Unit::Second => Ok(Value::duration(size * 1000 * 1000 * 1000, span)),
Unit::Minute => match size.checked_mul(1000 * 1000 * 1000 * 60) {
Some(val) => Ok(Value::Duration { val, span }),
Some(val) => Ok(Value::duration(val, span)),
None => Err(ShellError::GenericError(
"duration too large".into(),
"duration too large".into(),
@@ -109,7 +76,7 @@ impl Unit {
)),
},
Unit::Hour => match size.checked_mul(1000 * 1000 * 1000 * 60 * 60) {
Some(val) => Ok(Value::Duration { val, span }),
Some(val) => Ok(Value::duration(val, span)),
None => Err(ShellError::GenericError(
"duration too large".into(),
"duration too large".into(),
@@ -119,7 +86,7 @@ impl Unit {
)),
},
Unit::Day => match size.checked_mul(1000 * 1000 * 1000 * 60 * 60 * 24) {
Some(val) => Ok(Value::Duration { val, span }),
Some(val) => Ok(Value::duration(val, span)),
None => Err(ShellError::GenericError(
"duration too large".into(),
"duration too large".into(),
@@ -129,7 +96,7 @@ impl Unit {
)),
},
Unit::Week => match size.checked_mul(1000 * 1000 * 1000 * 60 * 60 * 24 * 7) {
Some(val) => Ok(Value::Duration { val, span }),
Some(val) => Ok(Value::duration(val, span)),
None => Err(ShellError::GenericError(
"duration too large".into(),
"duration too large".into(),