mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 06:45:59 +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:
@ -8,8 +8,8 @@ pub fn execute_json_query(
|
||||
input: &Value,
|
||||
query: Option<Spanned<String>>,
|
||||
) -> Result<Value, LabeledError> {
|
||||
let input_string = match &input.coerce_string() {
|
||||
Ok(s) => s.clone(),
|
||||
let input_string = match input.coerce_str() {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
return Err(LabeledError {
|
||||
span: Some(call.head),
|
||||
|
@ -332,15 +332,15 @@ mod tests {
|
||||
Span::test_data(),
|
||||
);
|
||||
let out = item
|
||||
.as_list()
|
||||
.into_list()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.into_iter()
|
||||
.map(|matches| {
|
||||
matches
|
||||
.as_list()
|
||||
.into_list()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|text_nodes| text_nodes.coerce_string().unwrap())
|
||||
.into_iter()
|
||||
.map(|text_nodes| text_nodes.coerce_into_string().unwrap())
|
||||
.collect::<Vec<String>>()
|
||||
})
|
||||
.collect::<Vec<Vec<String>>>();
|
||||
|
@ -21,7 +21,7 @@ pub fn execute_xpath_query(
|
||||
};
|
||||
|
||||
let xpath = build_xpath(query_string, span)?;
|
||||
let input_string = input.coerce_string()?;
|
||||
let input_string = input.coerce_str()?;
|
||||
let package = parser::parse(&input_string);
|
||||
|
||||
if package.is_err() {
|
||||
|
Reference in New Issue
Block a user