mirror of
https://github.com/nushell/nushell.git
synced 2025-05-07 00:12:58 +00:00
# Description The second `Value` is redundant and will consume five extra bytes on each transmission of a custom value to/from a plugin. # User-Facing Changes This is a breaking change to the plugin protocol. The [example in the protocol reference](https://www.nushell.sh/contributor-book/plugin_protocol_reference.html#value) becomes ```json { "Custom": { "val": { "type": "PluginCustomValue", "name": "database", "data": [36, 190, 127, 40, 12, 3, 46, 83], "notify_on_drop": true }, "span": { "start": 320, "end": 340 } } } ``` instead of ```json { "CustomValue": { ... } } ``` # After Submitting Update plugin protocol reference
78 lines
2.1 KiB
Rust
78 lines
2.1 KiB
Rust
use std::cmp::Ordering;
|
|
|
|
use nu_protocol::{CustomValue, ShellError, Span, Value};
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
|
|
pub struct SecondCustomValue {
|
|
pub(crate) something: String,
|
|
}
|
|
|
|
impl SecondCustomValue {
|
|
pub fn new(content: &str) -> Self {
|
|
Self {
|
|
something: content.to_owned(),
|
|
}
|
|
}
|
|
|
|
pub fn into_value(self, span: Span) -> Value {
|
|
Value::custom(Box::new(self), span)
|
|
}
|
|
|
|
pub fn try_from_value(value: &Value) -> Result<Self, ShellError> {
|
|
let span = value.span();
|
|
match value {
|
|
Value::Custom { val, .. } => match val.as_any().downcast_ref::<Self>() {
|
|
Some(value) => Ok(value.clone()),
|
|
None => Err(ShellError::CantConvert {
|
|
to_type: "cool".into(),
|
|
from_type: "non-cool".into(),
|
|
span,
|
|
help: None,
|
|
}),
|
|
},
|
|
x => Err(ShellError::CantConvert {
|
|
to_type: "cool".into(),
|
|
from_type: x.get_type().to_string(),
|
|
span,
|
|
help: None,
|
|
}),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[typetag::serde]
|
|
impl CustomValue for SecondCustomValue {
|
|
fn clone_value(&self, span: nu_protocol::Span) -> Value {
|
|
Value::custom(Box::new(self.clone()), span)
|
|
}
|
|
|
|
fn type_name(&self) -> String {
|
|
self.typetag_name().to_string()
|
|
}
|
|
|
|
fn to_base_value(&self, span: nu_protocol::Span) -> Result<Value, ShellError> {
|
|
Ok(Value::string(
|
|
format!(
|
|
"I used to be a DIFFERENT custom value! ({})",
|
|
self.something
|
|
),
|
|
span,
|
|
))
|
|
}
|
|
|
|
fn partial_cmp(&self, other: &Value) -> Option<Ordering> {
|
|
if let Value::Custom { val, .. } = other {
|
|
val.as_any()
|
|
.downcast_ref()
|
|
.and_then(|other: &SecondCustomValue| PartialOrd::partial_cmp(self, other))
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
fn as_any(&self) -> &dyn std::any::Any {
|
|
self
|
|
}
|
|
}
|