2021-11-29 07:50:57 +01:00
|
|
|
use std::{cmp::Ordering, fmt};
|
2021-11-23 09:14:40 +01:00
|
|
|
|
2021-11-28 20:35:02 +01:00
|
|
|
use crate::{ast::Operator, Category, ShellError, Span, Value};
|
2021-11-23 09:14:40 +01:00
|
|
|
|
|
|
|
// Trait definition for a custom value
|
|
|
|
#[typetag::serde(tag = "type")]
|
|
|
|
pub trait CustomValue: fmt::Debug + Send + Sync {
|
|
|
|
fn clone_value(&self, span: Span) -> Value;
|
|
|
|
|
2021-11-28 20:35:02 +01:00
|
|
|
fn category(&self) -> Category;
|
|
|
|
|
2021-11-23 09:14:40 +01:00
|
|
|
// Define string representation of the custom value
|
|
|
|
fn value_string(&self) -> String;
|
|
|
|
|
|
|
|
// Converts the custom value to a base nushell value
|
|
|
|
// This is used to represent the custom value using the table representations
|
|
|
|
// That already exist in nushell
|
|
|
|
fn to_base_value(&self, span: Span) -> Result<Value, ShellError>;
|
|
|
|
|
|
|
|
// Json representation of custom value
|
|
|
|
fn to_json(&self) -> nu_json::Value {
|
|
|
|
nu_json::Value::Null
|
|
|
|
}
|
|
|
|
|
|
|
|
// Any representation used to downcast object to its original type
|
|
|
|
fn as_any(&self) -> &dyn std::any::Any;
|
|
|
|
|
|
|
|
// Follow cell path functions
|
|
|
|
fn follow_path_int(&self, count: usize, span: Span) -> Result<Value, ShellError>;
|
|
|
|
fn follow_path_string(&self, column_name: String, span: Span) -> Result<Value, ShellError>;
|
|
|
|
|
2021-11-29 07:50:57 +01:00
|
|
|
// ordering with other value
|
2021-12-06 05:09:49 +01:00
|
|
|
fn partial_cmp(&self, _other: &Value) -> Option<Ordering> {
|
|
|
|
None
|
|
|
|
}
|
2021-11-29 07:50:57 +01:00
|
|
|
|
2021-11-23 09:14:40 +01:00
|
|
|
// Definition of an operation between the object that implements the trait
|
|
|
|
// and another Value.
|
|
|
|
// The Operator enum is used to indicate the expected operation
|
|
|
|
fn operation(
|
|
|
|
&self,
|
|
|
|
_lhs_span: Span,
|
|
|
|
operator: Operator,
|
|
|
|
op: Span,
|
|
|
|
_right: &Value,
|
|
|
|
) -> Result<Value, ShellError> {
|
|
|
|
Err(ShellError::UnsupportedOperator(operator, op))
|
|
|
|
}
|
|
|
|
}
|