mirror of
https://github.com/nushell/nushell.git
synced 2024-11-28 19:33:47 +01:00
91d44f15c1
# Description This allows plugins to report their version (and potentially other metadata in the future). The version is shown in `plugin list` and in `version`. The metadata is stored in the registry file, and reflects whatever was retrieved on `plugin add`, not necessarily the running binary. This can help you to diagnose if there's some kind of mismatch with what you expect. We could potentially use this functionality to show a warning or error if a plugin being run does not have the same version as what was in the cache file, suggesting `plugin add` be run again, but I haven't done that at this point. It is optional, and it requires the plugin author to make some code changes if they want to provide it, since I can't automatically determine the version of the calling crate or anything tricky like that to do it. Example: ``` > plugin list | select name version is_running pid ╭───┬────────────────┬─────────┬────────────┬─────╮ │ # │ name │ version │ is_running │ pid │ ├───┼────────────────┼─────────┼────────────┼─────┤ │ 0 │ example │ 0.93.1 │ false │ │ │ 1 │ gstat │ 0.93.1 │ false │ │ │ 2 │ inc │ 0.93.1 │ false │ │ │ 3 │ python_example │ 0.1.0 │ false │ │ ╰───┴────────────────┴─────────┴────────────┴─────╯ ``` cc @maxim-uvarov (he asked for it) # User-Facing Changes - `plugin list` gets a `version` column - `version` shows plugin versions when available - plugin authors *should* add `fn metadata()` to their `impl Plugin`, but don't have to # Tests + Formatting Tested the low level stuff and also the `plugin list` column. # After Submitting - [ ] update plugin guide docs - [ ] update plugin protocol docs (`Metadata` call & response) - [ ] update plugin template (`fn metadata()` should be easy) - [ ] release notes
154 lines
4.0 KiB
Rust
154 lines
4.0 KiB
Rust
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,
|
|
};
|
|
|
|
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 {
|
|
Value::custom(Box::new(self), span)
|
|
}
|
|
}
|
|
|
|
#[typetag::serde]
|
|
impl CustomValue for CustomU32 {
|
|
fn clone_value(&self, span: Span) -> Value {
|
|
self.clone().into_value(span)
|
|
}
|
|
|
|
fn type_name(&self) -> String {
|
|
"CustomU32".into()
|
|
}
|
|
|
|
fn to_base_value(&self, span: Span) -> Result<Value, ShellError> {
|
|
Ok(Value::int(self.0 as i64, span))
|
|
}
|
|
|
|
fn as_any(&self) -> &dyn std::any::Any {
|
|
self
|
|
}
|
|
|
|
fn as_mut_any(&mut self) -> &mut dyn std::any::Any {
|
|
self
|
|
}
|
|
|
|
fn partial_cmp(&self, other: &Value) -> Option<Ordering> {
|
|
other
|
|
.as_custom_value()
|
|
.ok()
|
|
.and_then(|cv| cv.as_any().downcast_ref::<CustomU32>())
|
|
.and_then(|other_u32| PartialOrd::partial_cmp(self, other_u32))
|
|
}
|
|
}
|
|
|
|
struct CustomU32Plugin;
|
|
struct IntoU32;
|
|
struct IntoIntFromU32;
|
|
|
|
impl Plugin for CustomU32Plugin {
|
|
fn version(&self) -> String {
|
|
"0.0.0".into()
|
|
}
|
|
|
|
fn commands(&self) -> Vec<Box<dyn nu_plugin::PluginCommand<Plugin = Self>>> {
|
|
vec![Box::new(IntoU32), Box::new(IntoIntFromU32)]
|
|
}
|
|
}
|
|
|
|
impl SimplePluginCommand for IntoU32 {
|
|
type Plugin = CustomU32Plugin;
|
|
|
|
fn name(&self) -> &str {
|
|
"into u32"
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Convert a number to a 32-bit unsigned integer"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build(self.name()).input_output_type(Type::Int, Type::Custom("CustomU32".into()))
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![Example {
|
|
example: "340 | into u32",
|
|
description: "Make a u32",
|
|
result: Some(CustomU32(340).into_value(Span::test_data())),
|
|
}]
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
_plugin: &Self::Plugin,
|
|
_engine: &EngineInterface,
|
|
call: &EvaluatedCall,
|
|
input: &Value,
|
|
) -> Result<Value, LabeledError> {
|
|
let value: i64 = input.as_int()?;
|
|
let value_u32 = u32::try_from(value).map_err(|err| {
|
|
LabeledError::new(format!("Not a valid u32: {value}"))
|
|
.with_label(err.to_string(), input.span())
|
|
})?;
|
|
Ok(CustomU32(value_u32).into_value(call.head))
|
|
}
|
|
}
|
|
|
|
impl SimplePluginCommand for IntoIntFromU32 {
|
|
type Plugin = CustomU32Plugin;
|
|
|
|
fn name(&self) -> &str {
|
|
"into int from u32"
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Turn a u32 back into a number"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build(self.name()).input_output_type(Type::Custom("CustomU32".into()), Type::Int)
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
_plugin: &Self::Plugin,
|
|
_engine: &EngineInterface,
|
|
call: &EvaluatedCall,
|
|
input: &Value,
|
|
) -> Result<Value, LabeledError> {
|
|
let value: &CustomU32 = input
|
|
.as_custom_value()?
|
|
.as_any()
|
|
.downcast_ref()
|
|
.ok_or_else(|| ShellError::TypeMismatch {
|
|
err_message: "expected CustomU32".into(),
|
|
span: input.span(),
|
|
})?;
|
|
Ok(Value::int(value.0 as i64, call.head))
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn test_into_u32_examples() -> Result<(), ShellError> {
|
|
PluginTest::new("custom_u32", CustomU32Plugin.into())?.test_command_examples(&IntoU32)
|
|
}
|
|
|
|
#[test]
|
|
fn test_into_int_from_u32() -> Result<(), ShellError> {
|
|
let result = PluginTest::new("custom_u32", CustomU32Plugin.into())?
|
|
.eval_with(
|
|
"into int from u32",
|
|
PipelineData::Value(CustomU32(42).into_value(Span::test_data()), None),
|
|
)?
|
|
.into_value(Span::test_data())?;
|
|
assert_eq!(Value::test_int(42), result);
|
|
Ok(())
|
|
}
|