diff --git a/crates/nu-cli/src/completions/completer.rs b/crates/nu-cli/src/completions/completer.rs index 007a0e288a..14e1d91128 100644 --- a/crates/nu-cli/src/completions/completer.rs +++ b/crates/nu-cli/src/completions/completer.rs @@ -8,7 +8,7 @@ use nu_parser::{flatten_pipeline_element, parse, FlatShape}; use nu_protocol::{ debugger::WithoutDebug, engine::{Closure, EngineState, Stack, StateWorkingSet}, - PipelineData, Span, Value, + PipelineData, Span, TryIntoValue, Value, }; use reedline::{Completer as ReedlineCompleter, Suggestion}; use std::{str, sync::Arc}; @@ -103,7 +103,7 @@ impl NuCompleter { PipelineData::empty(), ); - match result.and_then(|data| data.into_value(span)) { + match result.and_then(|data| data.try_into_value(span)) { Ok(value) => { if let Value::List { vals, .. } = value { let result = diff --git a/crates/nu-cli/src/completions/custom_completions.rs b/crates/nu-cli/src/completions/custom_completions.rs index f14971d466..d8dbb93f42 100644 --- a/crates/nu-cli/src/completions/custom_completions.rs +++ b/crates/nu-cli/src/completions/custom_completions.rs @@ -7,7 +7,7 @@ use nu_protocol::{ ast::{Argument, Call, Expr, Expression}, debugger::WithoutDebug, engine::{Stack, StateWorkingSet}, - PipelineData, Span, Type, Value, + PipelineData, Span, TryIntoValue, Type, Value, }; use nu_utils::IgnoreCaseExt; use std::collections::HashMap; @@ -72,7 +72,7 @@ impl Completer for CustomCompletion { // Parse result let suggestions = result - .and_then(|data| data.into_value(span)) + .and_then(|data| data.try_into_value(span)) .map(|value| match &value { Value::Record { val, .. } => { let completions = val diff --git a/crates/nu-cli/src/menus/menu_completions.rs b/crates/nu-cli/src/menus/menu_completions.rs index c65f0bd100..d39b3a0d08 100644 --- a/crates/nu-cli/src/menus/menu_completions.rs +++ b/crates/nu-cli/src/menus/menu_completions.rs @@ -2,7 +2,7 @@ use nu_engine::eval_block; use nu_protocol::{ debugger::WithoutDebug, engine::{EngineState, Stack}, - IntoPipelineData, Span, Value, + IntoPipelineData, Span, TryIntoValue, Value, }; use reedline::{menu_functions::parse_selection_char, Completer, Suggestion}; use std::sync::Arc; @@ -59,7 +59,7 @@ impl Completer for NuMenuCompleter { let res = eval_block::(&self.engine_state, &mut self.stack, block, input); - if let Ok(values) = res.and_then(|data| data.into_value(self.span)) { + if let Ok(values) = res.and_then(|data| data.try_into_value(self.span)) { convert_to_suggestions(values, line, pos, self.only_buffer_difference) } else { Vec::new() diff --git a/crates/nu-cmd-extra/src/extra/filters/each_while.rs b/crates/nu-cmd-extra/src/extra/filters/each_while.rs index 58679c8eea..77b85e78e2 100644 --- a/crates/nu-cmd-extra/src/extra/filters/each_while.rs +++ b/crates/nu-cmd-extra/src/extra/filters/each_while.rs @@ -1,5 +1,5 @@ use nu_engine::{command_prelude::*, ClosureEval, ClosureEvalOnce}; -use nu_protocol::engine::Closure; +use nu_protocol::{engine::Closure, TryIntoValue}; #[derive(Clone)] pub struct EachWhile; @@ -82,7 +82,7 @@ impl Command for EachWhile { .map_while(move |value| { match closure .run_with_value(value) - .and_then(|data| data.into_value(head)) + .and_then(|data| data.try_into_value(head)) { Ok(value) => (!value.is_nothing()).then_some(value), Err(_) => None, @@ -100,7 +100,7 @@ impl Command for EachWhile { let value = value.ok()?; match closure .run_with_value(value) - .and_then(|data| data.into_value(span)) + .and_then(|data| data.try_into_value(span)) { Ok(value) => (!value.is_nothing()).then_some(value), Err(_) => None, diff --git a/crates/nu-cmd-extra/src/extra/filters/roll/roll_down.rs b/crates/nu-cmd-extra/src/extra/filters/roll/roll_down.rs index 24ea1bc309..cdff611e54 100644 --- a/crates/nu-cmd-extra/src/extra/filters/roll/roll_down.rs +++ b/crates/nu-cmd-extra/src/extra/filters/roll/roll_down.rs @@ -1,5 +1,6 @@ use super::{vertical_rotate_value, VerticalDirection}; use nu_engine::command_prelude::*; +use nu_protocol::TryIntoValue; #[derive(Clone)] pub struct RollDown; @@ -56,7 +57,7 @@ impl Command for RollDown { let by: Option = call.get_flag(engine_state, stack, "by")?; let metadata = input.metadata(); - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; let rotated_value = vertical_rotate_value(value, by, VerticalDirection::Down)?; Ok(rotated_value.into_pipeline_data().set_metadata(metadata)) diff --git a/crates/nu-cmd-extra/src/extra/filters/roll/roll_left.rs b/crates/nu-cmd-extra/src/extra/filters/roll/roll_left.rs index 789b70830d..d85a964457 100644 --- a/crates/nu-cmd-extra/src/extra/filters/roll/roll_left.rs +++ b/crates/nu-cmd-extra/src/extra/filters/roll/roll_left.rs @@ -1,5 +1,6 @@ use super::{horizontal_rotate_value, HorizontalDirection}; use nu_engine::command_prelude::*; +use nu_protocol::TryIntoValue; #[derive(Clone)] pub struct RollLeft; @@ -94,7 +95,7 @@ impl Command for RollLeft { let metadata = input.metadata(); let cells_only = call.has_flag(engine_state, stack, "cells-only")?; - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; let rotated_value = horizontal_rotate_value(value, by, cells_only, &HorizontalDirection::Left)?; diff --git a/crates/nu-cmd-extra/src/extra/filters/roll/roll_right.rs b/crates/nu-cmd-extra/src/extra/filters/roll/roll_right.rs index 55a1e42158..60a1777d54 100644 --- a/crates/nu-cmd-extra/src/extra/filters/roll/roll_right.rs +++ b/crates/nu-cmd-extra/src/extra/filters/roll/roll_right.rs @@ -1,5 +1,6 @@ use super::{horizontal_rotate_value, HorizontalDirection}; use nu_engine::command_prelude::*; +use nu_protocol::TryIntoValue; #[derive(Clone)] pub struct RollRight; @@ -94,7 +95,7 @@ impl Command for RollRight { let metadata = input.metadata(); let cells_only = call.has_flag(engine_state, stack, "cells-only")?; - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; let rotated_value = horizontal_rotate_value(value, by, cells_only, &HorizontalDirection::Right)?; diff --git a/crates/nu-cmd-extra/src/extra/filters/roll/roll_up.rs b/crates/nu-cmd-extra/src/extra/filters/roll/roll_up.rs index 7b9480599d..f3477776a3 100644 --- a/crates/nu-cmd-extra/src/extra/filters/roll/roll_up.rs +++ b/crates/nu-cmd-extra/src/extra/filters/roll/roll_up.rs @@ -1,5 +1,6 @@ use super::{vertical_rotate_value, VerticalDirection}; use nu_engine::command_prelude::*; +use nu_protocol::TryIntoValue; #[derive(Clone)] pub struct RollUp; @@ -56,7 +57,7 @@ impl Command for RollUp { let by: Option = call.get_flag(engine_state, stack, "by")?; let metadata = input.metadata(); - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; let rotated_value = vertical_rotate_value(value, by, VerticalDirection::Up)?; Ok(rotated_value.into_pipeline_data().set_metadata(metadata)) diff --git a/crates/nu-cmd-extra/src/extra/filters/update_cells.rs b/crates/nu-cmd-extra/src/extra/filters/update_cells.rs index c90e933410..184e39e20e 100644 --- a/crates/nu-cmd-extra/src/extra/filters/update_cells.rs +++ b/crates/nu-cmd-extra/src/extra/filters/update_cells.rs @@ -1,5 +1,5 @@ use nu_engine::{command_prelude::*, ClosureEval}; -use nu_protocol::{engine::Closure, PipelineIterator}; +use nu_protocol::{engine::Closure, PipelineIterator, TryIntoValue}; use std::collections::HashSet; #[derive(Clone)] @@ -152,7 +152,7 @@ impl Iterator for UpdateCellIterator { fn eval_value(closure: &mut ClosureEval, span: Span, value: Value) -> Value { closure .run_with_value(value) - .and_then(|data| data.into_value(span)) + .and_then(|data| data.try_into_value(span)) .unwrap_or_else(|err| Value::error(err, span)) } diff --git a/crates/nu-cmd-extra/src/extra/strings/format/command.rs b/crates/nu-cmd-extra/src/extra/strings/format/command.rs index 50e89e61e3..1ca7d41c4d 100644 --- a/crates/nu-cmd-extra/src/extra/strings/format/command.rs +++ b/crates/nu-cmd-extra/src/extra/strings/format/command.rs @@ -1,5 +1,5 @@ use nu_engine::command_prelude::*; -use nu_protocol::{ast::PathMember, engine::StateWorkingSet, ListStream}; +use nu_protocol::{ast::PathMember, engine::StateWorkingSet, ListStream, TryIntoValue}; #[derive(Clone)] pub struct FormatPattern; @@ -38,7 +38,7 @@ impl Command for FormatPattern { let mut working_set = StateWorkingSet::new(engine_state); let specified_pattern: Result = call.req(engine_state, stack, 0); - let input_val = input.into_value(call.head)?; + let input_val = input.try_into_value(call.head)?; // add '$it' variable to support format like this: $it.column1.column2. let it_id = working_set.add_variable(b"$it".to_vec(), call.head, Type::Any, false); stack.add_var(it_id, input_val.clone()); diff --git a/crates/nu-cmd-lang/src/core_commands/collect.rs b/crates/nu-cmd-lang/src/core_commands/collect.rs index 1c28646548..63a80dfe4e 100644 --- a/crates/nu-cmd-lang/src/core_commands/collect.rs +++ b/crates/nu-cmd-lang/src/core_commands/collect.rs @@ -1,5 +1,5 @@ use nu_engine::{command_prelude::*, get_eval_block, redirect_env}; -use nu_protocol::{engine::Closure, DataSource, PipelineMetadata}; +use nu_protocol::{engine::Closure, DataSource, PipelineMetadata, TryIntoValue}; #[derive(Clone)] pub struct Collect; @@ -54,7 +54,7 @@ is particularly large, this can cause high memory usage."# other => other, }; - let input = input.into_value(call.head)?; + let input = input.try_into_value(call.head)?; let result; if let Some(closure) = closure { diff --git a/crates/nu-cmd-lang/src/core_commands/describe.rs b/crates/nu-cmd-lang/src/core_commands/describe.rs index 3d992f3f33..22e1f17ea3 100644 --- a/crates/nu-cmd-lang/src/core_commands/describe.rs +++ b/crates/nu-cmd-lang/src/core_commands/describe.rs @@ -1,5 +1,5 @@ use nu_engine::command_prelude::*; -use nu_protocol::{engine::StateWorkingSet, ByteStreamSource, PipelineMetadata}; +use nu_protocol::{engine::StateWorkingSet, ByteStreamSource, IntoValue, PipelineMetadata}; #[derive(Clone)] pub struct Describe; @@ -195,7 +195,8 @@ fn run( let subtype = if options.no_collect { Value::string("any", head) } else { - describe_value(stream.into_value(), head, engine_state) + let span = stream.span(); + describe_value(stream.into_value(span), head, engine_state) }; Value::record( record! { @@ -209,7 +210,8 @@ fn run( } else if options.no_collect { Value::string("stream", head) } else { - let value = stream.into_value(); + let span = stream.span(); + let value = stream.into_value(span); let base_description = value.get_type().to_string(); Value::string(format!("{} (stream)", base_description), head) } @@ -232,7 +234,7 @@ enum Description { Record(Record), } -impl Description { +impl IntoValue for Description { fn into_value(self, span: Span) -> Value { match self { Description::String(ty) => Value::string(ty, span), diff --git a/crates/nu-cmd-lang/src/core_commands/for_.rs b/crates/nu-cmd-lang/src/core_commands/for_.rs index 0f2434d162..f5672062f9 100644 --- a/crates/nu-cmd-lang/src/core_commands/for_.rs +++ b/crates/nu-cmd-lang/src/core_commands/for_.rs @@ -1,6 +1,6 @@ use nu_engine::{command_prelude::*, get_eval_block, get_eval_expression}; use nu_protocol::engine::CommandType; -use nu_protocol::ParseWarning; +use nu_protocol::{ParseWarning, TryIntoValue}; #[derive(Clone)] pub struct For; @@ -193,7 +193,8 @@ impl Command for For { x => { stack.add_var(var_id, x); - eval_block(&engine_state, stack, block, PipelineData::empty())?.into_value(head)?; + eval_block(&engine_state, stack, block, PipelineData::empty())? + .try_into_value(head)?; } } Ok(PipelineData::empty()) diff --git a/crates/nu-cmd-lang/src/core_commands/let_.rs b/crates/nu-cmd-lang/src/core_commands/let_.rs index f2da628c31..bdeb9b1a23 100644 --- a/crates/nu-cmd-lang/src/core_commands/let_.rs +++ b/crates/nu-cmd-lang/src/core_commands/let_.rs @@ -1,5 +1,5 @@ use nu_engine::{command_prelude::*, get_eval_block}; -use nu_protocol::engine::CommandType; +use nu_protocol::{engine::CommandType, TryIntoValue}; #[derive(Clone)] pub struct Let; @@ -62,7 +62,7 @@ impl Command for Let { let eval_block = get_eval_block(engine_state); let stack = &mut stack.start_capture(); let pipeline_data = eval_block(engine_state, stack, block, input)?; - let value = pipeline_data.into_value(call.head)?; + let value = pipeline_data.try_into_value(call.head)?; // if given variable type is Glob, and our result is string // then nushell need to convert from Value::String to Value::Glob diff --git a/crates/nu-cmd-lang/src/core_commands/mut_.rs b/crates/nu-cmd-lang/src/core_commands/mut_.rs index 5db3c929af..14f760d662 100644 --- a/crates/nu-cmd-lang/src/core_commands/mut_.rs +++ b/crates/nu-cmd-lang/src/core_commands/mut_.rs @@ -1,5 +1,5 @@ use nu_engine::{command_prelude::*, get_eval_block}; -use nu_protocol::engine::CommandType; +use nu_protocol::{engine::CommandType, TryIntoValue}; #[derive(Clone)] pub struct Mut; @@ -62,7 +62,7 @@ impl Command for Mut { let eval_block = get_eval_block(engine_state); let stack = &mut stack.start_capture(); let pipeline_data = eval_block(engine_state, stack, block, input)?; - let value = pipeline_data.into_value(call.head)?; + let value = pipeline_data.try_into_value(call.head)?; // if given variable type is Glob, and our result is string // then nushell need to convert from Value::String to Value::Glob diff --git a/crates/nu-cmd-lang/src/example_support.rs b/crates/nu-cmd-lang/src/example_support.rs index bb03bbaf8c..508d4b34b6 100644 --- a/crates/nu-cmd-lang/src/example_support.rs +++ b/crates/nu-cmd-lang/src/example_support.rs @@ -4,7 +4,7 @@ use nu_protocol::{ ast::Block, debugger::WithoutDebug, engine::{StateDelta, StateWorkingSet}, - Range, + Range, TryIntoValue, }; use std::{ sync::Arc, @@ -123,7 +123,7 @@ pub fn eval_block( stack.add_env_var("PWD".to_string(), Value::test_string(cwd.to_string_lossy())); nu_engine::eval_block::(engine_state, &mut stack, &block, input) - .and_then(|data| data.into_value(Span::test_data())) + .and_then(|data| data.try_into_value(Span::test_data())) .unwrap_or_else(|err| panic!("test eval error in `{}`: {:?}", "TODO", err)) } diff --git a/crates/nu-color-config/src/style_computer.rs b/crates/nu-color-config/src/style_computer.rs index 91907c1428..8e718b93f8 100644 --- a/crates/nu-color-config/src/style_computer.rs +++ b/crates/nu-color-config/src/style_computer.rs @@ -4,7 +4,7 @@ use nu_engine::{env::get_config, ClosureEvalOnce}; use nu_protocol::{ cli_error::CliError, engine::{Closure, EngineState, Stack, StateWorkingSet}, - Span, Value, + Span, TryIntoValue, Value, }; use std::{ collections::HashMap, @@ -59,7 +59,7 @@ impl<'a> StyleComputer<'a> { let result = ClosureEvalOnce::new(self.engine_state, self.stack, closure.clone()) .debug(false) .run_with_value(value.clone()) - .and_then(|data| data.into_value(*span)); + .and_then(|data| data.try_into_value(*span)); match result { Ok(value) => { diff --git a/crates/nu-command/src/charting/hashable_value.rs b/crates/nu-command/src/charting/hashable_value.rs index 5b49a3fcd2..64ef1a380f 100644 --- a/crates/nu-command/src/charting/hashable_value.rs +++ b/crates/nu-command/src/charting/hashable_value.rs @@ -1,5 +1,5 @@ use chrono::{DateTime, FixedOffset}; -use nu_protocol::{ShellError, Span, Value}; +use nu_protocol::{IntoValue, ShellError, Span, Value}; use std::hash::{Hash, Hasher}; /// A subset of [`Value`](crate::Value), which is hashable. @@ -112,18 +112,19 @@ impl HashableValue { }), } } +} - /// Convert from self to nu's core data type `Value`. - pub fn into_value(self) -> Value { +impl IntoValue for HashableValue { + fn into_value(self, span: Span) -> Value { match self { - HashableValue::Bool { val, span } => Value::bool(val, span), - HashableValue::Int { val, span } => Value::int(val, span), - HashableValue::Filesize { val, span } => Value::filesize(val, span), - HashableValue::Duration { val, span } => Value::duration(val, span), - HashableValue::Date { val, span } => Value::date(val, span), - HashableValue::Float { val, span } => Value::float(f64::from_ne_bytes(val), span), - HashableValue::String { val, span } => Value::string(val, span), - HashableValue::Binary { val, span } => Value::binary(val, span), + HashableValue::Bool { val, .. } => Value::bool(val, span), + HashableValue::Int { val, .. } => Value::int(val, span), + HashableValue::Filesize { val, .. } => Value::filesize(val, span), + HashableValue::Duration { val, .. } => Value::duration(val, span), + HashableValue::Date { val, .. } => Value::date(val, span), + HashableValue::Float { val, .. } => Value::float(f64::from_ne_bytes(val), span), + HashableValue::String { val, .. } => Value::string(val, span), + HashableValue::Binary { val, .. } => Value::binary(val, span), } } } @@ -289,7 +290,7 @@ mod test { assert_eq!( HashableValue::from_value(val, Span::unknown()) .unwrap() - .into_value(), + .into_value(span), expected_val ); } diff --git a/crates/nu-command/src/charting/histogram.rs b/crates/nu-command/src/charting/histogram.rs index 29515c96c5..a31052ece0 100755 --- a/crates/nu-command/src/charting/histogram.rs +++ b/crates/nu-command/src/charting/histogram.rs @@ -1,6 +1,7 @@ use super::hashable_value::HashableValue; use itertools::Itertools; use nu_engine::command_prelude::*; +use nu_protocol::TryIntoValue; use std::collections::HashMap; @@ -121,7 +122,7 @@ impl Command for Histogram { }; let span = call.head; - let data_as_value = input.into_value(span)?; + let data_as_value = input.try_into_value(span)?; let value_span = data_as_value.span(); // `input` is not a list, here we can return an error. run_histogram( @@ -248,7 +249,7 @@ fn histogram_impl( count, // attach count first for easily sorting. Value::record( record! { - value_column_name => val.into_value(), + value_column_name => val.into_value(span), "count" => Value::int(count, span), "quantile" => Value::float(quantile, span), "percentage" => Value::string(percentage, span), diff --git a/crates/nu-command/src/conversions/into/record.rs b/crates/nu-command/src/conversions/into/record.rs index e867f06e15..126fc45e87 100644 --- a/crates/nu-command/src/conversions/into/record.rs +++ b/crates/nu-command/src/conversions/into/record.rs @@ -1,6 +1,6 @@ use chrono::{DateTime, Datelike, FixedOffset, Timelike}; use nu_engine::command_prelude::*; -use nu_protocol::format_duration_as_timeperiod; +use nu_protocol::{format_duration_as_timeperiod, TryIntoValue}; #[derive(Clone)] pub struct SubCommand; @@ -108,7 +108,7 @@ fn into_record( call: &Call, input: PipelineData, ) -> Result { - let input = input.into_value(call.head)?; + let input = input.try_into_value(call.head)?; let input_type = input.get_type(); let span = input.span(); let res = match input { diff --git a/crates/nu-command/src/database/values/sqlite.rs b/crates/nu-command/src/database/values/sqlite.rs index 483da7672e..53ca4ede21 100644 --- a/crates/nu-command/src/database/values/sqlite.rs +++ b/crates/nu-command/src/database/values/sqlite.rs @@ -2,7 +2,9 @@ use super::definitions::{ db_column::DbColumn, db_constraint::DbConstraint, db_foreignkey::DbForeignKey, db_index::DbIndex, db_table::DbTable, }; -use nu_protocol::{CustomValue, PipelineData, Record, ShellError, Span, Spanned, Value}; +use nu_protocol::{ + CustomValue, IntoValue, PipelineData, Record, ShellError, Span, Spanned, TryIntoValue, Value, +}; use rusqlite::{ types::ValueRef, Connection, DatabaseName, Error as SqliteError, OpenFlags, Row, Statement, ToSql, @@ -91,15 +93,10 @@ impl SQLiteDatabase { } pub fn try_from_pipeline(input: PipelineData, span: Span) -> Result { - let value = input.into_value(span)?; + let value = input.try_into_value(span)?; Self::try_from_value(value) } - pub fn into_value(self, span: Span) -> Value { - let db = Box::new(self); - Value::custom(db, span) - } - pub fn query( &self, sql: &Spanned, @@ -350,6 +347,13 @@ impl SQLiteDatabase { } } +impl IntoValue for SQLiteDatabase { + fn into_value(self, span: Span) -> Value { + let db = Box::new(self); + Value::custom(db, span) + } +} + impl CustomValue for SQLiteDatabase { fn clone_value(&self, span: Span) -> Value { let cloned = SQLiteDatabase { diff --git a/crates/nu-command/src/debug/inspect.rs b/crates/nu-command/src/debug/inspect.rs index ad6163c7b6..64a845d29d 100644 --- a/crates/nu-command/src/debug/inspect.rs +++ b/crates/nu-command/src/debug/inspect.rs @@ -1,5 +1,6 @@ use super::inspect_table; use nu_engine::command_prelude::*; +use nu_protocol::TryIntoValue; use terminal_size::{terminal_size, Height, Width}; #[derive(Clone)] @@ -29,7 +30,7 @@ impl Command for Inspect { input: PipelineData, ) -> Result { let input_metadata = input.metadata(); - let input_val = input.into_value(call.head)?; + let input_val = input.try_into_value(call.head)?; if input_val.is_nothing() { return Err(ShellError::PipelineEmpty { dst_span: call.head, diff --git a/crates/nu-command/src/debug/profile.rs b/crates/nu-command/src/debug/profile.rs index 0cef979094..cd836a641d 100644 --- a/crates/nu-command/src/debug/profile.rs +++ b/crates/nu-command/src/debug/profile.rs @@ -1,5 +1,5 @@ use nu_engine::{command_prelude::*, ClosureEvalOnce}; -use nu_protocol::{debugger::Profiler, engine::Closure}; +use nu_protocol::{debugger::Profiler, engine::Closure, TryIntoValue}; #[derive(Clone)] pub struct DebugProfile; @@ -125,7 +125,7 @@ confusing the id/parent_id hierarchy. The --expr flag is helpful for investigati let pipeline_data = result?; // Collect the output - let _ = pipeline_data.into_value(call.span()); + let _ = pipeline_data.try_into_value(call.span()); Ok(engine_state .deactivate_debugger() diff --git a/crates/nu-command/src/debug/timeit.rs b/crates/nu-command/src/debug/timeit.rs index a445679b81..2ae4178950 100644 --- a/crates/nu-command/src/debug/timeit.rs +++ b/crates/nu-command/src/debug/timeit.rs @@ -1,4 +1,5 @@ use nu_engine::{command_prelude::*, get_eval_block, get_eval_expression_with_input}; +use nu_protocol::TryIntoValue; use std::time::Instant; #[derive(Clone)] @@ -58,7 +59,7 @@ impl Command for TimeIt { } else { PipelineData::empty() } - .into_value(call.head)?; + .try_into_value(call.head)?; let end_time = Instant::now(); diff --git a/crates/nu-command/src/filesystem/save.rs b/crates/nu-command/src/filesystem/save.rs index ab5257fb01..a088bc267f 100644 --- a/crates/nu-command/src/filesystem/save.rs +++ b/crates/nu-command/src/filesystem/save.rs @@ -7,7 +7,7 @@ use nu_protocol::{ ast::{Expr, Expression}, byte_stream::copy_with_interrupt, process::ChildPipe, - ByteStreamSource, DataSource, OutDest, PipelineMetadata, + ByteStreamSource, DataSource, OutDest, PipelineMetadata, TryIntoValue, }; use std::{ fs::File, @@ -378,7 +378,7 @@ fn input_to_bytes( input }; - value_to_bytes(input.into_value(span)?) + value_to_bytes(input.try_into_value(span)?) } /// Convert given data into content of file of specified extension if diff --git a/crates/nu-command/src/filters/each.rs b/crates/nu-command/src/filters/each.rs index a074f63abb..7c98785bf1 100644 --- a/crates/nu-command/src/filters/each.rs +++ b/crates/nu-command/src/filters/each.rs @@ -129,7 +129,7 @@ with 'transpose' first."# } Some(Value::list(vals, span)) } - Ok(data) => Some(data.into_value(head).unwrap_or_else(|err| { + Ok(data) => Some(data.try_into_value(head).unwrap_or_else(|err| { Value::error(chain_error_with_input(err, is_error, span), span) })), Err(ShellError::Continue { span }) => Some(Value::nothing(span)), @@ -160,7 +160,7 @@ with 'transpose' first."# let is_error = value.is_error(); match closure .run_with_value(value) - .and_then(|data| data.into_value(head)) + .and_then(|data| data.try_into_value(head)) { Ok(value) => Some(value), Err(ShellError::Continue { span }) => Some(Value::nothing(span)), diff --git a/crates/nu-command/src/filters/filter.rs b/crates/nu-command/src/filters/filter.rs index 1ba1508839..fcfb8e804d 100644 --- a/crates/nu-command/src/filters/filter.rs +++ b/crates/nu-command/src/filters/filter.rs @@ -1,6 +1,6 @@ use super::utils::chain_error_with_input; use nu_engine::{command_prelude::*, ClosureEval, ClosureEvalOnce}; -use nu_protocol::engine::Closure; +use nu_protocol::{engine::Closure, TryIntoValue}; #[derive(Clone)] pub struct Filter; @@ -62,7 +62,7 @@ a variable. On the other hand, the "row condition" syntax is not supported."# .filter_map(move |value| { match closure .run_with_value(value.clone()) - .and_then(|data| data.into_value(head)) + .and_then(|data| data.try_into_value(head)) { Ok(cond) => cond.is_true().then_some(value), Err(err) => { @@ -87,7 +87,7 @@ a variable. On the other hand, the "row condition" syntax is not supported."# match closure .run_with_value(value.clone()) - .and_then(|data| data.into_value(head)) + .and_then(|data| data.try_into_value(head)) { Ok(cond) => cond.is_true().then_some(value), Err(err) => { @@ -107,7 +107,7 @@ a variable. On the other hand, the "row condition" syntax is not supported."# PipelineData::Value(value, ..) => { let result = ClosureEvalOnce::new(engine_state, stack, closure) .run_with_value(value.clone()) - .and_then(|data| data.into_value(head)); + .and_then(|data| data.try_into_value(head)); Ok(match result { Ok(cond) => cond.is_true().then_some(value), diff --git a/crates/nu-command/src/filters/get.rs b/crates/nu-command/src/filters/get.rs index 07f0ea9440..b3d833d649 100644 --- a/crates/nu-command/src/filters/get.rs +++ b/crates/nu-command/src/filters/get.rs @@ -81,7 +81,7 @@ If multiple cell paths are given, this will produce a list of values."# let paths = std::iter::once(cell_path).chain(rest); - let input = input.into_value(span)?; + let input = input.try_into_value(span)?; for path in paths { let val = input.clone().follow_cell_path(&path.members, !sensitive); diff --git a/crates/nu-command/src/filters/group_by.rs b/crates/nu-command/src/filters/group_by.rs index c1d76ebe08..3e5ad39915 100644 --- a/crates/nu-command/src/filters/group_by.rs +++ b/crates/nu-command/src/filters/group_by.rs @@ -1,6 +1,6 @@ use indexmap::IndexMap; use nu_engine::{command_prelude::*, ClosureEval}; -use nu_protocol::engine::Closure; +use nu_protocol::{engine::Closure, TryIntoValue}; #[derive(Clone)] pub struct GroupBy; @@ -207,7 +207,7 @@ fn group_closure( for value in values { let key = closure .run_with_value(value.clone())? - .into_value(span)? + .try_into_value(span)? .coerce_into_string()?; groups.entry(key).or_default().push(value); diff --git a/crates/nu-command/src/filters/headers.rs b/crates/nu-command/src/filters/headers.rs index 6e63c33ff9..a1c0945b4f 100644 --- a/crates/nu-command/src/filters/headers.rs +++ b/crates/nu-command/src/filters/headers.rs @@ -66,7 +66,7 @@ impl Command for Headers { let config = engine_state.get_config(); let metadata = input.metadata(); let span = input.span().unwrap_or(call.head); - let value = input.into_value(span)?; + let value = input.try_into_value(span)?; let Value::List { vals: table, .. } = value else { return Err(ShellError::TypeMismatch { err_message: "not a table".to_string(), diff --git a/crates/nu-command/src/filters/insert.rs b/crates/nu-command/src/filters/insert.rs index 5f1380b2ac..cebd3758ad 100644 --- a/crates/nu-command/src/filters/insert.rs +++ b/crates/nu-command/src/filters/insert.rs @@ -190,7 +190,7 @@ fn insert( let value = value.unwrap_or(Value::nothing(head)); let new_value = ClosureEvalOnce::new(engine_state, stack, *val) .run_with_value(value.clone())? - .into_value(head)?; + .try_into_value(head)?; pre_elems.push(new_value); if !end_of_stream { @@ -284,7 +284,9 @@ fn insert_value_by_closure( value.clone() }; - let new_value = closure.run_with_value(value_at_path)?.into_value(span)?; + let new_value = closure + .run_with_value(value_at_path)? + .try_into_value(span)?; value.insert_data_at_cell_path(cell_path, new_value, span) } @@ -304,7 +306,9 @@ fn insert_single_value_by_closure( value.clone() }; - let new_value = closure.run_with_value(value_at_path)?.into_value(span)?; + let new_value = closure + .run_with_value(value_at_path)? + .try_into_value(span)?; value.insert_data_at_cell_path(cell_path, new_value, span) } diff --git a/crates/nu-command/src/filters/items.rs b/crates/nu-command/src/filters/items.rs index ed30486bee..9666d76332 100644 --- a/crates/nu-command/src/filters/items.rs +++ b/crates/nu-command/src/filters/items.rs @@ -56,7 +56,7 @@ impl Command for Items { .add_arg(Value::string(col, span)) .add_arg(val) .run_with_input(PipelineData::Empty) - .and_then(|data| data.into_value(head)); + .and_then(|data| data.try_into_value(head)); match result { Ok(value) => Some(value), diff --git a/crates/nu-command/src/filters/join.rs b/crates/nu-command/src/filters/join.rs index f5e6d63deb..64d052dd54 100644 --- a/crates/nu-command/src/filters/join.rs +++ b/crates/nu-command/src/filters/join.rs @@ -75,7 +75,7 @@ impl Command for Join { let join_type = join_type(engine_state, stack, call)?; // FIXME: we should handle ListStreams properly instead of collecting - let collected_input = input.into_value(span)?; + let collected_input = input.try_into_value(span)?; match (&collected_input, &table_2, &l_on, &r_on) { ( diff --git a/crates/nu-command/src/filters/par_each.rs b/crates/nu-command/src/filters/par_each.rs index af72895df6..b4e4bb73a5 100644 --- a/crates/nu-command/src/filters/par_each.rs +++ b/crates/nu-command/src/filters/par_each.rs @@ -1,6 +1,6 @@ use super::utils::chain_error_with_input; use nu_engine::{command_prelude::*, ClosureEvalOnce}; -use nu_protocol::engine::Closure; +use nu_protocol::{engine::Closure, TryIntoValue}; use rayon::prelude::*; #[derive(Clone)] @@ -146,7 +146,7 @@ impl Command for ParEach { let value = ClosureEvalOnce::new(engine_state, stack, closure.clone()) .run_with_value(value) - .and_then(|data| data.into_value(span)) + .and_then(|data| data.try_into_value(span)) .unwrap_or_else(|err| { Value::error( chain_error_with_input(err, is_error, span), @@ -172,7 +172,7 @@ impl Command for ParEach { let value = ClosureEvalOnce::new(engine_state, stack, closure.clone()) .run_with_value(value) - .and_then(|data| data.into_value(span)) + .and_then(|data| data.try_into_value(span)) .unwrap_or_else(|err| { Value::error( chain_error_with_input(err, is_error, span), @@ -203,7 +203,7 @@ impl Command for ParEach { let is_error = value.is_error(); let value = ClosureEvalOnce::new(engine_state, stack, closure.clone()) .run_with_value(value) - .and_then(|data| data.into_value(head)) + .and_then(|data| data.try_into_value(head)) .unwrap_or_else(|err| { Value::error(chain_error_with_input(err, is_error, span), span) }); @@ -229,7 +229,7 @@ impl Command for ParEach { let value = ClosureEvalOnce::new(engine_state, stack, closure.clone()) .run_with_value(value) - .and_then(|data| data.into_value(head)) + .and_then(|data| data.try_into_value(head)) .unwrap_or_else(|err| Value::error(err, head)); (index, value) diff --git a/crates/nu-command/src/filters/reduce.rs b/crates/nu-command/src/filters/reduce.rs index fc808ca9af..c9928cc6c8 100644 --- a/crates/nu-command/src/filters/reduce.rs +++ b/crates/nu-command/src/filters/reduce.rs @@ -115,7 +115,7 @@ impl Command for Reduce { .add_arg(value) .add_arg(acc) .run_with_input(PipelineData::Empty)? - .into_value(head)?; + .try_into_value(head)?; } Ok(acc.with_span(head).into_pipeline_data()) diff --git a/crates/nu-command/src/filters/reject.rs b/crates/nu-command/src/filters/reject.rs index f8583d3f47..38917f501e 100644 --- a/crates/nu-command/src/filters/reject.rs +++ b/crates/nu-command/src/filters/reject.rs @@ -173,7 +173,7 @@ fn reject( ) -> Result { let mut unique_rows: HashSet = HashSet::new(); let metadata = input.metadata(); - let val = input.into_value(span)?; + let val = input.try_into_value(span)?; let mut val = val; let mut new_columns = vec![]; let mut new_rows = vec![]; diff --git a/crates/nu-command/src/filters/skip/skip_until.rs b/crates/nu-command/src/filters/skip/skip_until.rs index bb36785e00..edf420b79d 100644 --- a/crates/nu-command/src/filters/skip/skip_until.rs +++ b/crates/nu-command/src/filters/skip/skip_until.rs @@ -85,7 +85,7 @@ impl Command for SkipUntil { .skip_while(move |value| { closure .run_with_value(value.clone()) - .and_then(|data| data.into_value(head)) + .and_then(|data| data.try_into_value(head)) .map(|cond| cond.is_false()) .unwrap_or(false) }) diff --git a/crates/nu-command/src/filters/skip/skip_while.rs b/crates/nu-command/src/filters/skip/skip_while.rs index 2747ea6f97..077f9d868b 100644 --- a/crates/nu-command/src/filters/skip/skip_while.rs +++ b/crates/nu-command/src/filters/skip/skip_while.rs @@ -90,7 +90,7 @@ impl Command for SkipWhile { .skip_while(move |value| { closure .run_with_value(value.clone()) - .and_then(|data| data.into_value(head)) + .and_then(|data| data.try_into_value(head)) .map(|cond| cond.is_true()) .unwrap_or(false) }) diff --git a/crates/nu-command/src/filters/take/take_until.rs b/crates/nu-command/src/filters/take/take_until.rs index 0df2407cb1..7b7f657776 100644 --- a/crates/nu-command/src/filters/take/take_until.rs +++ b/crates/nu-command/src/filters/take/take_until.rs @@ -1,5 +1,5 @@ use nu_engine::{command_prelude::*, ClosureEval}; -use nu_protocol::engine::Closure; +use nu_protocol::{engine::Closure, TryIntoValue}; #[derive(Clone)] pub struct TakeUntil; @@ -81,7 +81,7 @@ impl Command for TakeUntil { .take_while(move |value| { closure .run_with_value(value.clone()) - .and_then(|data| data.into_value(head)) + .and_then(|data| data.try_into_value(head)) .map(|cond| cond.is_false()) .unwrap_or(false) }) diff --git a/crates/nu-command/src/filters/take/take_while.rs b/crates/nu-command/src/filters/take/take_while.rs index 7c282ac38a..4000c6ec9b 100644 --- a/crates/nu-command/src/filters/take/take_while.rs +++ b/crates/nu-command/src/filters/take/take_while.rs @@ -81,7 +81,7 @@ impl Command for TakeWhile { .take_while(move |value| { closure .run_with_value(value.clone()) - .and_then(|data| data.into_value(head)) + .and_then(|data| data.try_into_value(head)) .map(|cond| cond.is_true()) .unwrap_or(false) }) diff --git a/crates/nu-command/src/filters/update.rs b/crates/nu-command/src/filters/update.rs index e724ae77ad..f5ef4cf8d4 100644 --- a/crates/nu-command/src/filters/update.rs +++ b/crates/nu-command/src/filters/update.rs @@ -1,5 +1,5 @@ use nu_engine::{command_prelude::*, ClosureEval, ClosureEvalOnce}; -use nu_protocol::ast::PathMember; +use nu_protocol::{ast::PathMember, TryIntoValue}; #[derive(Clone)] pub struct Update; @@ -250,7 +250,7 @@ fn update_value_by_closure( let new_value = closure .add_arg(arg.clone()) .run_with_input(value_at_path.into_pipeline_data())? - .into_value(span)?; + .try_into_value(span)?; value.update_data_at_cell_path(cell_path, new_value) } @@ -273,7 +273,7 @@ fn update_single_value_by_closure( let new_value = closure .add_arg(arg.clone()) .run_with_input(value_at_path.into_pipeline_data())? - .into_value(span)?; + .try_into_value(span)?; value.update_data_at_cell_path(cell_path, new_value) } diff --git a/crates/nu-command/src/filters/upsert.rs b/crates/nu-command/src/filters/upsert.rs index e3678972fb..d833826880 100644 --- a/crates/nu-command/src/filters/upsert.rs +++ b/crates/nu-command/src/filters/upsert.rs @@ -1,5 +1,5 @@ use nu_engine::{command_prelude::*, ClosureEval, ClosureEvalOnce}; -use nu_protocol::ast::PathMember; +use nu_protocol::{ast::PathMember, TryIntoValue}; #[derive(Clone)] pub struct Upsert; @@ -218,7 +218,7 @@ fn upsert( if let Value::Closure { val, .. } = replacement { ClosureEvalOnce::new(engine_state, stack, *val) .run_with_value(value)? - .into_value(head)? + .try_into_value(head)? } else { replacement } @@ -314,7 +314,7 @@ fn upsert_value_by_closure( let new_value = closure .add_arg(arg) .run_with_input(input)? - .into_value(span)?; + .try_into_value(span)?; value.upsert_data_at_cell_path(cell_path, new_value) } @@ -341,7 +341,7 @@ fn upsert_single_value_by_closure( let new_value = closure .add_arg(arg) .run_with_input(input)? - .into_value(span)?; + .try_into_value(span)?; value.upsert_data_at_cell_path(cell_path, new_value) } diff --git a/crates/nu-command/src/filters/utils.rs b/crates/nu-command/src/filters/utils.rs index 8d9b1300f6..455c0afdf0 100644 --- a/crates/nu-command/src/filters/utils.rs +++ b/crates/nu-command/src/filters/utils.rs @@ -2,7 +2,7 @@ use nu_engine::{CallExt, ClosureEval}; use nu_protocol::{ ast::Call, engine::{Closure, EngineState, Stack}, - IntoPipelineData, PipelineData, ShellError, Span, Value, + IntoPipelineData, PipelineData, ShellError, Span, TryIntoValue, Value, }; pub fn chain_error_with_input( @@ -36,7 +36,10 @@ pub fn boolean_fold( break; } - let pred = closure.run_with_value(value)?.into_value(head)?.is_true(); + let pred = closure + .run_with_value(value)? + .try_into_value(head)? + .is_true(); if pred == accumulator { return Ok(Value::bool(accumulator, head).into_pipeline_data()); diff --git a/crates/nu-command/src/filters/where_.rs b/crates/nu-command/src/filters/where_.rs index fe73de354f..703a5c0130 100644 --- a/crates/nu-command/src/filters/where_.rs +++ b/crates/nu-command/src/filters/where_.rs @@ -1,5 +1,5 @@ use nu_engine::{command_prelude::*, ClosureEval}; -use nu_protocol::engine::Closure; +use nu_protocol::{engine::Closure, TryIntoValue}; #[derive(Clone)] pub struct Where; @@ -60,7 +60,7 @@ not supported."# .filter_map(move |value| { match closure .run_with_value(value.clone()) - .and_then(|data| data.into_value(head)) + .and_then(|data| data.try_into_value(head)) { Ok(cond) => cond.is_true().then_some(value), Err(err) => Some(Value::error(err, head)), diff --git a/crates/nu-command/src/filters/wrap.rs b/crates/nu-command/src/filters/wrap.rs index 52a0fb22c3..d86b657260 100644 --- a/crates/nu-command/src/filters/wrap.rs +++ b/crates/nu-command/src/filters/wrap.rs @@ -1,4 +1,5 @@ use nu_engine::command_prelude::*; +use nu_protocol::TryIntoValue; #[derive(Clone)] pub struct Wrap; @@ -44,7 +45,7 @@ impl Command for Wrap { .map(move |x| Value::record(record! { name.clone() => x }, span)) .into_pipeline_data_with_metadata(span, engine_state.ctrlc.clone(), metadata)), PipelineData::ByteStream(stream, ..) => Ok(Value::record( - record! { name => stream.into_value()? }, + record! { name => stream.try_into_value(span)? }, span, ) .into_pipeline_data_with_metadata(metadata)), diff --git a/crates/nu-command/src/formats/to/delimited.rs b/crates/nu-command/src/formats/to/delimited.rs index a7a2480a34..3284caef5d 100644 --- a/crates/nu-command/src/formats/to/delimited.rs +++ b/crates/nu-command/src/formats/to/delimited.rs @@ -1,7 +1,8 @@ use csv::WriterBuilder; use nu_cmd_base::formats::to::delimited::merge_descriptors; use nu_protocol::{ - ByteStream, ByteStreamType, Config, PipelineData, ShellError, Span, Spanned, Value, + ByteStream, ByteStreamType, Config, PipelineData, ShellError, Span, Spanned, TryIntoValue, + Value, }; use std::{iter, sync::Arc}; @@ -110,7 +111,7 @@ pub fn to_delimited_data( None => { // The columns were not provided. We need to detect them, and in order to do so, we have // to convert the input into a value first, so that we can find all of them - let value = input.into_value(span)?; + let value = input.try_into_value(span)?; let columns = match &value { Value::List { vals, .. } => merge_descriptors(vals), Value::Record { val, .. } => val.columns().cloned().collect(), diff --git a/crates/nu-command/src/formats/to/json.rs b/crates/nu-command/src/formats/to/json.rs index c4c87f804f..60894c3dc1 100644 --- a/crates/nu-command/src/formats/to/json.rs +++ b/crates/nu-command/src/formats/to/json.rs @@ -1,5 +1,5 @@ use nu_engine::command_prelude::*; -use nu_protocol::ast::PathMember; +use nu_protocol::{ast::PathMember, TryIntoValue}; #[derive(Clone)] pub struct ToJson; @@ -46,7 +46,7 @@ impl Command for ToJson { let span = call.head; // allow ranges to expand and turn into array let input = input.try_expand_range()?; - let value = input.into_value(span)?; + let value = input.try_into_value(span)?; let json_value = value_to_json_value(&value)?; let json_result = if raw { diff --git a/crates/nu-command/src/formats/to/msgpack.rs b/crates/nu-command/src/formats/to/msgpack.rs index bfeb428e3e..f4327c5239 100644 --- a/crates/nu-command/src/formats/to/msgpack.rs +++ b/crates/nu-command/src/formats/to/msgpack.rs @@ -5,7 +5,7 @@ use std::io; use byteorder::{BigEndian, WriteBytesExt}; use nu_engine::command_prelude::*; -use nu_protocol::{ast::PathMember, Spanned}; +use nu_protocol::{ast::PathMember, Spanned, TryIntoValue}; use rmp::encode as mp; /// Max recursion depth @@ -75,7 +75,7 @@ MessagePack: https://msgpack.org/ input: PipelineData, ) -> Result { let value_span = input.span().unwrap_or(call.head); - let value = input.into_value(value_span)?; + let value = input.try_into_value(value_span)?; let mut out = vec![]; write_value(&mut out, &value, 0)?; diff --git a/crates/nu-command/src/formats/to/msgpackz.rs b/crates/nu-command/src/formats/to/msgpackz.rs index 83d812cab6..ee6087102b 100644 --- a/crates/nu-command/src/formats/to/msgpackz.rs +++ b/crates/nu-command/src/formats/to/msgpackz.rs @@ -1,6 +1,7 @@ use std::io::Write; use nu_engine::command_prelude::*; +use nu_protocol::TryIntoValue; use super::msgpack::write_value; @@ -70,7 +71,7 @@ impl Command for ToMsgpackz { .transpose()?; let value_span = input.span().unwrap_or(call.head); - let value = input.into_value(value_span)?; + let value = input.try_into_value(value_span)?; let mut out_buf = vec![]; let mut out = brotli::CompressorWriter::new( &mut out_buf, diff --git a/crates/nu-command/src/formats/to/nuon.rs b/crates/nu-command/src/formats/to/nuon.rs index f40b7b5c1d..bf02402836 100644 --- a/crates/nu-command/src/formats/to/nuon.rs +++ b/crates/nu-command/src/formats/to/nuon.rs @@ -1,4 +1,5 @@ use nu_engine::command_prelude::*; +use nu_protocol::TryIntoValue; #[derive(Clone)] pub struct ToNuon; @@ -53,7 +54,7 @@ impl Command for ToNuon { }; let span = call.head; - let value = input.into_value(span)?; + let value = input.try_into_value(span)?; match nuon::to_nuon(&value, style, Some(span)) { Ok(serde_nuon_string) => { diff --git a/crates/nu-command/src/formats/to/toml.rs b/crates/nu-command/src/formats/to/toml.rs index ed1490231c..5332897d79 100644 --- a/crates/nu-command/src/formats/to/toml.rs +++ b/crates/nu-command/src/formats/to/toml.rs @@ -1,6 +1,6 @@ use chrono::{DateTime, Datelike, FixedOffset, Timelike}; use nu_engine::command_prelude::*; -use nu_protocol::ast::PathMember; +use nu_protocol::{ast::PathMember, TryIntoValue}; #[derive(Clone)] pub struct ToToml; @@ -139,7 +139,7 @@ fn to_toml( input: PipelineData, span: Span, ) -> Result { - let value = input.into_value(span)?; + let value = input.try_into_value(span)?; let toml_value = value_to_toml_value(engine_state, &value, span)?; match toml_value { diff --git a/crates/nu-command/src/formats/to/xml.rs b/crates/nu-command/src/formats/to/xml.rs index 648094318b..7b11f97b96 100644 --- a/crates/nu-command/src/formats/to/xml.rs +++ b/crates/nu-command/src/formats/to/xml.rs @@ -2,6 +2,7 @@ use crate::formats::nu_xml_format::{COLUMN_ATTRS_NAME, COLUMN_CONTENT_NAME, COLU use indexmap::IndexMap; use nu_engine::command_prelude::*; +use nu_protocol::TryIntoValue; use quick_xml::{ escape, events::{BytesEnd, BytesStart, BytesText, Event}, @@ -132,7 +133,7 @@ impl Job { } fn run(mut self, input: PipelineData, head: Span) -> Result { - let value = input.into_value(head)?; + let value = input.try_into_value(head)?; self.write_xml_entry(value, true).and_then(|_| { let b = self.writer.into_inner().into_inner(); diff --git a/crates/nu-command/src/formats/to/yaml.rs b/crates/nu-command/src/formats/to/yaml.rs index bea2dd3381..e5124d2852 100644 --- a/crates/nu-command/src/formats/to/yaml.rs +++ b/crates/nu-command/src/formats/to/yaml.rs @@ -1,5 +1,5 @@ use nu_engine::command_prelude::*; -use nu_protocol::ast::PathMember; +use nu_protocol::{ast::PathMember, TryIntoValue}; #[derive(Clone)] pub struct ToYaml; @@ -95,7 +95,7 @@ pub fn value_to_yaml_value(v: &Value) -> Result { } fn to_yaml(input: PipelineData, head: Span) -> Result { - let value = input.into_value(head)?; + let value = input.try_into_value(head)?; let yaml_value = value_to_yaml_value(&value)?; match serde_yaml::to_string(&yaml_value) { diff --git a/crates/nu-command/src/generators/generate.rs b/crates/nu-command/src/generators/generate.rs index 3549667ff0..f97f7eb882 100644 --- a/crates/nu-command/src/generators/generate.rs +++ b/crates/nu-command/src/generators/generate.rs @@ -1,6 +1,6 @@ use itertools::unfold; use nu_engine::{command_prelude::*, ClosureEval}; -use nu_protocol::engine::Closure; +use nu_protocol::{engine::Closure, TryIntoValue}; #[derive(Clone)] pub struct Generate; @@ -159,7 +159,7 @@ used as the next argument to the closure, otherwise generation stops. Ok(other) => { let error = other - .into_value(head) + .try_into_value(head) .map(|val| ShellError::GenericError { error: "Invalid block return".into(), msg: format!("Expected record, found {}", val.get_type()), diff --git a/crates/nu-command/src/misc/tutor.rs b/crates/nu-command/src/misc/tutor.rs index 6b1c43534b..06a262d85c 100644 --- a/crates/nu-command/src/misc/tutor.rs +++ b/crates/nu-command/src/misc/tutor.rs @@ -1,5 +1,6 @@ use itertools::Itertools; use nu_engine::command_prelude::*; +use nu_protocol::TryIntoValue; #[derive(Clone)] pub struct Tutor; @@ -416,7 +417,7 @@ fn display(help: &str, engine_state: &EngineState, stack: &mut Stack, span: Span Value::string(item, Span::unknown()).into_pipeline_data(), ); - if let Ok(value) = result.and_then(|data| data.into_value(Span::unknown())) { + if let Ok(value) = result.and_then(|data| data.try_into_value(Span::unknown())) { match value.coerce_into_string() { Ok(s) => { build.push_str(&s); diff --git a/crates/nu-command/src/network/http/client.rs b/crates/nu-command/src/network/http/client.rs index 8317fb50bc..899796e04d 100644 --- a/crates/nu-command/src/network/http/client.rs +++ b/crates/nu-command/src/network/http/client.rs @@ -5,7 +5,7 @@ use base64::{ Engine, }; use nu_engine::command_prelude::*; -use nu_protocol::ByteStream; +use nu_protocol::{ByteStream, TryIntoValue}; use std::{ collections::HashMap, path::PathBuf, @@ -529,11 +529,11 @@ fn request_handle_response_content( let response_status = resp.status(); let request_headers_value = headers_to_nu(&extract_request_headers(&request), span) - .and_then(|data| data.into_value(span)) + .and_then(|data| data.try_into_value(span)) .unwrap_or(Value::nothing(span)); let response_headers_value = headers_to_nu(&extract_response_headers(&resp), span) - .and_then(|data| data.into_value(span)) + .and_then(|data| data.try_into_value(span)) .unwrap_or(Value::nothing(span)); let headers = record! { @@ -541,7 +541,7 @@ fn request_handle_response_content( "response" => response_headers_value, }; - let body = consume_response_body(resp)?.into_value(span)?; + let body = consume_response_body(resp)?.try_into_value(span)?; let full_response = Value::record( record! { diff --git a/crates/nu-command/src/network/url/parse.rs b/crates/nu-command/src/network/url/parse.rs index e71c8d472a..9ca799fca2 100644 --- a/crates/nu-command/src/network/url/parse.rs +++ b/crates/nu-command/src/network/url/parse.rs @@ -1,4 +1,5 @@ use nu_engine::command_prelude::*; +use nu_protocol::TryIntoValue; use url::Url; #[derive(Clone)] @@ -42,7 +43,7 @@ impl Command for SubCommand { call: &Call, input: PipelineData, ) -> Result { - parse(input.into_value(call.head)?, call.head, engine_state) + parse(input.try_into_value(call.head)?, call.head, engine_state) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/path/join.rs b/crates/nu-command/src/path/join.rs index 19d65b4c46..349e96fb56 100644 --- a/crates/nu-command/src/path/join.rs +++ b/crates/nu-command/src/path/join.rs @@ -1,6 +1,6 @@ use super::PathSubcommandArguments; use nu_engine::command_prelude::*; -use nu_protocol::engine::StateWorkingSet; +use nu_protocol::{engine::StateWorkingSet, IntoValue}; use std::path::{Path, PathBuf}; struct Arguments { @@ -171,10 +171,13 @@ fn run(call: &Call, args: &Arguments, input: PipelineData) -> Result Ok(PipelineData::Value(handle_value(val, args, head), md)), - PipelineData::ListStream(stream, ..) => Ok(PipelineData::Value( - handle_value(stream.into_value(), args, head), - metadata, - )), + PipelineData::ListStream(stream, ..) => { + let span = stream.span(); + Ok(PipelineData::Value( + handle_value(stream.into_value(span), args, head), + metadata, + )) + } PipelineData::Empty { .. } => Err(ShellError::PipelineEmpty { dst_span: head }), _ => Err(ShellError::UnsupportedInput { msg: "Input value cannot be joined".to_string(), diff --git a/crates/nu-engine/src/command_prelude.rs b/crates/nu-engine/src/command_prelude.rs index 5c21af27e0..1f0e449708 100644 --- a/crates/nu-engine/src/command_prelude.rs +++ b/crates/nu-engine/src/command_prelude.rs @@ -3,6 +3,6 @@ pub use nu_protocol::{ ast::{Call, CellPath}, engine::{Command, EngineState, Stack, StateWorkingSet}, record, ByteStream, ByteStreamType, Category, ErrSpan, Example, IntoInterruptiblePipelineData, - IntoPipelineData, IntoSpanned, PipelineData, Record, ShellError, Signature, Span, Spanned, - SyntaxShape, Type, Value, + IntoPipelineData, IntoSpanned, IntoValue, PipelineData, Record, ShellError, Signature, Span, + Spanned, SyntaxShape, TryIntoValue, Type, Value, }; diff --git a/crates/nu-engine/src/documentation.rs b/crates/nu-engine/src/documentation.rs index 94cef3298e..9c383ee957 100644 --- a/crates/nu-engine/src/documentation.rs +++ b/crates/nu-engine/src/documentation.rs @@ -4,7 +4,7 @@ use nu_protocol::{ debugger::WithoutDebug, engine::{Command, EngineState, Stack, UNKNOWN_SPAN_ID}, record, Category, Example, IntoPipelineData, PipelineData, Signature, Span, SpanId, - SyntaxShape, Type, Value, + SyntaxShape, TryIntoValue, Type, Value, }; use std::{collections::HashMap, fmt::Write}; @@ -51,7 +51,7 @@ fn nu_highlight_string(code_string: &str, engine_state: &EngineState, stack: &mu &Call::new(Span::unknown()), Value::string(code_string, Span::unknown()).into_pipeline_data(), ) { - let result = output.into_value(Span::unknown()); + let result = output.try_into_value(Span::unknown()); if let Ok(s) = result.and_then(Value::coerce_into_string) { return s; // successfully highlighted string } @@ -277,7 +277,7 @@ fn get_documentation( Value::string(example.example, Span::unknown()).into_pipeline_data(), ) { Ok(output) => { - let result = output.into_value(Span::unknown()); + let result = output.try_into_value(Span::unknown()); match result.and_then(Value::coerce_into_string) { Ok(s) => { let _ = write!(long_desc, "\n > {s}\n"); diff --git a/crates/nu-engine/src/env.rs b/crates/nu-engine/src/env.rs index 048d9bfb99..2e5965989e 100644 --- a/crates/nu-engine/src/env.rs +++ b/crates/nu-engine/src/env.rs @@ -3,7 +3,7 @@ use nu_path::canonicalize_with; use nu_protocol::{ ast::{Call, Expr}, engine::{EngineState, Stack, StateWorkingSet}, - Config, ShellError, Span, Value, VarId, + Config, ShellError, Span, TryIntoValue, Value, VarId, }; use std::{ collections::HashMap, @@ -357,7 +357,7 @@ fn get_converted_value( .debug(false) .run_with_value(orig_val.clone()) }) - .and_then(|data| data.into_value(orig_val.span())) + .and_then(|data| data.try_into_value(orig_val.span())) .map_or_else(ConversionResult::ConversionError, ConversionResult::Ok) } else { ConversionResult::CellPathError diff --git a/crates/nu-engine/src/eval.rs b/crates/nu-engine/src/eval.rs index 044bf86980..0fec628d42 100644 --- a/crates/nu-engine/src/eval.rs +++ b/crates/nu-engine/src/eval.rs @@ -10,7 +10,7 @@ use nu_protocol::{ engine::{Closure, EngineState, Redirection, Stack}, eval_base::Eval, ByteStreamSource, Config, FromValue, IntoPipelineData, OutDest, PipelineData, ShellError, Span, - Spanned, Type, Value, VarId, ENV_VARIABLE_ID, + Spanned, TryIntoValue, Type, Value, VarId, ENV_VARIABLE_ID, }; use nu_utils::IgnoreCaseExt; use std::{borrow::Cow, fs::OpenOptions, path::PathBuf}; @@ -275,7 +275,7 @@ pub fn eval_expression_with_input( let stack = &mut stack.start_capture(); // FIXME: protect this collect with ctrl-c input = eval_subexpression::(engine_state, stack, block, input)? - .into_value(*span)? + .try_into_value(*span)? .follow_cell_path(&full_cell_path.tail, false)? .into_pipeline_data() } else { @@ -704,7 +704,7 @@ impl Eval for EvalRuntime { _: Span, ) -> Result { // FIXME: protect this collect with ctrl-c - eval_call::(engine_state, stack, call, PipelineData::empty())?.into_value(call.head) + eval_call::(engine_state, stack, call, PipelineData::empty())?.try_into_value(call.head) } fn eval_external_call( @@ -716,7 +716,7 @@ impl Eval for EvalRuntime { ) -> Result { let span = head.span(&engine_state); // FIXME: protect this collect with ctrl-c - eval_external(engine_state, stack, head, args, PipelineData::empty())?.into_value(span) + eval_external(engine_state, stack, head, args, PipelineData::empty())?.try_into_value(span) } fn eval_subexpression( @@ -727,7 +727,8 @@ impl Eval for EvalRuntime { ) -> Result { let block = engine_state.get_block(block_id); // FIXME: protect this collect with ctrl-c - eval_subexpression::(engine_state, stack, block, PipelineData::empty())?.into_value(span) + eval_subexpression::(engine_state, stack, block, PipelineData::empty())? + .try_into_value(span) } fn regex_match( diff --git a/crates/nu-explore/src/nu_common/value.rs b/crates/nu-explore/src/nu_common/value.rs index ae90278358..73a4b8c081 100644 --- a/crates/nu-explore/src/nu_common/value.rs +++ b/crates/nu-explore/src/nu_common/value.rs @@ -1,7 +1,9 @@ use super::NuSpan; use anyhow::Result; use nu_engine::get_columns; -use nu_protocol::{record, ByteStream, ListStream, PipelineData, PipelineMetadata, Value}; +use nu_protocol::{ + record, ByteStream, ListStream, PipelineData, PipelineMetadata, TryIntoValue, Value, +}; use std::collections::HashMap; pub fn collect_pipeline(input: PipelineData) -> Result<(Vec, Vec>)> { @@ -63,8 +65,9 @@ fn collect_byte_stream( } }, Err(stream) => { + let span = stream.span(); let value = stream - .into_value() + .try_into_value(span) .unwrap_or_else(|err| Value::error(err, span)); columns.push("".into()); diff --git a/crates/nu-plugin-engine/src/context.rs b/crates/nu-plugin-engine/src/context.rs index d5be6ad4b6..92844b4717 100644 --- a/crates/nu-plugin-engine/src/context.rs +++ b/crates/nu-plugin-engine/src/context.rs @@ -3,7 +3,8 @@ use nu_engine::{get_eval_block_with_early_return, get_full_help, ClosureEvalOnce use nu_protocol::{ ast::Call, engine::{Closure, EngineState, Redirection, Stack}, - Config, IntoSpanned, OutDest, PipelineData, PluginIdentity, ShellError, Span, Spanned, Value, + Config, IntoSpanned, OutDest, PipelineData, PluginIdentity, ShellError, Span, Spanned, + TryIntoValue, Value, }; use std::{ borrow::Cow, @@ -108,7 +109,7 @@ impl<'a> PluginExecutionContext for PluginExecutionCommandContext<'a> { Value::Closure { val, .. } => { ClosureEvalOnce::new(&self.engine_state, &self.stack, *val) .run_with_input(PipelineData::Empty) - .and_then(|data| data.into_value(span)) + .and_then(|data| data.try_into_value(span)) .unwrap_or_else(|err| Value::error(err, self.call.head)) } _ => value.clone(), diff --git a/crates/nu-plugin-engine/src/interface/mod.rs b/crates/nu-plugin-engine/src/interface/mod.rs index adab9dc68d..90b908aadf 100644 --- a/crates/nu-plugin-engine/src/interface/mod.rs +++ b/crates/nu-plugin-engine/src/interface/mod.rs @@ -12,7 +12,7 @@ use nu_plugin_protocol::{ }; use nu_protocol::{ ast::Operator, CustomValue, IntoSpanned, PipelineData, PluginSignature, ShellError, Span, - Spanned, Value, + Spanned, TryIntoValue, Value, }; use std::{ collections::{btree_map, BTreeMap}, @@ -953,7 +953,7 @@ impl PluginInterface { let call = PluginCall::CustomValueOp(value.map(|cv| cv.without_source()), op); match self.plugin_call(call, None)? { - PluginCallResponse::PipelineData(out_data) => out_data.into_value(span), + PluginCallResponse::PipelineData(out_data) => out_data.try_into_value(span), PluginCallResponse::Error(err) => Err(err.into()), _ => Err(ShellError::PluginFailedToDecode { msg: format!("Received unexpected response to custom value {op_name}() call"), diff --git a/crates/nu-plugin-engine/src/interface/tests.rs b/crates/nu-plugin-engine/src/interface/tests.rs index e718886b3b..2c590ef814 100644 --- a/crates/nu-plugin-engine/src/interface/tests.rs +++ b/crates/nu-plugin-engine/src/interface/tests.rs @@ -17,8 +17,8 @@ use nu_plugin_protocol::{ use nu_protocol::{ ast::{Math, Operator}, engine::Closure, - ByteStreamType, CustomValue, IntoInterruptiblePipelineData, IntoSpanned, PipelineData, - PluginSignature, ShellError, Span, Spanned, Value, + ByteStreamType, CustomValue, IntoInterruptiblePipelineData, IntoSpanned, IntoValue, + PipelineData, PluginSignature, ShellError, Span, Spanned, TryIntoValue, Value, }; use serde::{Deserialize, Serialize}; use std::{ @@ -1066,7 +1066,7 @@ fn interface_run() -> Result<(), ShellError> { assert_eq!( Value::test_int(number), - result.into_value(Span::test_data())?, + result.try_into_value(Span::test_data())?, ); assert!(test.has_unconsumed_write()); Ok(()) @@ -1115,7 +1115,7 @@ fn interface_prepare_pipeline_data_accepts_normal_values() -> Result<(), ShellEr match interface.prepare_pipeline_data(PipelineData::Value(value.clone(), None), &state) { Ok(data) => assert_eq!( value.get_type(), - data.into_value(Span::test_data())?.get_type(), + data.try_into_value(Span::test_data())?.get_type(), ), Err(err) => panic!("failed to accept {value:?}: {err}"), } diff --git a/crates/nu-plugin-engine/src/plugin_custom_value_with_source/mod.rs b/crates/nu-plugin-engine/src/plugin_custom_value_with_source/mod.rs index 01008a29e8..145231116d 100644 --- a/crates/nu-plugin-engine/src/plugin_custom_value_with_source/mod.rs +++ b/crates/nu-plugin-engine/src/plugin_custom_value_with_source/mod.rs @@ -2,7 +2,9 @@ use std::{cmp::Ordering, sync::Arc}; use nu_plugin_core::util::with_custom_values_in; use nu_plugin_protocol::PluginCustomValue; -use nu_protocol::{ast::Operator, CustomValue, IntoSpanned, ShellError, Span, Spanned, Value}; +use nu_protocol::{ + ast::Operator, CustomValue, IntoSpanned, IntoValue, ShellError, Span, Spanned, Value, +}; use serde::Serialize; use crate::{PluginInterface, PluginSource}; @@ -27,11 +29,6 @@ impl PluginCustomValueWithSource { PluginCustomValueWithSource { inner, source } } - /// Create a [`Value`] containing this custom value. - pub fn into_value(self, span: Span) -> Value { - Value::custom(Box::new(self), span) - } - /// Which plugin the custom value came from. This provides a direct reference to be able to get /// a plugin interface in order to make a call, when needed. pub fn source(&self) -> &Arc { @@ -133,6 +130,12 @@ impl PluginCustomValueWithSource { } } +impl IntoValue for PluginCustomValueWithSource { + fn into_value(self, span: Span) -> Value { + Value::custom(Box::new(self), span) + } +} + impl std::ops::Deref for PluginCustomValueWithSource { type Target = PluginCustomValue; diff --git a/crates/nu-plugin-protocol/src/plugin_custom_value/mod.rs b/crates/nu-plugin-protocol/src/plugin_custom_value/mod.rs index ea2b1551c9..2557878153 100644 --- a/crates/nu-plugin-protocol/src/plugin_custom_value/mod.rs +++ b/crates/nu-plugin-protocol/src/plugin_custom_value/mod.rs @@ -1,6 +1,6 @@ use std::cmp::Ordering; -use nu_protocol::{ast::Operator, CustomValue, ShellError, Span, Value}; +use nu_protocol::{ast::Operator, CustomValue, IntoValue, ShellError, Span, Value}; use nu_utils::SharedCow; use serde::{Deserialize, Serialize}; @@ -109,11 +109,6 @@ impl PluginCustomValue { })) } - /// Create a [`Value`] containing this custom value. - pub fn into_value(self, span: Span) -> Value { - Value::custom(Box::new(self), span) - } - /// The name of the type of the custom value as defined by the plugin (`type_name()`) pub fn name(&self) -> &str { &self.0.name @@ -219,3 +214,9 @@ impl PluginCustomValue { }) } } + +impl IntoValue for PluginCustomValue { + fn into_value(self, span: Span) -> Value { + Value::custom(Box::new(self), span) + } +} diff --git a/crates/nu-plugin-test-support/src/plugin_test.rs b/crates/nu-plugin-test-support/src/plugin_test.rs index 3d6b3eec23..ca9d201826 100644 --- a/crates/nu-plugin-test-support/src/plugin_test.rs +++ b/crates/nu-plugin-test-support/src/plugin_test.rs @@ -11,7 +11,7 @@ use nu_protocol::{ debugger::WithoutDebug, engine::{EngineState, Stack, StateWorkingSet}, report_error_new, CustomValue, Example, IntoSpanned as _, LabeledError, PipelineData, - ShellError, Span, Value, + ShellError, Span, TryIntoValue, Value, }; use crate::{diff::diff_by_line, fake_register::fake_register}; @@ -230,7 +230,7 @@ impl PluginTest { if let Some(expectation) = &example.result { match self.eval(example.example) { Ok(data) => { - let mut value = data.into_value(Span::test_data())?; + let mut value = data.try_into_value(Span::test_data())?; // Set all of the spans in the value to test_data() to avoid unnecessary // differences when printing diff --git a/crates/nu-plugin-test-support/tests/custom_value/mod.rs b/crates/nu-plugin-test-support/tests/custom_value/mod.rs index f703a92e33..acdbbf108c 100644 --- a/crates/nu-plugin-test-support/tests/custom_value/mod.rs +++ b/crates/nu-plugin-test-support/tests/custom_value/mod.rs @@ -3,7 +3,8 @@ use std::cmp::Ordering; use nu_plugin::{EngineInterface, EvaluatedCall, Plugin, SimplePluginCommand}; use nu_plugin_test_support::PluginTest; use nu_protocol::{ - CustomValue, Example, LabeledError, PipelineData, ShellError, Signature, Span, Type, Value, + CustomValue, Example, IntoValue, LabeledError, PipelineData, ShellError, Signature, Span, + TryIntoValue, Type, Value, }; use serde::{Deserialize, Serialize}; @@ -11,8 +12,8 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Serialize, Deserialize, PartialOrd, Ord, PartialEq, Eq)] struct CustomU32(u32); -impl CustomU32 { - pub fn into_value(self, span: Span) -> Value { +impl IntoValue for CustomU32 { + fn into_value(self, span: Span) -> Value { Value::custom(Box::new(self), span) } } @@ -143,7 +144,7 @@ fn test_into_int_from_u32() -> Result<(), ShellError> { "into int from u32", PipelineData::Value(CustomU32(42).into_value(Span::test_data()), None), )? - .into_value(Span::test_data())?; + .try_into_value(Span::test_data())?; assert_eq!(Value::test_int(42), result); Ok(()) } diff --git a/crates/nu-plugin-test-support/tests/hello/mod.rs b/crates/nu-plugin-test-support/tests/hello/mod.rs index 424940f156..a36ed63aed 100644 --- a/crates/nu-plugin-test-support/tests/hello/mod.rs +++ b/crates/nu-plugin-test-support/tests/hello/mod.rs @@ -2,7 +2,7 @@ use nu_plugin::*; use nu_plugin_test_support::PluginTest; -use nu_protocol::{Example, LabeledError, ShellError, Signature, Type, Value}; +use nu_protocol::{Example, LabeledError, ShellError, Signature, TryIntoValue, Type, Value}; struct HelloPlugin; struct Hello; @@ -80,7 +80,7 @@ fn test_requiring_nu_cmd_lang_commands() -> Result<(), ShellError> { let result = PluginTest::new("hello", HelloPlugin.into())? .eval("do { let greeting = hello; $greeting }")? - .into_value(Span::test_data())?; + .try_into_value(Span::test_data())?; assert_eq!(Value::test_string("Hello, World!"), result); diff --git a/crates/nu-plugin-test-support/tests/lowercase/mod.rs b/crates/nu-plugin-test-support/tests/lowercase/mod.rs index 0072a08aa2..4c270baaa5 100644 --- a/crates/nu-plugin-test-support/tests/lowercase/mod.rs +++ b/crates/nu-plugin-test-support/tests/lowercase/mod.rs @@ -2,7 +2,7 @@ use nu_plugin::*; use nu_plugin_test_support::PluginTest; use nu_protocol::{ Example, IntoInterruptiblePipelineData, LabeledError, PipelineData, ShellError, Signature, - Span, Type, Value, + Span, TryIntoValue, Type, Value, }; struct LowercasePlugin; @@ -73,7 +73,7 @@ fn test_lowercase_using_eval_with() -> Result<(), ShellError> { assert_eq!( Value::test_list(vec![Value::test_string("hello world")]), - result.into_value(Span::test_data())? + result.try_into_value(Span::test_data())? ); Ok(()) diff --git a/crates/nu-plugin/src/plugin/command.rs b/crates/nu-plugin/src/plugin/command.rs index 5def950b0b..f185b56aa8 100644 --- a/crates/nu-plugin/src/plugin/command.rs +++ b/crates/nu-plugin/src/plugin/command.rs @@ -1,6 +1,6 @@ use nu_protocol::{ Example, IntoSpanned, LabeledError, PipelineData, PluginExample, PluginSignature, ShellError, - Signature, Value, + Signature, TryIntoValue, Value, }; use crate::{EngineInterface, EvaluatedCall, Plugin}; @@ -313,7 +313,7 @@ where // Unwrap the PipelineData from input, consuming the potential stream, and pass it to the // simpler signature in Plugin let span = input.span().unwrap_or(call.head); - let input_value = input.into_value(span)?; + let input_value = input.try_into_value(span)?; // Wrap the output in PipelineData::Value ::run(self, plugin, engine, call, &input_value) .map(|value| PipelineData::Value(value, None)) diff --git a/crates/nu-plugin/src/plugin/interface/mod.rs b/crates/nu-plugin/src/plugin/interface/mod.rs index e3e9679471..1c024bbd9c 100644 --- a/crates/nu-plugin/src/plugin/interface/mod.rs +++ b/crates/nu-plugin/src/plugin/interface/mod.rs @@ -12,7 +12,7 @@ use nu_plugin_protocol::{ }; use nu_protocol::{ engine::Closure, Config, LabeledError, PipelineData, PluginSignature, ShellError, Span, - Spanned, Value, + Spanned, TryIntoValue, Value, }; use std::{ collections::{btree_map, BTreeMap, HashMap}, @@ -850,7 +850,7 @@ impl EngineInterface { let input = input.map_or_else(|| PipelineData::Empty, |v| PipelineData::Value(v, None)); let output = self.eval_closure_with_stream(closure, positional, input, true, false)?; // Unwrap an error value - match output.into_value(closure.span)? { + match output.try_into_value(closure.span)? { Value::Error { error, .. } => Err(*error), value => Ok(value), } diff --git a/crates/nu-plugin/src/plugin/interface/tests.rs b/crates/nu-plugin/src/plugin/interface/tests.rs index 6c3dfdf6c9..875225abc2 100644 --- a/crates/nu-plugin/src/plugin/interface/tests.rs +++ b/crates/nu-plugin/src/plugin/interface/tests.rs @@ -10,7 +10,7 @@ use nu_plugin_protocol::{ }; use nu_protocol::{ engine::Closure, ByteStreamType, Config, CustomValue, IntoInterruptiblePipelineData, - LabeledError, PipelineData, PluginSignature, ShellError, Span, Spanned, Value, + LabeledError, PipelineData, PluginSignature, ShellError, Span, Spanned, TryIntoValue, Value, }; use std::{ collections::HashMap, @@ -1039,7 +1039,7 @@ fn interface_eval_closure_with_stream() -> Result<(), ShellError> { true, false, )? - .into_value(Span::test_data())?; + .try_into_value(Span::test_data())?; assert_eq!(Value::test_int(2), result); diff --git a/crates/nu-protocol/src/eval_const.rs b/crates/nu-protocol/src/eval_const.rs index 9f81a6f38b..875ce1ad07 100644 --- a/crates/nu-protocol/src/eval_const.rs +++ b/crates/nu-protocol/src/eval_const.rs @@ -3,7 +3,8 @@ use crate::{ debugger::{DebugContext, WithoutDebug}, engine::{EngineState, StateWorkingSet}, eval_base::Eval, - record, Config, HistoryFileFormat, PipelineData, Record, ShellError, Span, Value, VarId, + record, Config, HistoryFileFormat, PipelineData, Record, ShellError, Span, TryIntoValue, Value, + VarId, }; use nu_system::os_info::{get_kernel_version, get_os_arch, get_os_family, get_os_name}; use std::{ @@ -349,7 +350,7 @@ impl Eval for EvalConst { ) -> Result { // TODO: Allow debugging const eval // TODO: eval.rs uses call.head for the span rather than expr.span - eval_const_call(working_set, call, PipelineData::empty())?.into_value(span) + eval_const_call(working_set, call, PipelineData::empty())?.try_into_value(span) } fn eval_external_call( @@ -371,7 +372,8 @@ impl Eval for EvalConst { ) -> Result { // TODO: Allow debugging const eval let block = working_set.get_block(block_id); - eval_const_subexpression(working_set, block, PipelineData::empty(), span)?.into_value(span) + eval_const_subexpression(working_set, block, PipelineData::empty(), span)? + .try_into_value(span) } fn regex_match( diff --git a/crates/nu-protocol/src/pipeline/byte_stream.rs b/crates/nu-protocol/src/pipeline/byte_stream.rs index babd195a9e..c127c4edb8 100644 --- a/crates/nu-protocol/src/pipeline/byte_stream.rs +++ b/crates/nu-protocol/src/pipeline/byte_stream.rs @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize}; use crate::{ process::{ChildPipe, ChildProcess, ExitStatus}, - ErrSpan, IntoSpanned, OutDest, PipelineData, ShellError, Span, Type, Value, + ErrSpan, IntoSpanned, OutDest, PipelineData, ShellError, Span, TryIntoValue, Type, Value, }; #[cfg(unix)] use std::os::fd::OwnedFd; @@ -166,9 +166,10 @@ impl From for Type { /// Additionally, there are few methods to collect a [`Bytestream`] into memory: /// - [`into_bytes`](ByteStream::into_bytes): collects all bytes into a [`Vec`]. /// - [`into_string`](ByteStream::into_string): collects all bytes into a [`String`], erroring if utf-8 decoding failed. -/// - [`into_value`](ByteStream::into_value): collects all bytes into a value typed appropriately -/// for the [type](.type_()) of this stream. If the type is [`Unknown`](ByteStreamType::Unknown), -/// it will produce a string value if the data is valid UTF-8, or a binary value otherwise. +/// - [`try_into_value`](ByteStream::try_into_value): collects all bytes into a value typed +/// appropriately for the [type](.type_()) of this stream. +/// If the type is [`Unknown`](ByteStreamType::Unknown), it will produce a string value if the +/// data is valid UTF-8, or a binary value otherwise. /// /// There are also a few other methods to consume all the data of a [`Bytestream`]: /// - [`drain`](ByteStream::drain): consumes all bytes and outputs nothing. @@ -512,37 +513,10 @@ impl ByteStream { } } - /// Collect all the bytes of the [`ByteStream`] into a [`Value`]. - /// - /// If this is a `String` stream, the stream is decoded to UTF-8. If the stream came from an - /// external process or file, the trailing new line (`\n` or `\r\n`), if any, is removed from - /// the [`String`] prior to being returned. - /// - /// If this is a `Binary` stream, a [`Value::Binary`] is returned with any trailing new lines - /// preserved. - /// - /// If this is an `Unknown` stream, the behavior depends on whether the stream parses as valid - /// UTF-8 or not. If it does, this is uses the `String` behavior; if not, it uses the `Binary` - /// behavior. + #[deprecated = "use `TryIntoValue::try_into_value` instead"] pub fn into_value(self) -> Result { - let span = self.span; - let trim = self.stream.is_external(); - let value = match self.type_ { - // If the type is specified, then the stream should always become that type: - ByteStreamType::Binary => Value::binary(self.into_bytes()?, span), - ByteStreamType::String => Value::string(self.into_string()?, span), - // If the type is not specified, then it just depends on whether it parses or not: - ByteStreamType::Unknown => match String::from_utf8(self.into_bytes()?) { - Ok(mut str) => { - if trim { - trim_end_newline(&mut str); - } - Value::string(str, span) - } - Err(err) => Value::binary(err.into_bytes(), span), - }, - }; - Ok(value) + let span = self.span(); + Self::try_into_value(self, span) } /// Consume and drop all bytes of the [`ByteStream`]. @@ -686,6 +660,41 @@ impl ByteStream { } } +impl TryIntoValue for ByteStream { + /// Collect all the bytes of the [`ByteStream`] into a [`Value`]. + /// + /// If this is a `String` stream, the stream is decoded to UTF-8. If the stream came from an + /// external process or file, the trailing new line (`\n` or `\r\n`), if any, is removed from + /// the [`String`] prior to being returned. + /// + /// If this is a `Binary` stream, a [`Value::Binary`] is returned with any trailing new lines + /// preserved. + /// + /// If this is an `Unknown` stream, the behavior depends on whether the stream parses as valid + /// UTF-8 or not. If it does, this is uses the `String` behavior; if not, it uses the `Binary` + /// behavior. + fn try_into_value(self, _: Span) -> Result { + let span = self.span; + let trim = self.stream.is_external(); + let value = match self.type_ { + // If the type is specified, then the stream should always become that type: + ByteStreamType::Binary => Value::binary(self.into_bytes()?, span), + ByteStreamType::String => Value::string(self.into_string()?, span), + // If the type is not specified, then it just depends on whether it parses or not: + ByteStreamType::Unknown => match String::from_utf8(self.into_bytes()?) { + Ok(mut str) => { + if trim { + trim_end_newline(&mut str); + } + Value::string(str, span) + } + Err(err) => Value::binary(err.into_bytes(), span), + }, + }; + Ok(value) + } +} + impl From for PipelineData { fn from(stream: ByteStream) -> Self { Self::ByteStream(stream, None) diff --git a/crates/nu-protocol/src/pipeline/list_stream.rs b/crates/nu-protocol/src/pipeline/list_stream.rs index 117c264219..85420e084e 100644 --- a/crates/nu-protocol/src/pipeline/list_stream.rs +++ b/crates/nu-protocol/src/pipeline/list_stream.rs @@ -1,4 +1,4 @@ -use crate::{Config, PipelineData, ShellError, Span, Value}; +use crate::{Config, IntoValue, PipelineData, ShellError, Span, Value}; use std::{ fmt::Debug, sync::{atomic::AtomicBool, Arc}, @@ -48,11 +48,6 @@ impl ListStream { .join(separator) } - /// Collect the values of a [`ListStream`] into a list [`Value`]. - pub fn into_value(self) -> Value { - Value::list(self.stream.collect(), self.span) - } - /// Consume all values in the stream, returning an error if any of the values is a `Value::Error`. pub fn drain(self) -> Result<(), ShellError> { for next in self { @@ -98,6 +93,13 @@ impl Debug for ListStream { } } +impl IntoValue for ListStream { + /// Collect the values of a [`ListStream`] into a list [`Value`]. + fn into_value(self, _: Span) -> Value { + Value::list(self.stream.collect(), self.span) + } +} + impl IntoIterator for ListStream { type Item = Value; diff --git a/crates/nu-protocol/src/pipeline/pipeline_data.rs b/crates/nu-protocol/src/pipeline/pipeline_data.rs index 03fbc64d21..395fa76c58 100644 --- a/crates/nu-protocol/src/pipeline/pipeline_data.rs +++ b/crates/nu-protocol/src/pipeline/pipeline_data.rs @@ -2,8 +2,8 @@ use crate::{ ast::{Call, PathMember}, engine::{EngineState, Stack}, process::{ChildPipe, ChildProcess, ExitStatus}, - ByteStream, ByteStreamType, Config, ErrSpan, ListStream, OutDest, PipelineMetadata, Range, - ShellError, Span, Type, Value, + ByteStream, ByteStreamType, Config, ErrSpan, IntoValue, ListStream, OutDest, PipelineMetadata, + Range, ShellError, Span, TryIntoValue, Type, Value, }; use nu_utils::{stderr_write_all_and_flush, stdout_write_all_and_flush}; use std::{ @@ -117,13 +117,9 @@ impl PipelineData { } } + #[deprecated = "use `TryIntoValue::try_into_value` instead"] pub fn into_value(self, span: Span) -> Result { - match self { - PipelineData::Empty => Ok(Value::nothing(span)), - PipelineData::Value(value, ..) => Ok(value.with_span(span)), - PipelineData::ListStream(stream, ..) => Ok(stream.into_value()), - PipelineData::ByteStream(stream, ..) => stream.into_value(), - } + Self::try_into_value(self, span) } /// Writes all values or redirects all output to the current [`OutDest`]s in `stack`. @@ -333,7 +329,8 @@ impl PipelineData { Ok(PipelineData::ListStream(stream.map(f), metadata)) } PipelineData::ByteStream(stream, metadata) => { - Ok(f(stream.into_value()?).into_pipeline_data_with_metadata(metadata)) + let span = stream.span(); + Ok(f(stream.try_into_value(span)?).into_pipeline_data_with_metadata(metadata)) } } } @@ -612,6 +609,17 @@ impl PipelineData { } } +impl TryIntoValue for PipelineData { + fn try_into_value(self, span: Span) -> Result { + match self { + PipelineData::Empty => Ok(Value::nothing(span)), + PipelineData::Value(value, ..) => Ok(value.with_span(span)), + PipelineData::ListStream(stream, ..) => Ok(stream.into_value(span)), + PipelineData::ByteStream(stream, ..) => stream.try_into_value(span), + } + } +} + enum PipelineIteratorInner { Empty, Value(Value), diff --git a/crates/nu-protocol/tests/test_pipeline_data.rs b/crates/nu-protocol/tests/test_pipeline_data.rs index 95941285ad..be3259443f 100644 --- a/crates/nu-protocol/tests/test_pipeline_data.rs +++ b/crates/nu-protocol/tests/test_pipeline_data.rs @@ -1,4 +1,4 @@ -use nu_protocol::{IntoPipelineData, Span, Value}; +use nu_protocol::{IntoPipelineData, Span, TryIntoValue, Value}; #[test] fn test_convert_pipeline_data_to_value() { @@ -9,7 +9,7 @@ fn test_convert_pipeline_data_to_value() { // Test that conversion into Value is correct let new_span = Span::new(5, 6); - let converted_value = pipeline_data.into_value(new_span); + let converted_value = pipeline_data.try_into_value(new_span); assert_eq!(converted_value, Ok(Value::int(value_val, new_span))); } diff --git a/crates/nu_plugin_custom_values/src/cool_custom_value.rs b/crates/nu_plugin_custom_values/src/cool_custom_value.rs index ea0cf8ec92..2bf2c8fa41 100644 --- a/crates/nu_plugin_custom_values/src/cool_custom_value.rs +++ b/crates/nu_plugin_custom_values/src/cool_custom_value.rs @@ -1,4 +1,4 @@ -use nu_protocol::{ast, CustomValue, ShellError, Span, Value}; +use nu_protocol::{ast, CustomValue, IntoValue, ShellError, Span, Value}; use serde::{Deserialize, Serialize}; use std::cmp::Ordering; @@ -14,10 +14,6 @@ impl CoolCustomValue { } } - pub fn into_value(self, span: Span) -> Value { - Value::custom(Box::new(self), span) - } - pub fn try_from_value(value: &Value) -> Result { let span = value.span(); match value { @@ -43,6 +39,12 @@ impl CoolCustomValue { } } +impl IntoValue for CoolCustomValue { + fn into_value(self, span: Span) -> Value { + Value::custom(Box::new(self), span) + } +} + #[typetag::serde] impl CustomValue for CoolCustomValue { fn clone_value(&self, span: Span) -> Value { diff --git a/crates/nu_plugin_custom_values/src/generate.rs b/crates/nu_plugin_custom_values/src/generate.rs index b4cc8bd6b1..1593463982 100644 --- a/crates/nu_plugin_custom_values/src/generate.rs +++ b/crates/nu_plugin_custom_values/src/generate.rs @@ -1,6 +1,6 @@ use crate::{cool_custom_value::CoolCustomValue, CustomValuePlugin}; use nu_plugin::{EngineInterface, EvaluatedCall, SimplePluginCommand}; -use nu_protocol::{Category, Example, LabeledError, Signature, Span, Value}; +use nu_protocol::{Category, Example, IntoValue, LabeledError, Signature, Span, Value}; pub struct Generate; diff --git a/crates/nu_plugin_custom_values/src/generate2.rs b/crates/nu_plugin_custom_values/src/generate2.rs index 806086f4fb..d1fb2346cc 100644 --- a/crates/nu_plugin_custom_values/src/generate2.rs +++ b/crates/nu_plugin_custom_values/src/generate2.rs @@ -1,6 +1,8 @@ use crate::{second_custom_value::SecondCustomValue, CustomValuePlugin}; use nu_plugin::{EngineInterface, EvaluatedCall, SimplePluginCommand}; -use nu_protocol::{Category, Example, LabeledError, Signature, Span, SyntaxShape, Value}; +use nu_protocol::{ + Category, Example, IntoValue, LabeledError, Signature, Span, SyntaxShape, Value, +}; pub struct Generate2; diff --git a/crates/nu_plugin_custom_values/src/handle_custom_value.rs b/crates/nu_plugin_custom_values/src/handle_custom_value.rs index ac4fc6bbea..b59600360d 100644 --- a/crates/nu_plugin_custom_values/src/handle_custom_value.rs +++ b/crates/nu_plugin_custom_values/src/handle_custom_value.rs @@ -1,12 +1,12 @@ -use nu_protocol::{CustomValue, LabeledError, ShellError, Span, Value}; +use nu_protocol::{CustomValue, IntoValue, LabeledError, ShellError, Span, Value}; use serde::{Deserialize, Serialize}; /// References a stored handle within the plugin #[derive(Debug, Clone, Serialize, Deserialize)] pub struct HandleCustomValue(pub u64); -impl HandleCustomValue { - pub fn into_value(self, span: Span) -> Value { +impl IntoValue for HandleCustomValue { + fn into_value(self, span: Span) -> Value { Value::custom(Box::new(self), span) } } diff --git a/crates/nu_plugin_custom_values/src/second_custom_value.rs b/crates/nu_plugin_custom_values/src/second_custom_value.rs index fde02cfade..9e0f2a4c89 100644 --- a/crates/nu_plugin_custom_values/src/second_custom_value.rs +++ b/crates/nu_plugin_custom_values/src/second_custom_value.rs @@ -1,6 +1,6 @@ use std::cmp::Ordering; -use nu_protocol::{CustomValue, ShellError, Span, Value}; +use nu_protocol::{CustomValue, IntoValue, ShellError, Span, Value}; use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] @@ -15,10 +15,6 @@ impl SecondCustomValue { } } - pub fn into_value(self, span: Span) -> Value { - Value::custom(Box::new(self), span) - } - pub fn try_from_value(value: &Value) -> Result { let span = value.span(); match value { @@ -41,6 +37,12 @@ impl SecondCustomValue { } } +impl IntoValue for SecondCustomValue { + fn into_value(self, span: Span) -> Value { + Value::custom(Box::new(self), span) + } +} + #[typetag::serde] impl CustomValue for SecondCustomValue { fn clone_value(&self, span: nu_protocol::Span) -> Value { diff --git a/crates/nu_plugin_custom_values/src/update.rs b/crates/nu_plugin_custom_values/src/update.rs index 0ce7c09ec9..5bb5a7c132 100644 --- a/crates/nu_plugin_custom_values/src/update.rs +++ b/crates/nu_plugin_custom_values/src/update.rs @@ -2,7 +2,7 @@ use crate::{ cool_custom_value::CoolCustomValue, second_custom_value::SecondCustomValue, CustomValuePlugin, }; use nu_plugin::{EngineInterface, EvaluatedCall, SimplePluginCommand}; -use nu_protocol::{Category, Example, LabeledError, ShellError, Signature, Span, Value}; +use nu_protocol::{Category, Example, IntoValue, LabeledError, ShellError, Signature, Span, Value}; pub struct Update; diff --git a/crates/nu_plugin_polars/src/cache/get.rs b/crates/nu_plugin_polars/src/cache/get.rs index 11ade261b9..279cf73097 100644 --- a/crates/nu_plugin_polars/src/cache/get.rs +++ b/crates/nu_plugin_polars/src/cache/get.rs @@ -1,7 +1,7 @@ use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ - Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, - Value, + Category, Example, IntoValue, LabeledError, PipelineData, ShellError, Signature, Span, + SyntaxShape, Type, Value, }; use polars::{prelude::NamedFrom, series::Series}; use uuid::Uuid; diff --git a/crates/nu_plugin_polars/src/dataframe/eager/cast.rs b/crates/nu_plugin_polars/src/dataframe/eager/cast.rs index d676fb04ea..e76f448980 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/cast.rs +++ b/crates/nu_plugin_polars/src/dataframe/eager/cast.rs @@ -8,7 +8,7 @@ use super::super::values::NuDataFrame; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ record, Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, - SyntaxShape, Type, Value, + SyntaxShape, TryIntoValue, Type, Value, }; use polars::prelude::*; @@ -91,7 +91,7 @@ impl PluginCommand for CastDF { call: &EvaluatedCall, input: PipelineData, ) -> Result { - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; match PolarsPluginObject::try_from_value(plugin, &value)? { PolarsPluginObject::NuLazyFrame(lazy) => { let (dtype, column_nm) = df_args(call)?; diff --git a/crates/nu_plugin_polars/src/dataframe/eager/filter_with.rs b/crates/nu_plugin_polars/src/dataframe/eager/filter_with.rs index b95f646edd..456ec8ec30 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/filter_with.rs +++ b/crates/nu_plugin_polars/src/dataframe/eager/filter_with.rs @@ -1,7 +1,7 @@ use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ - Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, - Value, + Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, + TryIntoValue, Type, Value, }; use polars::prelude::LazyFrame; @@ -84,7 +84,7 @@ impl PluginCommand for FilterWith { call: &EvaluatedCall, input: PipelineData, ) -> Result { - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; match PolarsPluginObject::try_from_value(plugin, &value)? { PolarsPluginObject::NuDataFrame(df) => command_eager(plugin, engine, call, df), PolarsPluginObject::NuLazyFrame(lazy) => command_lazy(plugin, engine, call, lazy), diff --git a/crates/nu_plugin_polars/src/dataframe/eager/first.rs b/crates/nu_plugin_polars/src/dataframe/eager/first.rs index a35ea7dd16..3012763022 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/first.rs +++ b/crates/nu_plugin_polars/src/dataframe/eager/first.rs @@ -6,8 +6,8 @@ use crate::{ use super::super::values::{NuDataFrame, NuExpression}; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ - Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, - Value, + Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, + TryIntoValue, Type, Value, }; #[derive(Clone)] @@ -97,7 +97,7 @@ impl PluginCommand for FirstDF { call: &EvaluatedCall, input: PipelineData, ) -> Result { - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; if NuDataFrame::can_downcast(&value) || NuLazyFrame::can_downcast(&value) { let df = NuDataFrame::try_from_value_coerce(plugin, &value, call.head)?; command(plugin, engine, call, df).map_err(|e| e.into()) diff --git a/crates/nu_plugin_polars/src/dataframe/eager/last.rs b/crates/nu_plugin_polars/src/dataframe/eager/last.rs index 23b44eb473..80ee889b1c 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/last.rs +++ b/crates/nu_plugin_polars/src/dataframe/eager/last.rs @@ -6,8 +6,8 @@ use crate::{ use super::super::values::{utils::DEFAULT_ROWS, NuDataFrame, NuExpression}; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ - Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, - Value, + Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, + TryIntoValue, Type, Value, }; #[derive(Clone)] @@ -72,7 +72,7 @@ impl PluginCommand for LastDF { call: &EvaluatedCall, input: PipelineData, ) -> Result { - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; if NuDataFrame::can_downcast(&value) || NuLazyFrame::can_downcast(&value) { let df = NuDataFrame::try_from_value_coerce(plugin, &value, call.head)?; command(plugin, engine, call, df).map_err(|e| e.into()) diff --git a/crates/nu_plugin_polars/src/dataframe/eager/rename.rs b/crates/nu_plugin_polars/src/dataframe/eager/rename.rs index 724cf1f983..a03518c3e2 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/rename.rs +++ b/crates/nu_plugin_polars/src/dataframe/eager/rename.rs @@ -1,7 +1,7 @@ use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ - Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, - Value, + Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, + TryIntoValue, Type, Value, }; use crate::{ @@ -120,7 +120,7 @@ impl PluginCommand for RenameDF { call: &EvaluatedCall, input: PipelineData, ) -> Result { - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; match PolarsPluginObject::try_from_value(plugin, &value).map_err(LabeledError::from)? { PolarsPluginObject::NuDataFrame(df) => { command_eager(plugin, engine, call, df).map_err(LabeledError::from) diff --git a/crates/nu_plugin_polars/src/dataframe/eager/to_nu.rs b/crates/nu_plugin_polars/src/dataframe/eager/to_nu.rs index 72aff7a728..3c3572cd81 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/to_nu.rs +++ b/crates/nu_plugin_polars/src/dataframe/eager/to_nu.rs @@ -1,7 +1,7 @@ use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ record, Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, - SyntaxShape, Type, Value, + SyntaxShape, TryIntoValue, Type, Value, }; use crate::{ @@ -90,7 +90,7 @@ impl PluginCommand for ToNu { call: &EvaluatedCall, input: PipelineData, ) -> Result { - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; if NuDataFrame::can_downcast(&value) || NuLazyFrame::can_downcast(&value) { dataframe_command(plugin, call, value) } else { diff --git a/crates/nu_plugin_polars/src/dataframe/eager/with_column.rs b/crates/nu_plugin_polars/src/dataframe/eager/with_column.rs index 5db11766b9..a1fe7ea47c 100644 --- a/crates/nu_plugin_polars/src/dataframe/eager/with_column.rs +++ b/crates/nu_plugin_polars/src/dataframe/eager/with_column.rs @@ -6,8 +6,8 @@ use crate::{ }; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ - Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, - Value, + Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, + TryIntoValue, Type, Value, }; #[derive(Clone)] @@ -113,7 +113,7 @@ impl PluginCommand for WithColumn { call: &EvaluatedCall, input: PipelineData, ) -> Result { - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; match PolarsPluginObject::try_from_value(plugin, &value)? { PolarsPluginObject::NuDataFrame(df) => command_eager(plugin, engine, call, df), PolarsPluginObject::NuLazyFrame(lazy) => command_lazy(plugin, engine, call, lazy), diff --git a/crates/nu_plugin_polars/src/dataframe/expressions/expressions_macro.rs b/crates/nu_plugin_polars/src/dataframe/expressions/expressions_macro.rs index 58b60f3427..0fe801be7a 100644 --- a/crates/nu_plugin_polars/src/dataframe/expressions/expressions_macro.rs +++ b/crates/nu_plugin_polars/src/dataframe/expressions/expressions_macro.rs @@ -5,7 +5,9 @@ use crate::dataframe::values::{Column, NuDataFrame, NuExpression, NuLazyFrame}; use crate::values::CustomValueSupport; use crate::PolarsPlugin; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; -use nu_protocol::{Category, Example, LabeledError, PipelineData, Signature, Span, Type, Value}; +use nu_protocol::{ + Category, Example, LabeledError, PipelineData, Signature, Span, TryIntoValue, Type, Value, +}; // The structs defined in this file are structs that form part of other commands // since they share a similar name @@ -158,7 +160,7 @@ macro_rules! lazy_expr_command { call: &EvaluatedCall, input: PipelineData, ) -> Result { - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; if NuDataFrame::can_downcast(&value) || NuLazyFrame::can_downcast(&value) { let lazy = NuLazyFrame::try_from_value_coerce(plugin, &value) .map_err(LabeledError::from)?; @@ -228,7 +230,7 @@ macro_rules! lazy_expr_command { call: &EvaluatedCall, input: PipelineData, ) -> Result { - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; if NuDataFrame::can_downcast(&value) || NuLazyFrame::can_downcast(&value) { let lazy = NuLazyFrame::try_from_value_coerce(plugin, &value) .map_err(LabeledError::from)?; diff --git a/crates/nu_plugin_polars/src/dataframe/expressions/is_in.rs b/crates/nu_plugin_polars/src/dataframe/expressions/is_in.rs index 136ac5c1e6..214abcca48 100644 --- a/crates/nu_plugin_polars/src/dataframe/expressions/is_in.rs +++ b/crates/nu_plugin_polars/src/dataframe/expressions/is_in.rs @@ -5,8 +5,8 @@ use crate::{ }; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ - Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, - Value, + Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, + TryIntoValue, Type, Value, }; use polars::prelude::{is_in, lit, DataType, IntoSeries}; @@ -114,7 +114,7 @@ impl PluginCommand for ExprIsIn { call: &EvaluatedCall, input: PipelineData, ) -> Result { - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; match PolarsPluginObject::try_from_value(plugin, &value)? { PolarsPluginObject::NuDataFrame(df) => command_df(plugin, engine, call, df), PolarsPluginObject::NuLazyFrame(lazy) => { diff --git a/crates/nu_plugin_polars/src/dataframe/expressions/otherwise.rs b/crates/nu_plugin_polars/src/dataframe/expressions/otherwise.rs index 73e616f8f8..d0516a3dd6 100644 --- a/crates/nu_plugin_polars/src/dataframe/expressions/otherwise.rs +++ b/crates/nu_plugin_polars/src/dataframe/expressions/otherwise.rs @@ -5,7 +5,8 @@ use crate::{ }; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ - Category, Example, LabeledError, PipelineData, Signature, Span, SyntaxShape, Type, Value, + Category, Example, LabeledError, PipelineData, Signature, Span, SyntaxShape, TryIntoValue, + Type, Value, }; #[derive(Clone)] @@ -99,7 +100,7 @@ impl PluginCommand for ExprOtherwise { let otherwise_predicate: Value = call.req(0)?; let otherwise_predicate = NuExpression::try_from_value(plugin, &otherwise_predicate)?; - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; let complete: NuExpression = match NuWhen::try_from_value(plugin, &value)?.when_type { NuWhenType::Then(then) => then.otherwise(otherwise_predicate.into_polars()).into(), NuWhenType::ChainedThen(chained_when) => chained_when diff --git a/crates/nu_plugin_polars/src/dataframe/expressions/when.rs b/crates/nu_plugin_polars/src/dataframe/expressions/when.rs index 9ed9ffddac..011252396a 100644 --- a/crates/nu_plugin_polars/src/dataframe/expressions/when.rs +++ b/crates/nu_plugin_polars/src/dataframe/expressions/when.rs @@ -5,7 +5,8 @@ use crate::{ }; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ - Category, Example, LabeledError, PipelineData, Signature, Span, SyntaxShape, Type, Value, + Category, Example, LabeledError, PipelineData, Signature, Span, SyntaxShape, TryIntoValue, + Type, Value, }; use polars::prelude::when; @@ -111,7 +112,7 @@ impl PluginCommand for ExprWhen { let then_predicate: Value = call.req(1)?; let then_predicate = NuExpression::try_from_value(plugin, &then_predicate)?; - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; let when_then: NuWhen = match value { Value::Nothing { .. } => when(when_predicate.into_polars()) .then(then_predicate.into_polars()) diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/collect.rs b/crates/nu_plugin_polars/src/dataframe/lazy/collect.rs index 91adc4f713..97df28e927 100644 --- a/crates/nu_plugin_polars/src/dataframe/lazy/collect.rs +++ b/crates/nu_plugin_polars/src/dataframe/lazy/collect.rs @@ -5,7 +5,9 @@ use crate::{ }; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; -use nu_protocol::{Category, Example, LabeledError, PipelineData, Signature, Span, Type, Value}; +use nu_protocol::{ + Category, Example, LabeledError, PipelineData, Signature, Span, TryIntoValue, Type, Value, +}; #[derive(Clone)] pub struct LazyCollect; @@ -61,7 +63,7 @@ impl PluginCommand for LazyCollect { call: &EvaluatedCall, input: PipelineData, ) -> Result { - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; match PolarsPluginObject::try_from_value(plugin, &value)? { PolarsPluginObject::NuLazyFrame(lazy) => { let mut eager = lazy.collect(call.head)?; diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/explode.rs b/crates/nu_plugin_polars/src/dataframe/lazy/explode.rs index 787f07fd46..ea6a21f826 100644 --- a/crates/nu_plugin_polars/src/dataframe/lazy/explode.rs +++ b/crates/nu_plugin_polars/src/dataframe/lazy/explode.rs @@ -4,8 +4,8 @@ use crate::PolarsPlugin; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ - Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, - Value, + Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, + TryIntoValue, Type, Value, }; #[derive(Clone)] @@ -116,7 +116,7 @@ pub(crate) fn explode( call: &EvaluatedCall, input: PipelineData, ) -> Result { - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; match PolarsPluginObject::try_from_value(plugin, &value)? { PolarsPluginObject::NuDataFrame(df) => { let lazy = df.lazy(); diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/fetch.rs b/crates/nu_plugin_polars/src/dataframe/lazy/fetch.rs index eb9663265b..9638ed9041 100644 --- a/crates/nu_plugin_polars/src/dataframe/lazy/fetch.rs +++ b/crates/nu_plugin_polars/src/dataframe/lazy/fetch.rs @@ -3,8 +3,8 @@ use crate::values::{CustomValueSupport, NuLazyFrame}; use crate::PolarsPlugin; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ - Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, - Value, + Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, + TryIntoValue, Type, Value, }; #[derive(Clone)] @@ -67,7 +67,7 @@ impl PluginCommand for LazyFetch { input: PipelineData, ) -> Result { let rows: i64 = call.req(0)?; - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; let lazy = NuLazyFrame::try_from_value_coerce(plugin, &value)?; let mut eager: NuDataFrame = lazy diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/fill_nan.rs b/crates/nu_plugin_polars/src/dataframe/lazy/fill_nan.rs index baeb9da01b..b8bcd487e1 100644 --- a/crates/nu_plugin_polars/src/dataframe/lazy/fill_nan.rs +++ b/crates/nu_plugin_polars/src/dataframe/lazy/fill_nan.rs @@ -5,8 +5,8 @@ use crate::{ }; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ - Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, - Value, + Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, + TryIntoValue, Type, Value, }; #[derive(Clone)] @@ -92,7 +92,7 @@ impl PluginCommand for LazyFillNA { input: PipelineData, ) -> Result { let fill: Value = call.req(0)?; - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; match PolarsPluginObject::try_from_value(plugin, &value)? { PolarsPluginObject::NuDataFrame(df) => { diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/fill_null.rs b/crates/nu_plugin_polars/src/dataframe/lazy/fill_null.rs index 6f4f13b08e..ae69fb32ce 100644 --- a/crates/nu_plugin_polars/src/dataframe/lazy/fill_null.rs +++ b/crates/nu_plugin_polars/src/dataframe/lazy/fill_null.rs @@ -5,8 +5,8 @@ use crate::{ }; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ - Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, - Value, + Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, + TryIntoValue, Type, Value, }; #[derive(Clone)] @@ -69,7 +69,7 @@ impl PluginCommand for LazyFillNull { input: PipelineData, ) -> Result { let fill: Value = call.req(0)?; - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; match PolarsPluginObject::try_from_value(plugin, &value)? { PolarsPluginObject::NuDataFrame(df) => cmd_lazy(plugin, engine, call, df.lazy(), fill), diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/filter.rs b/crates/nu_plugin_polars/src/dataframe/lazy/filter.rs index e7e30fd57d..314a3f316c 100644 --- a/crates/nu_plugin_polars/src/dataframe/lazy/filter.rs +++ b/crates/nu_plugin_polars/src/dataframe/lazy/filter.rs @@ -6,8 +6,8 @@ use crate::{ use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ - Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, - Value, + Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, + TryIntoValue, Type, Value, }; #[derive(Clone)] @@ -72,7 +72,7 @@ impl PluginCommand for LazyFilter { ) -> Result { let expr_value: Value = call.req(0)?; let filter_expr = NuExpression::try_from_value(plugin, &expr_value)?; - let pipeline_value = input.into_value(call.head)?; + let pipeline_value = input.try_into_value(call.head)?; let lazy = NuLazyFrame::try_from_value_coerce(plugin, &pipeline_value)?; command(plugin, engine, call, lazy, filter_expr).map_err(LabeledError::from) } diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/groupby.rs b/crates/nu_plugin_polars/src/dataframe/lazy/groupby.rs index 9edf8f5e60..f1b997f6a0 100644 --- a/crates/nu_plugin_polars/src/dataframe/lazy/groupby.rs +++ b/crates/nu_plugin_polars/src/dataframe/lazy/groupby.rs @@ -5,8 +5,8 @@ use crate::{ }; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ - Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, - Value, + Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, + TryIntoValue, Type, Value, }; use polars::prelude::Expr; @@ -138,7 +138,7 @@ impl PluginCommand for ToLazyGroupBy { })?; } - let pipeline_value = input.into_value(call.head)?; + let pipeline_value = input.try_into_value(call.head)?; let lazy = NuLazyFrame::try_from_value_coerce(plugin, &pipeline_value)?; command(plugin, engine, call, lazy, expressions).map_err(LabeledError::from) } diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/join.rs b/crates/nu_plugin_polars/src/dataframe/lazy/join.rs index 67f5aee9ba..f344787ad1 100644 --- a/crates/nu_plugin_polars/src/dataframe/lazy/join.rs +++ b/crates/nu_plugin_polars/src/dataframe/lazy/join.rs @@ -5,8 +5,8 @@ use crate::{ }; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ - Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, - Value, + Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, + TryIntoValue, Type, Value, }; use polars::prelude::{Expr, JoinType}; @@ -228,7 +228,7 @@ impl PluginCommand for LazyJoin { let suffix: Option = call.get_flag("suffix")?; let suffix = suffix.unwrap_or_else(|| "_x".into()); - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; let lazy = NuLazyFrame::try_from_value_coerce(plugin, &value)?; let from_eager = lazy.from_eager; let lazy = lazy.to_polars(); diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/median.rs b/crates/nu_plugin_polars/src/dataframe/lazy/median.rs index 8106218c26..9c94fa8aa7 100644 --- a/crates/nu_plugin_polars/src/dataframe/lazy/median.rs +++ b/crates/nu_plugin_polars/src/dataframe/lazy/median.rs @@ -7,7 +7,8 @@ use crate::{ }; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ - Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, Type, Value, + Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, TryIntoValue, Type, + Value, }; #[derive(Clone)] pub struct LazyMedian; @@ -89,7 +90,7 @@ impl PluginCommand for LazyMedian { call: &EvaluatedCall, input: PipelineData, ) -> Result { - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; match PolarsPluginObject::try_from_value(plugin, &value)? { PolarsPluginObject::NuDataFrame(df) => command(plugin, engine, call, df.lazy()), PolarsPluginObject::NuLazyFrame(lazy) => command(plugin, engine, call, lazy), diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/quantile.rs b/crates/nu_plugin_polars/src/dataframe/lazy/quantile.rs index e63fae6610..378233ba4b 100644 --- a/crates/nu_plugin_polars/src/dataframe/lazy/quantile.rs +++ b/crates/nu_plugin_polars/src/dataframe/lazy/quantile.rs @@ -7,8 +7,8 @@ use crate::{ }; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ - Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, - Value, + Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, + TryIntoValue, Type, Value, }; use polars::prelude::{lit, QuantileInterpolOptions}; @@ -97,7 +97,7 @@ impl PluginCommand for LazyQuantile { call: &EvaluatedCall, input: PipelineData, ) -> Result { - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; let quantile: f64 = call.req(0)?; match PolarsPluginObject::try_from_value(plugin, &value)? { PolarsPluginObject::NuDataFrame(df) => { diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/select.rs b/crates/nu_plugin_polars/src/dataframe/lazy/select.rs index 03c4306c30..8ae00c151f 100644 --- a/crates/nu_plugin_polars/src/dataframe/lazy/select.rs +++ b/crates/nu_plugin_polars/src/dataframe/lazy/select.rs @@ -6,7 +6,8 @@ use crate::{ use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ - Category, Example, LabeledError, PipelineData, Signature, Span, SyntaxShape, Type, Value, + Category, Example, LabeledError, PipelineData, Signature, Span, SyntaxShape, TryIntoValue, + Type, Value, }; #[derive(Clone)] pub struct LazySelect; @@ -65,7 +66,7 @@ impl PluginCommand for LazySelect { let expr_value = Value::list(vals, call.head); let expressions = NuExpression::extract_exprs(plugin, expr_value)?; - let pipeline_value = input.into_value(call.head)?; + let pipeline_value = input.try_into_value(call.head)?; let lazy = NuLazyFrame::try_from_value_coerce(plugin, &pipeline_value)?; let lazy = NuLazyFrame::new(lazy.from_eager, lazy.to_polars().select(&expressions)); lazy.to_pipeline_data(plugin, engine, call.head) diff --git a/crates/nu_plugin_polars/src/dataframe/lazy/sort_by_expr.rs b/crates/nu_plugin_polars/src/dataframe/lazy/sort_by_expr.rs index b282bd6d04..f452c2eb28 100644 --- a/crates/nu_plugin_polars/src/dataframe/lazy/sort_by_expr.rs +++ b/crates/nu_plugin_polars/src/dataframe/lazy/sort_by_expr.rs @@ -6,8 +6,8 @@ use crate::{ }; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ - Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, - Value, + Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, + TryIntoValue, Type, Value, }; use polars::chunked_array::ops::SortMultipleOptions; @@ -145,7 +145,7 @@ impl PluginCommand for LazySortBy { maintain_order, }; - let pipeline_value = input.into_value(call.head)?; + let pipeline_value = input.try_into_value(call.head)?; let lazy = NuLazyFrame::try_from_value_coerce(plugin, &pipeline_value)?; let lazy = NuLazyFrame::new( lazy.from_eager, diff --git a/crates/nu_plugin_polars/src/dataframe/series/masks/is_not_null.rs b/crates/nu_plugin_polars/src/dataframe/series/masks/is_not_null.rs index b7e506a67c..def36b90fb 100644 --- a/crates/nu_plugin_polars/src/dataframe/series/masks/is_not_null.rs +++ b/crates/nu_plugin_polars/src/dataframe/series/masks/is_not_null.rs @@ -6,7 +6,8 @@ use crate::{ use super::super::super::values::{Column, NuDataFrame, NuExpression}; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ - Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, Type, Value, + Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, TryIntoValue, Type, + Value, }; use polars::prelude::IntoSeries; @@ -78,7 +79,7 @@ impl PluginCommand for IsNotNull { call: &EvaluatedCall, input: PipelineData, ) -> Result { - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; match PolarsPluginObject::try_from_value(plugin, &value)? { PolarsPluginObject::NuDataFrame(df) => command(plugin, engine, call, df), PolarsPluginObject::NuLazyFrame(lazy) => { diff --git a/crates/nu_plugin_polars/src/dataframe/series/masks/is_null.rs b/crates/nu_plugin_polars/src/dataframe/series/masks/is_null.rs index bc04e7fb76..9ee12b2db6 100644 --- a/crates/nu_plugin_polars/src/dataframe/series/masks/is_null.rs +++ b/crates/nu_plugin_polars/src/dataframe/series/masks/is_null.rs @@ -8,7 +8,8 @@ use crate::{ use super::super::super::values::{Column, NuDataFrame}; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ - Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, Type, Value, + Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, TryIntoValue, Type, + Value, }; use polars::prelude::IntoSeries; @@ -80,7 +81,7 @@ impl PluginCommand for IsNull { call: &EvaluatedCall, input: PipelineData, ) -> Result { - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; match PolarsPluginObject::try_from_value(plugin, &value)? { PolarsPluginObject::NuDataFrame(df) => command(plugin, engine, call, df), PolarsPluginObject::NuLazyFrame(lazy) => { diff --git a/crates/nu_plugin_polars/src/dataframe/series/n_unique.rs b/crates/nu_plugin_polars/src/dataframe/series/n_unique.rs index 51c6e1bdb3..8c63c20feb 100644 --- a/crates/nu_plugin_polars/src/dataframe/series/n_unique.rs +++ b/crates/nu_plugin_polars/src/dataframe/series/n_unique.rs @@ -6,7 +6,8 @@ use crate::{ use super::super::values::{Column, NuDataFrame, NuExpression}; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ - Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, Type, Value, + Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, TryIntoValue, Type, + Value, }; #[derive(Clone)] @@ -70,7 +71,7 @@ impl PluginCommand for NUnique { call: &EvaluatedCall, input: PipelineData, ) -> Result { - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; match PolarsPluginObject::try_from_value(plugin, &value)? { PolarsPluginObject::NuDataFrame(df) => command(plugin, engine, call, df), PolarsPluginObject::NuLazyFrame(lazy) => { diff --git a/crates/nu_plugin_polars/src/dataframe/series/shift.rs b/crates/nu_plugin_polars/src/dataframe/series/shift.rs index 58dc64ec47..52b383c2e5 100644 --- a/crates/nu_plugin_polars/src/dataframe/series/shift.rs +++ b/crates/nu_plugin_polars/src/dataframe/series/shift.rs @@ -8,8 +8,8 @@ use super::super::values::{Column, NuDataFrame}; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ - Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, - Value, + Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, + TryIntoValue, Type, Value, }; use polars_plan::prelude::lit; @@ -93,7 +93,7 @@ impl PluginCommand for Shift { call: &EvaluatedCall, input: PipelineData, ) -> Result { - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; match PolarsPluginObject::try_from_value(plugin, &value)? { PolarsPluginObject::NuDataFrame(df) => command_eager(plugin, engine, call, df), diff --git a/crates/nu_plugin_polars/src/dataframe/series/unique.rs b/crates/nu_plugin_polars/src/dataframe/series/unique.rs index 7626b35404..6cb400e79a 100644 --- a/crates/nu_plugin_polars/src/dataframe/series/unique.rs +++ b/crates/nu_plugin_polars/src/dataframe/series/unique.rs @@ -8,8 +8,8 @@ use super::super::values::{Column, NuDataFrame}; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_protocol::{ - Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, - Value, + Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, + TryIntoValue, Type, Value, }; use polars::prelude::{IntoSeries, UniqueKeepStrategy}; @@ -134,7 +134,7 @@ impl PluginCommand for Unique { call: &EvaluatedCall, input: PipelineData, ) -> Result { - let value = input.into_value(call.head)?; + let value = input.try_into_value(call.head)?; match PolarsPluginObject::try_from_value(plugin, &value)? { PolarsPluginObject::NuDataFrame(df) => command_eager(plugin, engine, call, df), diff --git a/crates/nu_plugin_polars/src/dataframe/values/mod.rs b/crates/nu_plugin_polars/src/dataframe/values/mod.rs index d52ee25203..e930794a5e 100644 --- a/crates/nu_plugin_polars/src/dataframe/values/mod.rs +++ b/crates/nu_plugin_polars/src/dataframe/values/mod.rs @@ -13,7 +13,9 @@ pub use nu_expression::{NuExpression, NuExpressionCustomValue}; pub use nu_lazyframe::{NuLazyFrame, NuLazyFrameCustomValue}; pub use nu_lazygroupby::{NuLazyGroupBy, NuLazyGroupByCustomValue}; use nu_plugin::EngineInterface; -use nu_protocol::{ast::Operator, CustomValue, PipelineData, ShellError, Span, Spanned, Value}; +use nu_protocol::{ + ast::Operator, CustomValue, PipelineData, ShellError, Span, Spanned, TryIntoValue, Value, +}; pub use nu_schema::{str_to_dtype, NuSchema}; pub use nu_when::{NuWhen, NuWhenCustomValue, NuWhenType}; use uuid::Uuid; @@ -84,7 +86,7 @@ impl PolarsPluginObject { input: PipelineData, span: Span, ) -> Result { - let value = input.into_value(span)?; + let value = input.try_into_value(span)?; Self::try_from_value(plugin, &value) } @@ -107,8 +109,10 @@ impl PolarsPluginObject { PolarsPluginObject::NuWhen(w) => w.id, } } +} - pub fn into_value(self, span: Span) -> Value { +impl nu_protocol::IntoValue for PolarsPluginObject { + fn into_value(self, span: Span) -> Value { match self { PolarsPluginObject::NuDataFrame(df) => df.into_value(span), PolarsPluginObject::NuLazyFrame(lf) => lf.into_value(span), @@ -301,7 +305,7 @@ pub trait CustomValueSupport: Cacheable { input: PipelineData, span: Span, ) -> Result { - let value = input.into_value(span)?; + let value = input.try_into_value(span)?; Self::try_from_value(plugin, &value) } diff --git a/crates/nu_plugin_polars/src/dataframe/values/nu_dataframe/mod.rs b/crates/nu_plugin_polars/src/dataframe/values/nu_dataframe/mod.rs index 89a8c862e7..00c2d789ef 100644 --- a/crates/nu_plugin_polars/src/dataframe/values/nu_dataframe/mod.rs +++ b/crates/nu_plugin_polars/src/dataframe/values/nu_dataframe/mod.rs @@ -7,7 +7,7 @@ pub use conversion::{Column, ColumnMap}; pub use operations::Axis; use indexmap::map::IndexMap; -use nu_protocol::{did_you_mean, PipelineData, Record, ShellError, Span, Value}; +use nu_protocol::{did_you_mean, PipelineData, Record, ShellError, Span, TryIntoValue, Value}; use polars::{ chunked_array::ops::SortMultipleOptions, prelude::{DataFrame, DataType, IntoLazy, PolarsObject, Series}, @@ -533,7 +533,7 @@ impl NuDataFrame { input: PipelineData, span: Span, ) -> Result { - let value = input.into_value(span)?; + let value = input.try_into_value(span)?; Self::try_from_value_coerce(plugin, &value, span) } } diff --git a/crates/nu_plugin_polars/src/dataframe/values/nu_lazyframe/mod.rs b/crates/nu_plugin_polars/src/dataframe/values/nu_lazyframe/mod.rs index 4dc231d706..efc558ecfc 100644 --- a/crates/nu_plugin_polars/src/dataframe/values/nu_lazyframe/mod.rs +++ b/crates/nu_plugin_polars/src/dataframe/values/nu_lazyframe/mod.rs @@ -7,7 +7,7 @@ use super::{ PolarsPluginType, }; use core::fmt; -use nu_protocol::{record, PipelineData, ShellError, Span, Value}; +use nu_protocol::{record, PipelineData, ShellError, Span, TryIntoValue, Value}; use polars::prelude::{Expr, IntoLazy, LazyFrame}; use std::sync::Arc; use uuid::Uuid; @@ -111,7 +111,7 @@ impl NuLazyFrame { input: PipelineData, span: Span, ) -> Result { - let value = input.into_value(span)?; + let value = input.try_into_value(span)?; Self::try_from_value_coerce(plugin, &value) } }