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

@ -107,15 +107,15 @@ where
Ok(v @ Value::Error { .. }) => return Ok(v.into_pipeline_data()),
// Unsupported data
Ok(other) => {
return Ok(Value::Error {
error: Box::new(ShellError::OnlySupportsThisInputType {
return Ok(Value::error(
ShellError::OnlySupportsThisInputType {
exp_input_type: "string and binary".into(),
wrong_type: other.get_type().to_string(),
dst_span: span,
src_span: other.span(),
}),
},
span,
}
)
.into_pipeline_data());
}
Err(err) => return Err(err),
@ -123,17 +123,9 @@ where
}
let digest = hasher.finalize();
if args.binary {
Ok(Value::Binary {
val: digest.to_vec(),
span,
}
.into_pipeline_data())
Ok(Value::binary(digest.to_vec(), span).into_pipeline_data())
} else {
Ok(Value::String {
val: format!("{digest:x}"),
span,
}
.into_pipeline_data())
Ok(Value::string(format!("{digest:x}"), span).into_pipeline_data())
}
}
_ => operate(
@ -152,37 +144,32 @@ where
D: HashDigest,
digest::Output<D>: core::fmt::LowerHex,
{
let span = input.span();
let (bytes, span) = match input {
Value::String { val, span } => (val.as_bytes(), *span),
Value::Binary { val, span } => (val.as_slice(), *span),
Value::String { val, .. } => (val.as_bytes(), span),
Value::Binary { val, .. } => (val.as_slice(), span),
// Propagate existing errors
Value::Error { .. } => return input.clone(),
other => {
let span = input.span();
return Value::Error {
error: Box::new(ShellError::OnlySupportsThisInputType {
return Value::error(
ShellError::OnlySupportsThisInputType {
exp_input_type: "string or binary".into(),
wrong_type: other.get_type().to_string(),
dst_span: span,
src_span: other.span(),
}),
},
span,
};
);
}
};
let digest = D::digest(bytes);
if args.binary {
Value::Binary {
val: digest.to_vec(),
span,
}
Value::binary(digest.to_vec(), span)
} else {
Value::String {
val: format!("{digest:x}"),
span,
}
Value::string(format!("{digest:x}"), span)
}
}

View File

@ -32,16 +32,16 @@ impl Command for Hash {
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
Ok(Value::String {
val: get_full_help(
Ok(Value::string(
get_full_help(
&Self.signature(),
&Self.examples(),
engine_state,
stack,
self.is_parser_keyword(),
),
span: call.head,
}
call.head,
)
.into_pipeline_data())
}
}

View File

@ -14,21 +14,21 @@ impl HashDigest for Md5 {
Example {
description: "Return the md5 hash of a string, hex-encoded",
example: "'abcdefghijklmnopqrstuvwxyz' | hash md5",
result: Some(Value::String {
val: "c3fcd3d76192e4007dfb496cca67e13b".to_owned(),
span: Span::test_data(),
}),
result: Some(Value::string(
"c3fcd3d76192e4007dfb496cca67e13b".to_owned(),
Span::test_data(),
)),
},
Example {
description: "Return the md5 hash of a string, as binary",
example: "'abcdefghijklmnopqrstuvwxyz' | hash md5 --binary",
result: Some(Value::Binary {
val: vec![
result: Some(Value::binary(
vec![
0xc3, 0xfc, 0xd3, 0xd7, 0x61, 0x92, 0xe4, 0x00, 0x7d, 0xfb, 0x49, 0x6c,
0xca, 0x67, 0xe1, 0x3b,
],
span: Span::test_data(),
}),
Span::test_data(),
)),
},
Example {
description: "Return the md5 hash of a file's contents",
@ -51,14 +51,11 @@ mod tests {
#[test]
fn hash_string() {
let binary = Value::String {
val: "abcdefghijklmnopqrstuvwxyz".to_owned(),
span: Span::test_data(),
};
let expected = Value::String {
val: "c3fcd3d76192e4007dfb496cca67e13b".to_owned(),
span: Span::test_data(),
};
let binary = Value::string("abcdefghijklmnopqrstuvwxyz".to_owned(), Span::test_data());
let expected = Value::string(
"c3fcd3d76192e4007dfb496cca67e13b".to_owned(),
Span::test_data(),
);
let actual = generic_digest::action::<Md5>(
&binary,
&Arguments {
@ -72,14 +69,11 @@ mod tests {
#[test]
fn hash_bytes() {
let binary = Value::Binary {
val: vec![0xC0, 0xFF, 0xEE],
span: Span::test_data(),
};
let expected = Value::String {
val: "5f80e231382769b0102b1164cf722d83".to_owned(),
span: Span::test_data(),
};
let binary = Value::binary(vec![0xC0, 0xFF, 0xEE], Span::test_data());
let expected = Value::string(
"5f80e231382769b0102b1164cf722d83".to_owned(),
Span::test_data(),
);
let actual = generic_digest::action::<Md5>(
&binary,
&Arguments {

View File

@ -14,23 +14,22 @@ impl HashDigest for Sha256 {
Example {
description: "Return the sha256 hash of a string, hex-encoded",
example: "'abcdefghijklmnopqrstuvwxyz' | hash sha256",
result: Some(Value::String {
val: "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73"
.to_owned(),
span: Span::test_data(),
}),
result: Some(Value::string(
"71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73".to_owned(),
Span::test_data(),
)),
},
Example {
description: "Return the sha256 hash of a string, as binary",
example: "'abcdefghijklmnopqrstuvwxyz' | hash sha256 --binary",
result: Some(Value::Binary {
val: vec![
result: Some(Value::binary(
vec![
0x71, 0xc4, 0x80, 0xdf, 0x93, 0xd6, 0xae, 0x2f, 0x1e, 0xfa, 0xd1, 0x44,
0x7c, 0x66, 0xc9, 0x52, 0x5e, 0x31, 0x62, 0x18, 0xcf, 0x51, 0xfc, 0x8d,
0x9e, 0xd8, 0x32, 0xf2, 0xda, 0xf1, 0x8b, 0x73,
],
span: Span::test_data(),
}),
Span::test_data(),
)),
},
Example {
description: "Return the sha256 hash of a file's contents",
@ -53,14 +52,11 @@ mod tests {
#[test]
fn hash_string() {
let binary = Value::String {
val: "abcdefghijklmnopqrstuvwxyz".to_owned(),
span: Span::test_data(),
};
let expected = Value::String {
val: "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73".to_owned(),
span: Span::test_data(),
};
let binary = Value::string("abcdefghijklmnopqrstuvwxyz".to_owned(), Span::test_data());
let expected = Value::string(
"71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73".to_owned(),
Span::test_data(),
);
let actual = generic_digest::action::<Sha256>(
&binary,
&Arguments {
@ -74,14 +70,11 @@ mod tests {
#[test]
fn hash_bytes() {
let binary = Value::Binary {
val: vec![0xC0, 0xFF, 0xEE],
span: Span::test_data(),
};
let expected = Value::String {
val: "c47a10dc272b1221f0380a2ae0f7d7fa830b3e378f2f5309bbf13f61ad211913".to_owned(),
span: Span::test_data(),
};
let binary = Value::binary(vec![0xC0, 0xFF, 0xEE], Span::test_data());
let expected = Value::string(
"c47a10dc272b1221f0380a2ae0f7d7fa830b3e378f2f5309bbf13f61ad211913".to_owned(),
Span::test_data(),
);
let actual = generic_digest::action::<Sha256>(
&binary,
&Arguments {