mirror of
https://github.com/nushell/nushell.git
synced 2025-08-23 23:36:18 +02:00
Plugin json (#474)
* json encoder * thread to pass messages * description for example
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use super::value;
|
||||
use crate::{evaluated_call::EvaluatedCall, plugin_capnp::evaluated_call};
|
||||
use crate::{plugin_capnp::evaluated_call, EvaluatedCall};
|
||||
use nu_protocol::{ShellError, Span, Spanned, Value};
|
||||
|
||||
pub(crate) fn serialize_call(
|
43
crates/nu-plugin/src/serializers/capnp/mod.rs
Normal file
43
crates/nu-plugin/src/serializers/capnp/mod.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
mod call;
|
||||
mod plugin_call;
|
||||
mod signature;
|
||||
mod value;
|
||||
|
||||
use nu_protocol::ShellError;
|
||||
|
||||
use crate::{plugin::PluginEncoder, protocol::PluginResponse};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CapnpSerializer;
|
||||
|
||||
impl PluginEncoder for CapnpSerializer {
|
||||
fn encode_call(
|
||||
&self,
|
||||
plugin_call: &crate::protocol::PluginCall,
|
||||
writer: &mut impl std::io::Write,
|
||||
) -> Result<(), nu_protocol::ShellError> {
|
||||
plugin_call::encode_call(plugin_call, writer)
|
||||
}
|
||||
|
||||
fn decode_call(
|
||||
&self,
|
||||
reader: &mut impl std::io::BufRead,
|
||||
) -> Result<crate::protocol::PluginCall, nu_protocol::ShellError> {
|
||||
plugin_call::decode_call(reader)
|
||||
}
|
||||
|
||||
fn encode_response(
|
||||
&self,
|
||||
plugin_response: &PluginResponse,
|
||||
writer: &mut impl std::io::Write,
|
||||
) -> Result<(), ShellError> {
|
||||
plugin_call::encode_response(plugin_response, writer)
|
||||
}
|
||||
|
||||
fn decode_response(
|
||||
&self,
|
||||
reader: &mut impl std::io::BufRead,
|
||||
) -> Result<PluginResponse, ShellError> {
|
||||
plugin_call::decode_response(reader)
|
||||
}
|
||||
}
|
@@ -1,7 +1,7 @@
|
||||
use crate::plugin::{CallInfo, LabeledError, PluginCall, PluginResponse};
|
||||
use super::signature::deserialize_signature;
|
||||
use super::{call, signature, value};
|
||||
use crate::plugin_capnp::{plugin_call, plugin_response};
|
||||
use crate::serializers::signature::deserialize_signature;
|
||||
use crate::serializers::{call, signature, value};
|
||||
use crate::protocol::{CallInfo, LabeledError, PluginCall, PluginResponse};
|
||||
use capnp::serialize;
|
||||
use nu_protocol::{ShellError, Signature, Span};
|
||||
|
||||
@@ -191,8 +191,7 @@ pub fn decode_response(reader: &mut impl std::io::BufRead) -> Result<PluginRespo
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::evaluated_call::EvaluatedCall;
|
||||
use crate::plugin::{PluginCall, PluginResponse};
|
||||
use crate::protocol::{EvaluatedCall, LabeledError, PluginCall, PluginResponse};
|
||||
use nu_protocol::{Signature, Span, Spanned, SyntaxShape, Value};
|
||||
|
||||
#[test]
|
144
crates/nu-plugin/src/serializers/capnp/schema/plugin.capnp
Normal file
144
crates/nu-plugin/src/serializers/capnp/schema/plugin.capnp
Normal file
@@ -0,0 +1,144 @@
|
||||
@0xb299d30dc02d72bc;
|
||||
# Schema representing all the structs that are used to comunicate with
|
||||
# the plugins.
|
||||
# This schema, together with the command capnp proto is used to generate
|
||||
# the rust file that defines the serialization/deserialization objects
|
||||
# required to comunicate with the plugins created for nushell
|
||||
#
|
||||
# If you modify the schema remember to compile it to generate the corresponding
|
||||
# rust file and place that file into the main nu-plugin folder.
|
||||
# After compiling, you may need to run cargo fmt on the file so it passes the CI
|
||||
|
||||
struct Err(T) {
|
||||
union {
|
||||
err @0 :Text;
|
||||
ok @1 :T;
|
||||
}
|
||||
}
|
||||
|
||||
struct Map(Key, Value) {
|
||||
struct Entry {
|
||||
key @0 :Key;
|
||||
value @1 :Value;
|
||||
}
|
||||
entries @0 :List(Entry);
|
||||
}
|
||||
|
||||
# Main plugin structures
|
||||
struct Span {
|
||||
start @0 :UInt64;
|
||||
end @1 :UInt64;
|
||||
}
|
||||
|
||||
# Resulting value from plugin
|
||||
struct Value {
|
||||
span @0: Span;
|
||||
|
||||
union {
|
||||
void @1 :Void;
|
||||
bool @2 :Bool;
|
||||
int @3 :Int64;
|
||||
float @4 :Float64;
|
||||
string @5 :Text;
|
||||
list @6 :List(Value);
|
||||
record @7: Record;
|
||||
}
|
||||
}
|
||||
|
||||
struct Record {
|
||||
cols @0 :List(Text);
|
||||
vals @1 :List(Value);
|
||||
}
|
||||
|
||||
# Structs required to define the plugin signature
|
||||
struct Signature {
|
||||
name @0 :Text;
|
||||
usage @1 :Text;
|
||||
extraUsage @2 :Text;
|
||||
requiredPositional @3 :List(Argument);
|
||||
optionalPositional @4 :List(Argument);
|
||||
# Optional value. Check for existence when deserializing
|
||||
rest @5 :Argument;
|
||||
named @6 :List(Flag);
|
||||
isFilter @7 :Bool;
|
||||
category @8 :Category;
|
||||
}
|
||||
|
||||
enum Category {
|
||||
default @0;
|
||||
conversions @1;
|
||||
core @2;
|
||||
date @3;
|
||||
env @4;
|
||||
experimental @5;
|
||||
filesystem @6;
|
||||
filters @7;
|
||||
formats @8;
|
||||
math @9;
|
||||
strings @10;
|
||||
system @11;
|
||||
viewers @12;
|
||||
}
|
||||
|
||||
struct Flag {
|
||||
long @0 :Text;
|
||||
# Optional value. Check for existence when deserializing (has_short)
|
||||
short @1 :Text;
|
||||
arg @2 :Shape;
|
||||
required @3 :Bool;
|
||||
desc @4 :Text;
|
||||
}
|
||||
|
||||
struct Argument {
|
||||
name @0 :Text;
|
||||
desc @1 :Text;
|
||||
shape @2 :Shape;
|
||||
}
|
||||
|
||||
# If we require more complex signatures for the plugins this could be
|
||||
# changed to a union
|
||||
enum Shape {
|
||||
none @0;
|
||||
any @1;
|
||||
string @2;
|
||||
number @3;
|
||||
int @4;
|
||||
boolean @5;
|
||||
}
|
||||
|
||||
struct EvaluatedCall {
|
||||
head @0: Span;
|
||||
positional @1 :List(Value);
|
||||
# The value in the map can be optional
|
||||
# Check for existence when deserializing
|
||||
named @2 :Map(Text, Value);
|
||||
}
|
||||
|
||||
struct CallInfo {
|
||||
name @0 :Text;
|
||||
call @1 :EvaluatedCall;
|
||||
input @2 :Value;
|
||||
}
|
||||
|
||||
# Main communication structs with the plugin
|
||||
struct PluginCall {
|
||||
union {
|
||||
signature @0 :Void;
|
||||
callInfo @1 :CallInfo;
|
||||
}
|
||||
}
|
||||
|
||||
struct PluginResponse {
|
||||
union {
|
||||
error @0 :LabeledError;
|
||||
signature @1 :List(Signature);
|
||||
value @2 :Value;
|
||||
}
|
||||
}
|
||||
|
||||
struct LabeledError {
|
||||
label @0 :Text;
|
||||
msg @1 :Text;
|
||||
# Optional Value. When decoding check if it exists (has_span)
|
||||
span @2 :Span;
|
||||
}
|
284
crates/nu-plugin/src/serializers/json.rs
Normal file
284
crates/nu-plugin/src/serializers/json.rs
Normal file
@@ -0,0 +1,284 @@
|
||||
use nu_protocol::ShellError;
|
||||
|
||||
use crate::{plugin::PluginEncoder, protocol::PluginResponse};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct JsonSerializer;
|
||||
|
||||
impl PluginEncoder for JsonSerializer {
|
||||
fn encode_call(
|
||||
&self,
|
||||
plugin_call: &crate::protocol::PluginCall,
|
||||
writer: &mut impl std::io::Write,
|
||||
) -> Result<(), nu_protocol::ShellError> {
|
||||
serde_json::to_writer(writer, plugin_call)
|
||||
.map_err(|err| ShellError::PluginFailedToEncode(err.to_string()))
|
||||
}
|
||||
|
||||
fn decode_call(
|
||||
&self,
|
||||
reader: &mut impl std::io::BufRead,
|
||||
) -> Result<crate::protocol::PluginCall, nu_protocol::ShellError> {
|
||||
serde_json::from_reader(reader)
|
||||
.map_err(|err| ShellError::PluginFailedToEncode(err.to_string()))
|
||||
}
|
||||
|
||||
fn encode_response(
|
||||
&self,
|
||||
plugin_response: &PluginResponse,
|
||||
writer: &mut impl std::io::Write,
|
||||
) -> Result<(), ShellError> {
|
||||
serde_json::to_writer(writer, plugin_response)
|
||||
.map_err(|err| ShellError::PluginFailedToEncode(err.to_string()))
|
||||
}
|
||||
|
||||
fn decode_response(
|
||||
&self,
|
||||
reader: &mut impl std::io::BufRead,
|
||||
) -> Result<PluginResponse, ShellError> {
|
||||
serde_json::from_reader(reader)
|
||||
.map_err(|err| ShellError::PluginFailedToEncode(err.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::protocol::{CallInfo, EvaluatedCall, LabeledError, PluginCall, PluginResponse};
|
||||
use nu_protocol::{Signature, Span, Spanned, SyntaxShape, Value};
|
||||
|
||||
#[test]
|
||||
fn callinfo_round_trip_signature() {
|
||||
let plugin_call = PluginCall::Signature;
|
||||
let encoder = JsonSerializer {};
|
||||
|
||||
let mut buffer: Vec<u8> = Vec::new();
|
||||
encoder
|
||||
.encode_call(&plugin_call, &mut buffer)
|
||||
.expect("unable to serialize message");
|
||||
let returned = encoder
|
||||
.decode_call(&mut buffer.as_slice())
|
||||
.expect("unable to deserialize message");
|
||||
|
||||
match returned {
|
||||
PluginCall::Signature => {}
|
||||
PluginCall::CallInfo(_) => panic!("decoded into wrong value"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn callinfo_round_trip_callinfo() {
|
||||
let name = "test".to_string();
|
||||
|
||||
let input = Value::Bool {
|
||||
val: false,
|
||||
span: Span { start: 1, end: 20 },
|
||||
};
|
||||
|
||||
let call = EvaluatedCall {
|
||||
head: Span { start: 0, end: 10 },
|
||||
positional: vec![
|
||||
Value::Float {
|
||||
val: 1.0,
|
||||
span: Span { start: 0, end: 10 },
|
||||
},
|
||||
Value::String {
|
||||
val: "something".into(),
|
||||
span: Span { start: 0, end: 10 },
|
||||
},
|
||||
],
|
||||
named: vec![(
|
||||
Spanned {
|
||||
item: "name".to_string(),
|
||||
span: Span { start: 0, end: 10 },
|
||||
},
|
||||
Some(Value::Float {
|
||||
val: 1.0,
|
||||
span: Span { start: 0, end: 10 },
|
||||
}),
|
||||
)],
|
||||
};
|
||||
|
||||
let plugin_call = PluginCall::CallInfo(Box::new(CallInfo {
|
||||
name: name.clone(),
|
||||
call: call.clone(),
|
||||
input: input.clone(),
|
||||
}));
|
||||
|
||||
let encoder = JsonSerializer {};
|
||||
let mut buffer: Vec<u8> = Vec::new();
|
||||
encoder
|
||||
.encode_call(&plugin_call, &mut buffer)
|
||||
.expect("unable to serialize message");
|
||||
let returned = encoder
|
||||
.decode_call(&mut buffer.as_slice())
|
||||
.expect("unable to deserialize message");
|
||||
|
||||
match returned {
|
||||
PluginCall::Signature => panic!("returned wrong call type"),
|
||||
PluginCall::CallInfo(call_info) => {
|
||||
assert_eq!(name, call_info.name);
|
||||
assert_eq!(input, call_info.input);
|
||||
assert_eq!(call.head, call_info.call.head);
|
||||
assert_eq!(call.positional.len(), call_info.call.positional.len());
|
||||
|
||||
call.positional
|
||||
.iter()
|
||||
.zip(call_info.call.positional.iter())
|
||||
.for_each(|(lhs, rhs)| assert_eq!(lhs, rhs));
|
||||
|
||||
call.named
|
||||
.iter()
|
||||
.zip(call_info.call.named.iter())
|
||||
.for_each(|(lhs, rhs)| {
|
||||
// Comparing the keys
|
||||
assert_eq!(lhs.0.item, rhs.0.item);
|
||||
|
||||
match (&lhs.1, &rhs.1) {
|
||||
(None, None) => {}
|
||||
(Some(a), Some(b)) => assert_eq!(a, b),
|
||||
_ => panic!("not matching values"),
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn response_round_trip_signature() {
|
||||
let signature = Signature::build("nu-plugin")
|
||||
.required("first", SyntaxShape::String, "first required")
|
||||
.required("second", SyntaxShape::Int, "second required")
|
||||
.required_named("first_named", SyntaxShape::String, "first named", Some('f'))
|
||||
.required_named(
|
||||
"second_named",
|
||||
SyntaxShape::String,
|
||||
"second named",
|
||||
Some('s'),
|
||||
)
|
||||
.rest("remaining", SyntaxShape::Int, "remaining");
|
||||
|
||||
let response = PluginResponse::Signature(vec![signature.clone()]);
|
||||
|
||||
let encoder = JsonSerializer {};
|
||||
let mut buffer: Vec<u8> = Vec::new();
|
||||
encoder
|
||||
.encode_response(&response, &mut buffer)
|
||||
.expect("unable to serialize message");
|
||||
let returned = encoder
|
||||
.decode_response(&mut buffer.as_slice())
|
||||
.expect("unable to deserialize message");
|
||||
|
||||
match returned {
|
||||
PluginResponse::Error(_) => panic!("returned wrong call type"),
|
||||
PluginResponse::Value(_) => panic!("returned wrong call type"),
|
||||
PluginResponse::Signature(returned_signature) => {
|
||||
assert!(returned_signature.len() == 1);
|
||||
assert_eq!(signature.name, returned_signature[0].name);
|
||||
assert_eq!(signature.usage, returned_signature[0].usage);
|
||||
assert_eq!(signature.extra_usage, returned_signature[0].extra_usage);
|
||||
assert_eq!(signature.is_filter, returned_signature[0].is_filter);
|
||||
|
||||
signature
|
||||
.required_positional
|
||||
.iter()
|
||||
.zip(returned_signature[0].required_positional.iter())
|
||||
.for_each(|(lhs, rhs)| assert_eq!(lhs, rhs));
|
||||
|
||||
signature
|
||||
.optional_positional
|
||||
.iter()
|
||||
.zip(returned_signature[0].optional_positional.iter())
|
||||
.for_each(|(lhs, rhs)| assert_eq!(lhs, rhs));
|
||||
|
||||
signature
|
||||
.named
|
||||
.iter()
|
||||
.zip(returned_signature[0].named.iter())
|
||||
.for_each(|(lhs, rhs)| assert_eq!(lhs, rhs));
|
||||
|
||||
assert_eq!(
|
||||
signature.rest_positional,
|
||||
returned_signature[0].rest_positional,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn response_round_trip_value() {
|
||||
let value = Value::Int {
|
||||
val: 10,
|
||||
span: Span { start: 2, end: 30 },
|
||||
};
|
||||
|
||||
let response = PluginResponse::Value(Box::new(value.clone()));
|
||||
|
||||
let encoder = JsonSerializer {};
|
||||
let mut buffer: Vec<u8> = Vec::new();
|
||||
encoder
|
||||
.encode_response(&response, &mut buffer)
|
||||
.expect("unable to serialize message");
|
||||
let returned = encoder
|
||||
.decode_response(&mut buffer.as_slice())
|
||||
.expect("unable to deserialize message");
|
||||
|
||||
match returned {
|
||||
PluginResponse::Error(_) => panic!("returned wrong call type"),
|
||||
PluginResponse::Signature(_) => panic!("returned wrong call type"),
|
||||
PluginResponse::Value(returned_value) => {
|
||||
assert_eq!(&value, returned_value.as_ref())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn response_round_trip_error() {
|
||||
let error = LabeledError {
|
||||
label: "label".into(),
|
||||
msg: "msg".into(),
|
||||
span: Some(Span { start: 2, end: 30 }),
|
||||
};
|
||||
let response = PluginResponse::Error(error.clone());
|
||||
|
||||
let encoder = JsonSerializer {};
|
||||
let mut buffer: Vec<u8> = Vec::new();
|
||||
encoder
|
||||
.encode_response(&response, &mut buffer)
|
||||
.expect("unable to serialize message");
|
||||
let returned = encoder
|
||||
.decode_response(&mut buffer.as_slice())
|
||||
.expect("unable to deserialize message");
|
||||
|
||||
match returned {
|
||||
PluginResponse::Error(msg) => assert_eq!(error, msg),
|
||||
PluginResponse::Signature(_) => panic!("returned wrong call type"),
|
||||
PluginResponse::Value(_) => panic!("returned wrong call type"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn response_round_trip_error_none() {
|
||||
let error = LabeledError {
|
||||
label: "label".into(),
|
||||
msg: "msg".into(),
|
||||
span: None,
|
||||
};
|
||||
let response = PluginResponse::Error(error.clone());
|
||||
|
||||
let encoder = JsonSerializer {};
|
||||
let mut buffer: Vec<u8> = Vec::new();
|
||||
encoder
|
||||
.encode_response(&response, &mut buffer)
|
||||
.expect("unable to serialize message");
|
||||
let returned = encoder
|
||||
.decode_response(&mut buffer.as_slice())
|
||||
.expect("unable to deserialize message");
|
||||
|
||||
match returned {
|
||||
PluginResponse::Error(msg) => assert_eq!(error, msg),
|
||||
PluginResponse::Signature(_) => panic!("returned wrong call type"),
|
||||
PluginResponse::Value(_) => panic!("returned wrong call type"),
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,6 +1,74 @@
|
||||
mod call;
|
||||
mod plugin_call;
|
||||
mod signature;
|
||||
mod value;
|
||||
use nu_protocol::ShellError;
|
||||
|
||||
pub use plugin_call::*;
|
||||
use crate::{
|
||||
plugin::PluginEncoder,
|
||||
protocol::{PluginCall, PluginResponse},
|
||||
};
|
||||
|
||||
pub mod capnp;
|
||||
pub mod json;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum EncodingType {
|
||||
Capnp(capnp::CapnpSerializer),
|
||||
Json(json::JsonSerializer),
|
||||
}
|
||||
|
||||
impl EncodingType {
|
||||
pub fn try_from_bytes(bytes: &[u8]) -> Option<Self> {
|
||||
match bytes {
|
||||
b"capnp" => Some(Self::Capnp(capnp::CapnpSerializer {})),
|
||||
b"json" => Some(Self::Json(json::JsonSerializer {})),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn encode_call(
|
||||
&self,
|
||||
plugin_call: &PluginCall,
|
||||
writer: &mut impl std::io::Write,
|
||||
) -> Result<(), ShellError> {
|
||||
match self {
|
||||
EncodingType::Capnp(encoder) => encoder.encode_call(plugin_call, writer),
|
||||
EncodingType::Json(encoder) => encoder.encode_call(plugin_call, writer),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decode_call(
|
||||
&self,
|
||||
reader: &mut impl std::io::BufRead,
|
||||
) -> Result<PluginCall, ShellError> {
|
||||
match self {
|
||||
EncodingType::Capnp(encoder) => encoder.decode_call(reader),
|
||||
EncodingType::Json(encoder) => encoder.decode_call(reader),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn encode_response(
|
||||
&self,
|
||||
plugin_response: &PluginResponse,
|
||||
writer: &mut impl std::io::Write,
|
||||
) -> Result<(), ShellError> {
|
||||
match self {
|
||||
EncodingType::Capnp(encoder) => encoder.encode_response(plugin_response, writer),
|
||||
EncodingType::Json(encoder) => encoder.encode_response(plugin_response, writer),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decode_response(
|
||||
&self,
|
||||
reader: &mut impl std::io::BufRead,
|
||||
) -> Result<PluginResponse, ShellError> {
|
||||
match self {
|
||||
EncodingType::Capnp(encoder) => encoder.decode_response(reader),
|
||||
EncodingType::Json(encoder) => encoder.decode_response(reader),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_str(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Capnp(_) => "capnp",
|
||||
Self::Json(_) => "json",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user