mirror of
https://github.com/nushell/nushell.git
synced 2025-08-19 08:41:29 +02:00
Add Value::coerce_str
(#11885)
# Description Following #11851, this PR adds one final conversion function for `Value`. `Value::coerce_str` takes a `&Value` and converts it to a `Cow<str>`, creating an owned `String` for types that needed converting. Otherwise, it returns a borrowed `str` for `String` and `Binary` `Value`s which avoids a clone/allocation. Where possible, `coerce_str` and `coerce_into_string` should be used instead of `coerce_string`, since `coerce_string` always allocates a new `String`.
This commit is contained in:
@@ -15,7 +15,7 @@ pub(super) fn process_string_enum<T, E>(
|
||||
E: Display,
|
||||
{
|
||||
let span = value.span();
|
||||
if let Ok(v) = value.coerce_string() {
|
||||
if let Ok(v) = value.coerce_str() {
|
||||
match v.parse() {
|
||||
Ok(format) => {
|
||||
*config_point = format;
|
||||
|
@@ -539,7 +539,7 @@ impl Value {
|
||||
process_bool_config(value, &mut errors, &mut config.filesize_metric);
|
||||
}
|
||||
"format" => {
|
||||
if let Ok(v) = value.coerce_string() {
|
||||
if let Ok(v) = value.coerce_str() {
|
||||
config.filesize_format = v.to_lowercase();
|
||||
} else {
|
||||
report_invalid_value("should be a string", span, &mut errors);
|
||||
|
@@ -272,7 +272,7 @@ pub(super) fn try_parse_trim_strategy(
|
||||
}
|
||||
|
||||
fn try_parse_trim_methodology(value: &Value) -> Option<TrimStrategy> {
|
||||
if let Ok(value) = value.coerce_string() {
|
||||
if let Ok(value) = value.coerce_str() {
|
||||
match value.to_lowercase().as_str() {
|
||||
"wrapping" => {
|
||||
return Some(TrimStrategy::Wrap {
|
||||
|
Reference in New Issue
Block a user