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

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

View File

@ -64,43 +64,46 @@ impl Command for SubCommand {
fn to_url(input: PipelineData, head: Span) -> Result<PipelineData, ShellError> {
let output: Result<String, ShellError> = input
.into_iter()
.map(move |value| match value {
Value::Record { ref val, span } => {
let mut row_vec = vec![];
for (k, v) in val {
match v.as_string() {
Ok(s) => {
row_vec.push((k.clone(), s.to_string()));
}
_ => {
return Err(ShellError::UnsupportedInput(
"Expected a record with string values".to_string(),
"value originates from here".into(),
head,
span,
));
.map(move |value| {
let span = value.span();
match value {
Value::Record { ref val, .. } => {
let mut row_vec = vec![];
for (k, v) in val {
match v.as_string() {
Ok(s) => {
row_vec.push((k.clone(), s.to_string()));
}
_ => {
return Err(ShellError::UnsupportedInput(
"Expected a record with string values".to_string(),
"value originates from here".into(),
head,
span,
));
}
}
}
}
match serde_urlencoded::to_string(row_vec) {
Ok(s) => Ok(s),
_ => Err(ShellError::CantConvert {
to_type: "URL".into(),
from_type: value.get_type().to_string(),
span: head,
help: None,
}),
match serde_urlencoded::to_string(row_vec) {
Ok(s) => Ok(s),
_ => Err(ShellError::CantConvert {
to_type: "URL".into(),
from_type: value.get_type().to_string(),
span: head,
help: None,
}),
}
}
// Propagate existing errors
Value::Error { error, .. } => Err(*error),
other => Err(ShellError::UnsupportedInput(
"Expected a table from pipeline".to_string(),
"value originates from here".into(),
head,
other.span(),
)),
}
// Propagate existing errors
Value::Error { error, .. } => Err(*error),
other => Err(ShellError::UnsupportedInput(
"Expected a table from pipeline".to_string(),
"value originates from here".into(),
head,
other.span(),
)),
})
.collect();

View File

@ -76,14 +76,14 @@ impl Command for SubCommand {
Example {
description: "Encode multiple urls with escape characters in list",
example: "['https://example.com/foo bar' 'https://example.com/a>b' '中文字/eng/12 34'] | url encode",
result: Some(Value::List {
vals: vec![
result: Some(Value::list(
vec![
Value::test_string("https://example.com/foo%20bar"),
Value::test_string("https://example.com/a%3Eb"),
Value::test_string("%E4%B8%AD%E6%96%87%E5%AD%97/eng/12%2034"),
],
span: Span::test_data(),
}),
Span::test_data(),
)),
},
Example {
description: "Encode all non alphanumeric chars with all flag",
@ -98,21 +98,18 @@ fn action_all(input: &Value, _arg: &CellPathOnlyArgs, head: Span) -> Value {
match input {
Value::String { val, .. } => {
const FRAGMENT: &AsciiSet = NON_ALPHANUMERIC;
Value::String {
val: utf8_percent_encode(val, FRAGMENT).to_string(),
span: head,
}
Value::string(utf8_percent_encode(val, FRAGMENT).to_string(), head)
}
Value::Error { .. } => input.clone(),
_ => Value::Error {
error: Box::new(ShellError::OnlySupportsThisInputType {
_ => Value::error(
ShellError::OnlySupportsThisInputType {
exp_input_type: "string".into(),
wrong_type: input.get_type().to_string(),
dst_span: head,
src_span: input.span(),
}),
span: head,
},
},
head,
),
}
}
@ -120,21 +117,18 @@ fn action(input: &Value, _arg: &CellPathOnlyArgs, head: Span) -> Value {
match input {
Value::String { val, .. } => {
const FRAGMENT: &AsciiSet = &NON_ALPHANUMERIC.remove(b'/').remove(b':').remove(b'.');
Value::String {
val: utf8_percent_encode(val, FRAGMENT).to_string(),
span: head,
}
Value::string(utf8_percent_encode(val, FRAGMENT).to_string(), head)
}
Value::Error { .. } => input.clone(),
_ => Value::Error {
error: Box::new(ShellError::OnlySupportsThisInputType {
_ => Value::error(
ShellError::OnlySupportsThisInputType {
exp_input_type: "string".into(),
wrong_type: input.get_type().to_string(),
dst_span: head,
src_span: input.span(),
}),
span: head,
},
},
head,
),
}
}

View File

@ -91,23 +91,26 @@ impl Command for SubCommand {
let output: Result<String, ShellError> = input
.into_iter()
.map(move |value| match value {
Value::Record { val, span } => {
let url_components = val
.into_iter()
.try_fold(UrlComponents::new(), |url, (k, v)| {
url.add_component(k, v, span)
});
.map(move |value| {
let span = value.span();
match value {
Value::Record { val, .. } => {
let url_components = val
.into_iter()
.try_fold(UrlComponents::new(), |url, (k, v)| {
url.add_component(k, v, span)
});
url_components?.to_url(span)
url_components?.to_url(span)
}
Value::Error { error, .. } => Err(*error),
other => Err(ShellError::UnsupportedInput(
"Expected a record from pipeline".to_string(),
"value originates from here".into(),
head,
other.span(),
)),
}
Value::Error { error, .. } => Err(*error),
other => Err(ShellError::UnsupportedInput(
"Expected a record from pipeline".to_string(),
"value originates from here".into(),
head,
other.span(),
)),
})
.collect();
@ -135,9 +138,10 @@ impl UrlComponents {
}
pub fn add_component(self, key: String, value: Value, _span: Span) -> Result<Self, ShellError> {
let span = value.span();
if key == "port" {
return match value {
Value::String { val, span } => {
Value::String { val, .. } => {
if val.trim().is_empty() {
Ok(self)
} else {
@ -155,7 +159,7 @@ impl UrlComponents {
}
}
}
Value::Int { val, span: _ } => Ok(Self {
Value::Int { val, .. } => Ok(Self {
port: Some(val),
..self
}),
@ -171,7 +175,7 @@ impl UrlComponents {
if key == "params" {
return match value {
Value::Record { ref val, span } => {
Value::Record { ref val, .. } => {
let mut qs = val
.iter()
.map(|(k, v)| match v.as_string() {

View File

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