nushell/crates/nu-protocol/src/value/custom_value.rs
Devyn Cairns ff41cf91ef
Misc doc fixes (#12266)
# Description

Just a bunch of miscellaneous fixes to the Rust documentation that I
found recently while doing
a pass on some things.

# User-Facing Changes
None
2024-03-23 07:26:08 -05:00

90 lines
2.9 KiB
Rust

use std::{cmp::Ordering, fmt};
use crate::{ast::Operator, ShellError, Span, Value};
/// Trait definition for a custom [`Value`](crate::Value) type
#[typetag::serde(tag = "type")]
pub trait CustomValue: fmt::Debug + Send + Sync {
/// Custom `Clone` implementation
///
/// This can reemit a `Value::CustomValue(Self, span)` or materialize another representation
/// if necessary.
fn clone_value(&self, span: Span) -> Value;
//fn category(&self) -> Category;
/// The friendly type name to show for the custom value, e.g. in `describe` and in error
/// messages. This does not have to be the same as the name of the struct or enum, but
/// conventionally often is.
fn type_name(&self) -> String;
/// Converts the custom value to a base nushell value.
///
/// This imposes the requirement that you can represent the custom value in some form using the
/// Value representations that already exist in nushell
fn to_base_value(&self, span: Span) -> Result<Value, ShellError>;
/// Any representation used to downcast object to its original type
fn as_any(&self) -> &dyn std::any::Any;
/// Follow cell path by numeric index (e.g. rows)
fn follow_path_int(
&self,
self_span: Span,
index: usize,
path_span: Span,
) -> Result<Value, ShellError> {
let _ = (self_span, index);
Err(ShellError::IncompatiblePathAccess {
type_name: self.type_name(),
span: path_span,
})
}
/// Follow cell path by string key (e.g. columns)
fn follow_path_string(
&self,
self_span: Span,
column_name: String,
path_span: Span,
) -> Result<Value, ShellError> {
let _ = (self_span, column_name);
Err(ShellError::IncompatiblePathAccess {
type_name: self.type_name(),
span: path_span,
})
}
/// ordering with other value (see [`std::cmp::PartialOrd`])
fn partial_cmp(&self, _other: &Value) -> Option<Ordering> {
None
}
/// Definition of an operation between the object that implements the trait
/// and another Value.
///
/// The Operator enum is used to indicate the expected operation.
///
/// Default impl raises [`ShellError::UnsupportedOperator`].
fn operation(
&self,
lhs_span: Span,
operator: Operator,
op: Span,
right: &Value,
) -> Result<Value, ShellError> {
let _ = (lhs_span, right);
Err(ShellError::UnsupportedOperator { operator, span: op })
}
/// For custom values in plugins: return `true` here if you would like to be notified when all
/// copies of this custom value are dropped in the engine.
///
/// The notification will take place via `custom_value_dropped()` on the plugin type.
///
/// The default is `false`.
fn notify_plugin_on_drop(&self) -> bool {
false
}
}