From 000885202ac67c6d9d2b30c7ff5ece1969633500 Mon Sep 17 00:00:00 2001 From: Ian Manske Date: Sat, 23 Nov 2024 22:16:53 -0800 Subject: [PATCH] Replace `ShellError::UnsupportedOperator` with new operator errors --- crates/nu-protocol/src/errors/shell_error.rs | 14 ------------ crates/nu-protocol/src/value/custom_value.rs | 12 +++++++--- .../src/cool_custom_value.rs | 13 ++++++----- .../src/dataframe/values/mod.rs | 22 +++++++++++-------- 4 files changed, 30 insertions(+), 31 deletions(-) diff --git a/crates/nu-protocol/src/errors/shell_error.rs b/crates/nu-protocol/src/errors/shell_error.rs index 6514028003..5f0bee1586 100644 --- a/crates/nu-protocol/src/errors/shell_error.rs +++ b/crates/nu-protocol/src/errors/shell_error.rs @@ -164,20 +164,6 @@ pub enum ShellError { call_span: Span, }, - /// This value cannot be used with this operator. - /// - /// ## Resolution - /// - /// Not all values, for example custom values, can be used with all operators. Either - /// implement support for the operator on this type, or convert the type to a supported one. - #[error("Unsupported operator: {operator}.")] - #[diagnostic(code(nu::shell::unsupported_operator))] - UnsupportedOperator { - operator: Operator, - #[label = "unsupported operator"] - span: Span, - }, - /// Invalid assignment left-hand side /// /// ## Resolution diff --git a/crates/nu-protocol/src/value/custom_value.rs b/crates/nu-protocol/src/value/custom_value.rs index 480ca0018e..fbfc4ee911 100644 --- a/crates/nu-protocol/src/value/custom_value.rs +++ b/crates/nu-protocol/src/value/custom_value.rs @@ -1,6 +1,6 @@ use std::{cmp::Ordering, fmt}; -use crate::{ast::Operator, ShellError, Span, Value}; +use crate::{ast::Operator, ShellError, Span, Type, Value}; /// Trait definition for a custom [`Value`](crate::Value) type #[typetag::serde(tag = "type")] @@ -68,7 +68,7 @@ pub trait CustomValue: fmt::Debug + Send + Sync { /// /// The Operator enum is used to indicate the expected operation. /// - /// Default impl raises [`ShellError::UnsupportedOperator`]. + /// Default impl raises [`ShellError::OperatorUnsupportedType`]. fn operation( &self, lhs_span: Span, @@ -77,7 +77,13 @@ pub trait CustomValue: fmt::Debug + Send + Sync { right: &Value, ) -> Result { let _ = (lhs_span, right); - Err(ShellError::UnsupportedOperator { operator, span: op }) + Err(ShellError::OperatorUnsupportedType { + op: operator, + unsupported: Type::Custom(self.type_name().into()), + op_span: op, + unsupported_span: lhs_span, + help: None, + }) } /// For custom values in plugins: return `true` here if you would like to be notified when all 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 f29f63ef49..a6ee6f4168 100644 --- a/crates/nu_plugin_custom_values/src/cool_custom_value.rs +++ b/crates/nu_plugin_custom_values/src/cool_custom_value.rs @@ -1,6 +1,6 @@ use nu_protocol::{ ast::{self, Math, Operator}, - CustomValue, ShellError, Span, Value, + CustomValue, ShellError, Span, Type, Value, }; use serde::{Deserialize, Serialize}; use std::cmp::Ordering; @@ -108,7 +108,7 @@ impl CustomValue for CoolCustomValue { fn operation( &self, - _lhs_span: Span, + lhs_span: Span, operator: ast::Operator, op_span: Span, right: &Value, @@ -137,9 +137,12 @@ impl CustomValue for CoolCustomValue { }) } } - _ => Err(ShellError::UnsupportedOperator { - operator, - span: op_span, + _ => Err(ShellError::OperatorUnsupportedType { + op: Operator::Math(Math::Concatenate), + unsupported: Type::Custom(self.type_name().into()), + op_span, + unsupported_span: lhs_span, + help: None, }), } } diff --git a/crates/nu_plugin_polars/src/dataframe/values/mod.rs b/crates/nu_plugin_polars/src/dataframe/values/mod.rs index 42fb8dace1..fec45fe1bb 100644 --- a/crates/nu_plugin_polars/src/dataframe/values/mod.rs +++ b/crates/nu_plugin_polars/src/dataframe/values/mod.rs @@ -7,20 +7,21 @@ mod nu_schema; mod nu_when; pub mod utils; +use crate::{Cacheable, PolarsPlugin}; +use nu_plugin::EngineInterface; +use nu_protocol::{ + ast::Operator, CustomValue, PipelineData, ShellError, Span, Spanned, Type, Value, +}; use std::{cmp::Ordering, fmt}; +use uuid::Uuid; pub use file_type::PolarsFileType; pub use nu_dataframe::{Axis, Column, NuDataFrame, NuDataFrameCustomValue}; 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}; pub use nu_schema::{str_to_dtype, NuSchema}; pub use nu_when::{NuWhen, NuWhenCustomValue, NuWhenType}; -use uuid::Uuid; - -use crate::{Cacheable, PolarsPlugin}; #[derive(Debug, Clone)] pub enum PolarsPluginType { @@ -217,13 +218,16 @@ pub trait PolarsPluginCustomValue: CustomValue { &self, _plugin: &PolarsPlugin, _engine: &EngineInterface, - _lhs_span: Span, + lhs_span: Span, operator: Spanned, _right: Value, ) -> Result { - Err(ShellError::UnsupportedOperator { - operator: operator.item, - span: operator.span, + Err(ShellError::OperatorUnsupportedType { + op: operator.item, + unsupported: Type::Custom(self.type_name().into()), + op_span: operator.span, + unsupported_span: lhs_span, + help: None, }) }