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

@ -90,23 +90,12 @@ Test' | from eml -b 1"
fn emailaddress_to_value(span: Span, email_address: &EmailAddress) -> Value {
let (n, a) = match email_address {
EmailAddress::AddressOnly { address } => (
Value::nothing(span),
Value::String {
val: address.to_string(),
span,
},
),
EmailAddress::NameAndEmailAddress { name, address } => (
Value::String {
val: name.to_string(),
span,
},
Value::String {
val: address.to_string(),
span,
},
),
EmailAddress::AddressOnly { address } => {
(Value::nothing(span), Value::string(address, span))
}
EmailAddress::NameAndEmailAddress { name, address } => {
(Value::string(name, span), Value::string(address, span))
}
};
Value::record(
@ -123,13 +112,13 @@ fn headerfieldvalue_to_value(head: Span, value: &HeaderFieldValue) -> Value {
match value {
SingleEmailAddress(address) => emailaddress_to_value(head, address),
MultipleEmailAddresses(addresses) => Value::List {
vals: addresses
MultipleEmailAddresses(addresses) => Value::list(
addresses
.iter()
.map(|a| emailaddress_to_value(head, a))
.collect(),
span: head,
},
head,
),
Unstructured(s) => Value::string(s, head),
Empty => Value::nothing(head),
}
@ -151,13 +140,7 @@ fn from_eml(input: &Value, body_preview: usize, head: Span) -> Result<Value, Lab
let mut collected = IndexMap::new();
if let Some(subj) = eml.subject {
collected.insert(
"Subject".to_string(),
Value::String {
val: subj,
span: head,
},
);
collected.insert("Subject".to_string(), Value::string(subj, head));
}
if let Some(from) = eml.from {
@ -173,13 +156,7 @@ fn from_eml(input: &Value, body_preview: usize, head: Span) -> Result<Value, Lab
}
if let Some(body) = eml.body {
collected.insert(
"Body".to_string(),
Value::String {
val: body,
span: head,
},
);
collected.insert("Body".to_string(), Value::string(body, head));
}
Ok(Value::record(collected.into_iter().collect(), head))

View File

@ -27,21 +27,18 @@ pub fn from_ics_call(call: &EvaluatedCall, input: &Value) -> Result<Value, Label
for calendar in parser {
match calendar {
Ok(c) => output.push(calendar_to_value(c, head)),
Err(e) => output.push(Value::Error {
error: Box::new(ShellError::UnsupportedInput(
Err(e) => output.push(Value::error(
ShellError::UnsupportedInput(
format!("input cannot be parsed as .ics ({e})"),
"value originates from here".into(),
head,
span,
)),
),
span,
}),
)),
}
}
Ok(Value::List {
vals: output,
span: head,
})
Ok(Value::list(output, head))
}
pub fn examples() -> Vec<PluginExample> {
@ -50,8 +47,8 @@ pub fn examples() -> Vec<PluginExample> {
END:VCALENDAR' | from ics"
.into(),
description: "Converts ics formatted string to table".into(),
result: Some(Value::List {
vals: vec![Value::test_record(Record {
result: Some(Value::list(
vec![Value::test_record(Record {
cols: vec![
"properties".to_string(),
"events".to_string(),
@ -62,38 +59,17 @@ pub fn examples() -> Vec<PluginExample> {
"timezones".to_string(),
],
vals: vec![
Value::List {
vals: vec![],
span: Span::test_data(),
},
Value::List {
vals: vec![],
span: Span::test_data(),
},
Value::List {
vals: vec![],
span: Span::test_data(),
},
Value::List {
vals: vec![],
span: Span::test_data(),
},
Value::List {
vals: vec![],
span: Span::test_data(),
},
Value::List {
vals: vec![],
span: Span::test_data(),
},
Value::List {
vals: vec![],
span: Span::test_data(),
},
Value::list(vec![], Span::test_data()),
Value::list(vec![], Span::test_data()),
Value::list(vec![], Span::test_data()),
Value::list(vec![], Span::test_data()),
Value::list(vec![], Span::test_data()),
Value::list(vec![], Span::test_data()),
Value::list(vec![], Span::test_data()),
],
})],
span: Span::test_data(),
}),
Span::test_data(),
)),
}]
}
@ -113,8 +89,8 @@ fn calendar_to_value(calendar: IcalCalendar, span: Span) -> Value {
}
fn events_to_value(events: Vec<IcalEvent>, span: Span) -> Value {
Value::List {
vals: events
Value::list(
events
.into_iter()
.map(|event| {
Value::record(
@ -127,12 +103,12 @@ fn events_to_value(events: Vec<IcalEvent>, span: Span) -> Value {
})
.collect::<Vec<Value>>(),
span,
}
)
}
fn alarms_to_value(alarms: Vec<IcalAlarm>, span: Span) -> Value {
Value::List {
vals: alarms
Value::list(
alarms
.into_iter()
.map(|alarm| {
Value::record(
@ -142,12 +118,12 @@ fn alarms_to_value(alarms: Vec<IcalAlarm>, span: Span) -> Value {
})
.collect::<Vec<Value>>(),
span,
}
)
}
fn todos_to_value(todos: Vec<IcalTodo>, span: Span) -> Value {
Value::List {
vals: todos
Value::list(
todos
.into_iter()
.map(|todo| {
Value::record(
@ -160,12 +136,12 @@ fn todos_to_value(todos: Vec<IcalTodo>, span: Span) -> Value {
})
.collect::<Vec<Value>>(),
span,
}
)
}
fn journals_to_value(journals: Vec<IcalJournal>, span: Span) -> Value {
Value::List {
vals: journals
Value::list(
journals
.into_iter()
.map(|journal| {
Value::record(
@ -175,12 +151,12 @@ fn journals_to_value(journals: Vec<IcalJournal>, span: Span) -> Value {
})
.collect::<Vec<Value>>(),
span,
}
)
}
fn free_busys_to_value(free_busys: Vec<IcalFreeBusy>, span: Span) -> Value {
Value::List {
vals: free_busys
Value::list(
free_busys
.into_iter()
.map(|free_busy| {
Value::record(
@ -190,12 +166,12 @@ fn free_busys_to_value(free_busys: Vec<IcalFreeBusy>, span: Span) -> Value {
})
.collect::<Vec<Value>>(),
span,
}
)
}
fn timezones_to_value(timezones: Vec<IcalTimeZone>, span: Span) -> Value {
Value::List {
vals: timezones
Value::list(
timezones
.into_iter()
.map(|timezone| {
Value::record(
@ -208,12 +184,12 @@ fn timezones_to_value(timezones: Vec<IcalTimeZone>, span: Span) -> Value {
})
.collect::<Vec<Value>>(),
span,
}
)
}
fn timezone_transitions_to_value(transitions: Vec<IcalTimeZoneTransition>, span: Span) -> Value {
Value::List {
vals: transitions
Value::list(
transitions
.into_iter()
.map(|transition| {
Value::record(
@ -223,20 +199,17 @@ fn timezone_transitions_to_value(transitions: Vec<IcalTimeZoneTransition>, span:
})
.collect::<Vec<Value>>(),
span,
}
)
}
fn properties_to_value(properties: Vec<Property>, span: Span) -> Value {
Value::List {
vals: properties
Value::list(
properties
.into_iter()
.map(|prop| {
let name = Value::String {
val: prop.name,
span,
};
let name = Value::string(prop.name, span);
let value = match prop.value {
Some(val) => Value::String { val, span },
Some(val) => Value::string(val, span),
None => Value::nothing(span),
};
let params = match prop.params {
@ -255,7 +228,7 @@ fn properties_to_value(properties: Vec<Property>, span: Span) -> Value {
})
.collect::<Vec<Value>>(),
span,
}
)
}
fn params_to_value(params: Vec<(String, Vec<String>)>, span: Span) -> Value {
@ -266,7 +239,7 @@ fn params_to_value(params: Vec<(String, Vec<String>)>, span: Span) -> Value {
.into_iter()
.map(|val| Value::string(val, span))
.collect();
let values = Value::List { vals: values, span };
let values = Value::list(values, span);
row.insert(param_name, values);
}

View File

@ -23,22 +23,19 @@ pub fn from_vcf_call(call: &EvaluatedCall, input: &Value) -> Result<Value, Label
let iter = parser.map(move |contact| match contact {
Ok(c) => contact_to_value(c, head),
Err(e) => Value::Error {
error: Box::new(ShellError::UnsupportedInput(
Err(e) => Value::error(
ShellError::UnsupportedInput(
format!("input cannot be parsed as .vcf ({e})"),
"value originates from here".into(),
head,
span,
)),
),
span,
},
),
});
let collected: Vec<_> = iter.collect();
Ok(Value::List {
vals: collected,
span: head,
})
Ok(Value::list(collected, head))
}
pub fn examples() -> Vec<PluginExample> {
@ -50,11 +47,11 @@ EMAIL:foo@bar.com
END:VCARD' | from vcf"
.into(),
description: "Converts ics formatted string to table".into(),
result: Some(Value::List {
vals: vec![Value::test_record(Record {
result: Some(Value::list(
vec![Value::test_record(Record {
cols: vec!["properties".to_string()],
vals: vec![Value::List {
vals: vec![
vals: vec![Value::list(
vec![
Value::test_record(Record {
cols: vec![
"name".to_string(),
@ -64,9 +61,7 @@ END:VCARD' | from vcf"
vals: vec![
Value::test_string("N"),
Value::test_string("Foo"),
Value::Nothing {
span: Span::test_data(),
},
Value::nothing(Span::test_data()),
],
}),
Value::test_record(Record {
@ -78,9 +73,7 @@ END:VCARD' | from vcf"
vals: vec![
Value::test_string("FN"),
Value::test_string("Bar"),
Value::Nothing {
span: Span::test_data(),
},
Value::nothing(Span::test_data()),
],
}),
Value::test_record(Record {
@ -92,17 +85,15 @@ END:VCARD' | from vcf"
vals: vec![
Value::test_string("EMAIL"),
Value::test_string("foo@bar.com"),
Value::Nothing {
span: Span::test_data(),
},
Value::nothing(Span::test_data()),
],
}),
],
span: Span::test_data(),
}],
Span::test_data(),
)],
})],
span: Span::test_data(),
}),
Span::test_data(),
)),
}]
}
@ -114,21 +105,18 @@ fn contact_to_value(contact: VcardContact, span: Span) -> Value {
}
fn properties_to_value(properties: Vec<Property>, span: Span) -> Value {
Value::List {
vals: properties
Value::list(
properties
.into_iter()
.map(|prop| {
let name = Value::String {
val: prop.name,
span,
};
let name = Value::string(prop.name, span);
let value = match prop.value {
Some(val) => Value::String { val, span },
None => Value::Nothing { span },
Some(val) => Value::string(val, span),
None => Value::nothing(span),
};
let params = match prop.params {
Some(param_list) => params_to_value(param_list, span),
None => Value::Nothing { span },
None => Value::nothing(span),
};
Value::record(
@ -142,7 +130,7 @@ fn properties_to_value(properties: Vec<Property>, span: Span) -> Value {
})
.collect::<Vec<Value>>(),
span,
}
)
}
fn params_to_value(params: Vec<(String, Vec<String>)>, span: Span) -> Value {
@ -153,7 +141,7 @@ fn params_to_value(params: Vec<(String, Vec<String>)>, span: Span) -> Value {
.into_iter()
.map(|val| Value::string(val, span))
.collect();
let values = Value::List { vals: values, span };
let values = Value::list(values, span);
row.insert(param_name, values);
}