Merge pull request #270 from elferherrera/plugins

Plugins for engine q
This commit is contained in:
JT 2021-11-02 19:07:45 +13:00 committed by GitHub
commit 7b2116dc29
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 6010 additions and 3 deletions

26
Cargo.lock generated
View file

@ -128,6 +128,21 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c58ec36aac5066d5ca17df51b3e70279f5670a72102f5752cb7e7c856adfc70"
[[package]]
name = "capnp"
version = "0.14.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae9b8a7119420b5279ddc2b4ee553ee15bcf4605df6135a26f03ffe153bee97c"
[[package]]
name = "capnpc"
version = "0.14.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b47bce811162518b5c38f746ed584bd2922ae7bb560ef64f230d2e4ee0d111fe"
dependencies = [
"capnp",
]
[[package]]
name = "cc"
version = "1.0.71"
@ -653,6 +668,7 @@ dependencies = [
"nu-json",
"nu-parser",
"nu-path",
"nu-plugin",
"nu-protocol",
"nu-table",
"nu-term-grid",
@ -694,6 +710,7 @@ name = "nu-parser"
version = "0.1.0"
dependencies = [
"miette",
"nu-plugin",
"nu-protocol",
"thiserror",
]
@ -706,6 +723,15 @@ dependencies = [
"dunce",
]
[[package]]
name = "nu-plugin"
version = "0.1.0"
dependencies = [
"capnp",
"capnpc",
"nu-protocol",
]
[[package]]
name = "nu-protocol"
version = "0.1.0"

View file

@ -6,7 +6,14 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[workspace]
members = ["crates/nu-cli", "crates/nu-engine", "crates/nu-parser", "crates/nu-command", "crates/nu-protocol"]
members = [
"crates/nu-cli",
"crates/nu-engine",
"crates/nu-parser",
"crates/nu-command",
"crates/nu-protocol",
"crates/nu-plugin",
]
[dependencies]
reedline = { git = "https://github.com/nushell/reedline", branch = "main" }

View file

@ -13,6 +13,7 @@ nu-protocol = { path = "../nu-protocol" }
nu-table = { path = "../nu-table" }
nu-term-grid = { path = "../nu-term-grid" }
nu-parser = { path = "../nu-parser" }
nu-plugin = { path = "../nu-plugin" }
trash = { version = "1.3.0", optional = true }
unicode-segmentation = "1.8.0"

View file

@ -9,6 +9,8 @@ mod hide;
mod if_;
mod let_;
mod module;
mod register;
mod run_plugin;
mod source;
mod use_;
@ -23,5 +25,7 @@ pub use hide::Hide;
pub use if_::If;
pub use let_::Let;
pub use module::Module;
pub use register::Register;
pub use run_plugin::RunPlugin;
pub use source::Source;
pub use use_::Use;

View file

@ -0,0 +1,34 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{PipelineData, Signature, SyntaxShape};
#[derive(Clone)]
pub struct Register;
impl Command for Register {
fn name(&self) -> &str {
"register"
}
fn usage(&self) -> &str {
"Register a plugin"
}
fn signature(&self) -> nu_protocol::Signature {
Signature::build("register").required(
"plugin",
SyntaxShape::Filepath,
"location of bin for plugin",
)
}
fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
_call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(PipelineData::new())
}
}

View file

@ -0,0 +1,30 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{PipelineData, Signature};
#[derive(Clone)]
pub struct RunPlugin;
impl Command for RunPlugin {
fn name(&self) -> &str {
"run_plugin"
}
fn usage(&self) -> &str {
"test for plugin encoding"
}
fn signature(&self) -> nu_protocol::Signature {
Signature::build("run_plugin")
}
fn run(
&self,
_context: &EngineState,
_stack: &mut Stack,
_call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(PipelineData::new())
}
}

View file

@ -72,8 +72,10 @@ pub fn create_default_context() -> EngineState {
Mv,
ParEach,
Ps,
Register,
Range,
Rm,
RunPlugin,
Select,
Size,
Split,

View file

@ -7,3 +7,4 @@ edition = "2018"
miette = "3.0.0"
thiserror = "1.0.29"
nu-protocol = { path = "../nu-protocol"}
nu-plugin = { path = "../nu-plugin"}

View file

@ -178,4 +178,12 @@ pub enum ParseError {
#[error("Module export not found.")]
#[diagnostic(code(nu::parser::export_not_found), url(docsrs))]
ExportNotFound(#[label = "could not find imports"] Span),
#[error("File not found")]
#[diagnostic(code(nu::parser::export_not_found), url(docsrs))]
FileNotFound(String),
#[error("Plugin error")]
#[diagnostic(code(nu::parser::export_not_found), url(docsrs))]
PluginError(String),
}

View file

@ -11,6 +11,6 @@ pub use flatten::{flatten_block, FlatShape};
pub use lex::{lex, Token, TokenContents};
pub use lite_parse::{lite_parse, LiteBlock};
pub use parse_keywords::{
parse_alias, parse_def, parse_def_predecl, parse_let, parse_module, parse_use,
parse_alias, parse_def, parse_def_predecl, parse_let, parse_module, parse_plugin, parse_use,
};
pub use parser::{find_captures_in_expr, parse, Import, VarDecl};

View file

@ -1,3 +1,4 @@
use nu_plugin::plugin::{get_signature, PluginDeclaration};
use nu_protocol::{
ast::{Block, Call, Expr, Expression, ImportPattern, ImportPatternMember, Pipeline, Statement},
engine::StateWorkingSet,
@ -923,3 +924,87 @@ pub fn parse_source(
)),
)
}
pub fn parse_plugin(
working_set: &mut StateWorkingSet,
spans: &[Span],
) -> (Statement, Option<ParseError>) {
let name = working_set.get_span_contents(spans[0]);
if name != b"register" {
return (
garbage_statement(spans),
Some(ParseError::UnknownState(
"internal error: Wrong call name for parse plugin function".into(),
span(spans),
)),
);
}
if let Some(decl_id) = working_set.find_decl(b"register") {
let (call, call_span, mut err) =
parse_internal_call(working_set, spans[0], &spans[1..], decl_id);
let error = {
match spans.len() {
1 => Some(ParseError::MissingPositional(
"plugin location".into(),
spans[0],
)),
2 => {
let name_expr = working_set.get_span_contents(spans[1]);
if let Ok(filename) = String::from_utf8(name_expr.to_vec()) {
let source_file = Path::new(&filename);
if source_file.exists() & source_file.is_file() {
// get signature from plugin
match get_signature(source_file) {
Err(err) => Some(ParseError::PluginError(format!("{}", err))),
Ok(signature) => {
// create plugin command declaration (need struct impl Command)
// store declaration in working set
let plugin_decl = PluginDeclaration::new(filename, signature);
working_set.add_decl(Box::new(plugin_decl));
None
}
}
} else {
Some(ParseError::FileNotFound(filename))
}
} else {
Some(ParseError::NonUtf8(spans[1]))
}
}
_ => {
let span = spans[2..].iter().fold(spans[2], |acc, next| Span {
start: acc.start,
end: next.end,
});
Some(ParseError::ExtraPositional(span))
}
}
};
err = error.or(err);
(
Statement::Pipeline(Pipeline::from_vec(vec![Expression {
expr: Expr::Call(call),
span: call_span,
ty: Type::Unknown,
custom_completion: None,
}])),
err,
)
} else {
(
garbage_statement(spans),
Some(ParseError::UnknownState(
"internal error: Register declaration not found".into(),
span(spans),
)),
)
}
}

View file

@ -15,7 +15,8 @@ use nu_protocol::{
};
use crate::parse_keywords::{
parse_alias, parse_def, parse_def_predecl, parse_hide, parse_let, parse_module, parse_use,
parse_alias, parse_def, parse_def_predecl, parse_hide, parse_let, parse_module, parse_plugin,
parse_use,
};
#[derive(Debug, Clone)]
@ -3006,6 +3007,7 @@ pub fn parse_statement(
Some(ParseError::UnexpectedKeyword("export".into(), spans[0])),
),
b"hide" => parse_hide(working_set, spans),
b"register" => parse_plugin(working_set, spans),
_ => {
let (expr, err) = parse_expression(working_set, spans, true);
(Statement::Pipeline(Pipeline::from_vec(vec![expr])), err)

View file

@ -0,0 +1,13 @@
[package]
name = "nu-plugin"
version = "0.1.0"
edition = "2018"
[dependencies]
capnp = "0.14.3"
nu-protocol = { path = "../nu-protocol" }
[build-dependencies]
capnpc = "0.14.3"

View file

@ -0,0 +1,132 @@
@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
# Generic structs used as helpers for the encoding
struct Option(T) {
union {
none @0 :Void;
some @1 :T;
}
}
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);
}
}
# 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);
rest @5 :Option(Argument);
named @6 :List(Flag);
isFilter @7 :Bool;
}
struct Flag {
long @0 :Text;
short @1 :Option(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;
}
# The next structs define the call information sent to th plugin
struct Expression {
union {
garbage @0 :Void;
bool @1 :Bool;
int @2 :Int64;
float @3 :Float64;
string @4 :Text;
list @5 :List(Expression);
# The expression list can be exteded based on the user need
# If a plugin requires something from the expression object, it
# will need to be added to this list
}
}
struct Call {
head @0: Span;
positional @1 :List(Expression);
named @2 :Map(Text, Option(Expression));
}
struct CallInfo {
call @0: Call;
input @1: Value;
}
# Main communication structs with the plugin
struct PluginCall {
union {
signature @0 :Void;
callInfo @1 :CallInfo;
}
}
struct PluginResponse {
union {
error @0 :Text;
signature @1 :Signature;
value @2 :Value;
}
}

View file

@ -0,0 +1,6 @@
pub mod plugin;
pub mod plugin_call;
pub mod plugin_capnp;
pub mod serializers;
pub use plugin::{serve_plugin, Plugin};

View file

@ -0,0 +1,262 @@
use crate::plugin_call::{self, decode_call, encode_response};
use std::io::BufReader;
use std::process::{Command as CommandSys, Stdio};
use std::{fmt::Display, path::Path};
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ast::Call, Signature, Value};
use nu_protocol::{PipelineData, ShellError};
const OUTPUT_BUFFER_SIZE: usize = 8192;
#[derive(Debug)]
pub struct CallInfo {
pub call: Call,
pub input: Value,
}
// Information sent to the plugin
#[derive(Debug)]
pub enum PluginCall {
Signature,
CallInfo(Box<CallInfo>),
}
// Information received from the plugin
#[derive(Debug)]
pub enum PluginResponse {
Error(String),
Signature(Box<Signature>),
Value(Box<Value>),
}
#[derive(Debug)]
pub enum PluginError {
MissingSignature,
UnableToGetStdout,
UnableToSpawn(String),
EncodingError(String),
DecodingError(String),
}
impl Display for PluginError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
PluginError::MissingSignature => write!(f, "missing signature in plugin"),
PluginError::UnableToGetStdout => write!(f, "couldn't get stdout from child process"),
PluginError::UnableToSpawn(err) => {
write!(f, "error in spawned child process: {}", err)
}
PluginError::EncodingError(err) => {
write!(f, "error while encoding: {}", err)
}
PluginError::DecodingError(err) => {
write!(f, "error while decoding: {}", err)
}
}
}
}
pub fn get_signature(path: &Path) -> Result<Box<Signature>, PluginError> {
let mut plugin_cmd = create_command(path);
// Both stdout and stdin are piped so we can get the information from the plugin
plugin_cmd.stdout(Stdio::piped());
plugin_cmd.stdin(Stdio::piped());
match plugin_cmd.spawn() {
Err(err) => Err(PluginError::UnableToSpawn(format!("{}", err))),
Ok(mut child) => {
// Create message to plugin to indicate that signature is required and
// send call to plugin asking for signature
if let Some(mut stdin_writer) = child.stdin.take() {
plugin_call::encode_call(&PluginCall::Signature, &mut stdin_writer)?
}
// deserialize response from plugin to extract the signature
let signature = if let Some(stdout_reader) = child.stdout.take() {
let mut buf_read = BufReader::with_capacity(OUTPUT_BUFFER_SIZE, stdout_reader);
let response = plugin_call::decode_response(&mut buf_read)?;
match response {
PluginResponse::Signature(sign) => Ok(sign),
PluginResponse::Error(msg) => Err(PluginError::DecodingError(msg)),
_ => Err(PluginError::DecodingError("signature not found".into())),
}
} else {
Err(PluginError::UnableToGetStdout)
}?;
match child.wait() {
Err(err) => Err(PluginError::UnableToSpawn(format!("{}", err))),
Ok(_) => Ok(signature),
}
}
}
}
fn create_command(path: &Path) -> CommandSys {
//TODO. The selection of shell could be modifiable from the config file.
if cfg!(windows) {
let mut process = CommandSys::new("cmd");
process.arg("/c");
process.arg(path);
process
} else {
let mut process = CommandSys::new("sh");
process.arg("-c").arg(path);
process
}
}
#[derive(Debug, Clone)]
pub struct PluginDeclaration {
name: String,
signature: Box<Signature>,
filename: String,
}
impl PluginDeclaration {
pub fn new(filename: String, signature: Box<Signature>) -> Self {
Self {
name: signature.name.clone(),
signature,
filename,
}
}
}
impl Command for PluginDeclaration {
fn name(&self) -> &str {
&self.name
}
fn signature(&self) -> Signature {
self.signature.as_ref().clone()
}
fn usage(&self) -> &str {
"plugin name plus arguments"
}
fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
// Call the command with self path
// Decode information from plugin
// Create PipelineData
let source_file = Path::new(&self.filename);
let mut plugin_cmd = create_command(source_file);
// Both stdout and stdin are piped so we can get the information from the plugin
plugin_cmd.stdout(Stdio::piped());
plugin_cmd.stdin(Stdio::piped());
match plugin_cmd.spawn() {
Err(err) => Err(ShellError::PluginError(format!("{}", err))),
Ok(mut child) => {
let input = match input {
PipelineData::Value(value) => value,
PipelineData::Stream(stream) => {
let values = stream.collect::<Vec<Value>>();
Value::List {
vals: values,
span: call.head,
}
}
};
// PluginCall information
let plugin_call = PluginCall::CallInfo(Box::new(CallInfo {
call: call.clone(),
input,
}));
// Create message to plugin to indicate that signature is required and
// send call to plugin asking for signature
if let Some(mut stdin_writer) = child.stdin.take() {
plugin_call::encode_call(&plugin_call, &mut stdin_writer)
.map_err(|err| ShellError::PluginError(err.to_string()))?
}
// Deserialize response from plugin to extract the resulting value
let pipeline_data = if let Some(stdout_reader) = child.stdout.take() {
let mut buf_read = BufReader::with_capacity(OUTPUT_BUFFER_SIZE, stdout_reader);
let response = plugin_call::decode_response(&mut buf_read)
.map_err(|err| ShellError::PluginError(err.to_string()))?;
match response {
PluginResponse::Value(value) => {
Ok(PipelineData::Value(value.as_ref().clone()))
}
PluginResponse::Error(msg) => Err(PluginError::DecodingError(msg)),
_ => Err(PluginError::DecodingError(
"result value from plugin not found".into(),
)),
}
} else {
Err(PluginError::UnableToGetStdout)
}
.map_err(|err| ShellError::PluginError(err.to_string()))?;
match child.wait() {
Err(err) => Err(ShellError::PluginError(format!("{}", err))),
Ok(_) => Ok(pipeline_data),
}
}
}
}
fn is_plugin(&self) -> bool {
true
}
}
/// The `Plugin` trait defines the API which plugins use to "hook" into nushell.
pub trait Plugin {
fn signature(&self) -> Signature;
fn run(&self, call: &Call, input: &Value) -> Result<Value, PluginError>;
}
// Function used in the plugin definition for the communication protocol between
// nushell and the external plugin.
// If you want to create a new plugin you have to use this function as the main
// entry point for the plugin
pub fn serve_plugin(plugin: &mut impl Plugin) {
let mut stdin_buf = BufReader::with_capacity(OUTPUT_BUFFER_SIZE, std::io::stdin());
let plugin_call = decode_call(&mut stdin_buf);
match plugin_call {
Err(err) => {
let response = PluginResponse::Error(err.to_string());
encode_response(&response, &mut std::io::stdout()).expect("Error encoding response");
}
Ok(plugin_call) => {
match plugin_call {
// Sending the signature back to nushell to create the declaration definition
PluginCall::Signature => {
let response = PluginResponse::Signature(Box::new(plugin.signature()));
encode_response(&response, &mut std::io::stdout())
.expect("Error encoding response");
}
PluginCall::CallInfo(call_info) => {
let value = plugin.run(&call_info.call, &call_info.input);
let response = match value {
Ok(value) => PluginResponse::Value(Box::new(value)),
Err(err) => PluginResponse::Error(err.to_string()),
};
encode_response(&response, &mut std::io::stdout())
.expect("Error encoding response");
}
}
}
}
}

View file

@ -0,0 +1,335 @@
use crate::plugin::{CallInfo, PluginCall, PluginError, PluginResponse};
use crate::plugin_capnp::{plugin_call, plugin_response};
use crate::serializers::{call, signature, value};
use capnp::serialize_packed;
pub fn encode_call(
plugin_call: &PluginCall,
writer: &mut impl std::io::Write,
) -> Result<(), PluginError> {
let mut message = ::capnp::message::Builder::new_default();
let mut builder = message.init_root::<plugin_call::Builder>();
match &plugin_call {
PluginCall::Signature => builder.set_signature(()),
PluginCall::CallInfo(call_info) => {
let mut call_info_builder = builder.reborrow().init_call_info();
// Serializing argument information from the call
let call_builder = call_info_builder
.reborrow()
.get_call()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
call::serialize_call(&call_info.call, call_builder)
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
// Serializing the input value from the call info
let value_builder = call_info_builder
.reborrow()
.get_input()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
value::serialize_value(&call_info.input, value_builder);
}
};
serialize_packed::write_message(writer, &message)
.map_err(|e| PluginError::EncodingError(e.to_string()))
}
pub fn decode_call(reader: &mut impl std::io::BufRead) -> Result<PluginCall, PluginError> {
let message_reader =
serialize_packed::read_message(reader, ::capnp::message::ReaderOptions::new()).unwrap();
let reader = message_reader
.get_root::<plugin_call::Reader>()
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
match reader.which() {
Err(capnp::NotInSchema(_)) => Err(PluginError::DecodingError("value not in schema".into())),
Ok(plugin_call::Signature(())) => Ok(PluginCall::Signature),
Ok(plugin_call::CallInfo(reader)) => {
let reader = reader.map_err(|e| PluginError::DecodingError(e.to_string()))?;
let call_reader = reader
.get_call()
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
let call = call::deserialize_call(call_reader)
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
let input_reader = reader
.get_input()
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
let input = value::deserialize_value(input_reader)
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
Ok(PluginCall::CallInfo(Box::new(CallInfo { call, input })))
}
}
}
pub fn encode_response(
plugin_response: &PluginResponse,
writer: &mut impl std::io::Write,
) -> Result<(), PluginError> {
let mut message = ::capnp::message::Builder::new_default();
let mut builder = message.init_root::<plugin_response::Builder>();
match &plugin_response {
PluginResponse::Error(msg) => builder.reborrow().set_error(msg.as_str()),
PluginResponse::Signature(sign) => {
let signature_builder = builder.reborrow().init_signature();
signature::serialize_signature(sign, signature_builder)
}
PluginResponse::Value(val) => {
let value_builder = builder.reborrow().init_value();
value::serialize_value(val, value_builder);
}
};
serialize_packed::write_message(writer, &message)
.map_err(|e| PluginError::EncodingError(e.to_string()))
}
pub fn decode_response(reader: &mut impl std::io::BufRead) -> Result<PluginResponse, PluginError> {
let message_reader =
serialize_packed::read_message(reader, ::capnp::message::ReaderOptions::new()).unwrap();
let reader = message_reader
.get_root::<plugin_response::Reader>()
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
match reader.which() {
Err(capnp::NotInSchema(_)) => Err(PluginError::DecodingError("value not in schema".into())),
Ok(plugin_response::Error(reader)) => {
let msg = reader.map_err(|e| PluginError::DecodingError(e.to_string()))?;
Ok(PluginResponse::Error(msg.to_string()))
}
Ok(plugin_response::Signature(reader)) => {
let reader = reader.map_err(|e| PluginError::DecodingError(e.to_string()))?;
let sign = signature::deserialize_signature(reader)
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
Ok(PluginResponse::Signature(Box::new(sign)))
}
Ok(plugin_response::Value(reader)) => {
let reader = reader.map_err(|e| PluginError::DecodingError(e.to_string()))?;
let val = value::deserialize_value(reader)
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
Ok(PluginResponse::Value(Box::new(val)))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::plugin::{PluginCall, PluginResponse};
use nu_protocol::{
ast::{Call, Expr, Expression},
Signature, Span, Spanned, SyntaxShape, Value,
};
fn compare_expressions(lhs: &Expression, rhs: &Expression) {
match (&lhs.expr, &rhs.expr) {
(Expr::Bool(a), Expr::Bool(b)) => assert_eq!(a, b),
(Expr::Int(a), Expr::Int(b)) => assert_eq!(a, b),
(Expr::Float(a), Expr::Float(b)) => assert_eq!(a, b),
(Expr::String(a), Expr::String(b)) => assert_eq!(a, b),
_ => panic!("not matching values"),
}
}
#[test]
fn callinfo_round_trip_signature() {
let plugin_call = PluginCall::Signature;
let mut buffer: Vec<u8> = Vec::new();
encode_call(&plugin_call, &mut buffer).expect("unable to serialize message");
let returned = 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 input = Value::Bool {
val: false,
span: Span { start: 1, end: 20 },
};
let call = Call {
decl_id: 1,
head: Span { start: 0, end: 10 },
positional: vec![
Expression {
expr: Expr::Float(1.0),
span: Span { start: 0, end: 10 },
ty: nu_protocol::Type::Float,
custom_completion: None,
},
Expression {
expr: Expr::String("something".into()),
span: Span { start: 0, end: 10 },
ty: nu_protocol::Type::Float,
custom_completion: None,
},
],
named: vec![(
Spanned {
item: "name".to_string(),
span: Span { start: 0, end: 10 },
},
Some(Expression {
expr: Expr::Float(1.0),
span: Span { start: 0, end: 10 },
ty: nu_protocol::Type::Float,
custom_completion: None,
}),
)],
};
let plugin_call = PluginCall::CallInfo(Box::new(CallInfo {
call: call.clone(),
input: input.clone(),
}));
let mut buffer: Vec<u8> = Vec::new();
encode_call(&plugin_call, &mut buffer).expect("unable to serialize message");
let returned = 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!(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)| compare_expressions(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)) => compare_expressions(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(Box::new(signature.clone()));
let mut buffer: Vec<u8> = Vec::new();
encode_response(&response, &mut buffer).expect("unable to serialize message");
let returned =
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_eq!(signature.name, returned_signature.name);
assert_eq!(signature.usage, returned_signature.usage);
assert_eq!(signature.extra_usage, returned_signature.extra_usage);
assert_eq!(signature.is_filter, returned_signature.is_filter);
signature
.required_positional
.iter()
.zip(returned_signature.required_positional.iter())
.for_each(|(lhs, rhs)| assert_eq!(lhs, rhs));
signature
.optional_positional
.iter()
.zip(returned_signature.optional_positional.iter())
.for_each(|(lhs, rhs)| assert_eq!(lhs, rhs));
signature
.named
.iter()
.zip(returned_signature.named.iter())
.for_each(|(lhs, rhs)| assert_eq!(lhs, rhs));
assert_eq!(
signature.rest_positional,
returned_signature.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 mut buffer: Vec<u8> = Vec::new();
encode_response(&response, &mut buffer).expect("unable to serialize message");
let returned =
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 message = "some error".to_string();
let response = PluginResponse::Error(message.clone());
let mut buffer: Vec<u8> = Vec::new();
encode_response(&response, &mut buffer).expect("unable to serialize message");
let returned =
decode_response(&mut buffer.as_slice()).expect("unable to deserialize message");
match returned {
PluginResponse::Error(msg) => assert_eq!(message, msg),
PluginResponse::Signature(_) => panic!("returned wrong call type"),
PluginResponse::Value(_) => panic!("returned wrong call type"),
}
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,296 @@
use crate::plugin::PluginError;
use crate::plugin_capnp::{call, expression, option};
use nu_protocol::{
ast::{Call, Expr, Expression},
Span, Spanned, Type,
};
pub(crate) fn serialize_call(call: &Call, mut builder: call::Builder) -> Result<(), PluginError> {
let mut head = builder.reborrow().init_head();
head.set_start(call.head.start as u64);
head.set_end(call.head.end as u64);
serialize_positional(&call.positional, builder.reborrow());
serialize_named(&call.named, builder)?;
Ok(())
}
fn serialize_positional(positional: &[Expression], mut builder: call::Builder) {
let mut positional_builder = builder.reborrow().init_positional(positional.len() as u32);
for (index, expression) in positional.iter().enumerate() {
serialize_expression(expression, positional_builder.reborrow().get(index as u32))
}
}
fn serialize_named(
named: &[(Spanned<String>, Option<Expression>)],
mut builder: call::Builder,
) -> Result<(), PluginError> {
let mut named_builder = builder
.reborrow()
.init_named()
.init_entries(named.len() as u32);
for (index, (key, expression)) in named.iter().enumerate() {
let mut entry_builder = named_builder.reborrow().get(index as u32);
entry_builder
.reborrow()
.set_key(key.item.as_str())
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
let mut value_builder = entry_builder.init_value();
match expression {
None => value_builder.set_none(()),
Some(expr) => {
let expression_builder = value_builder.init_some();
serialize_expression(expr, expression_builder);
}
}
}
Ok(())
}
fn serialize_expression(expression: &Expression, mut builder: expression::Builder) {
match &expression.expr {
Expr::Garbage => builder.set_garbage(()),
Expr::Bool(val) => builder.set_bool(*val),
Expr::Int(val) => builder.set_int(*val),
Expr::Float(val) => builder.set_float(*val),
Expr::String(val) => builder.set_string(val),
Expr::List(values) => {
let mut list_builder = builder.reborrow().init_list(values.len() as u32);
for (index, expression) in values.iter().enumerate() {
let inner_builder = list_builder.reborrow().get(index as u32);
serialize_expression(expression, inner_builder)
}
}
_ => {
// If there is the need to pass other type of argument to the plugin
// we have to define the encoding for that parameter in this match
}
}
}
pub(crate) fn deserialize_call(reader: call::Reader) -> Result<Call, PluginError> {
let head_reader = reader
.get_head()
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
let head = Span {
start: head_reader.get_start() as usize,
end: head_reader.get_end() as usize,
};
let positional = deserialize_positionals(head, reader)?;
let named = deserialize_named(head, reader)?;
Ok(Call {
decl_id: 0,
head,
positional,
named,
})
}
fn deserialize_positionals(
span: Span,
reader: call::Reader,
) -> Result<Vec<Expression>, PluginError> {
let positional_reader = reader
.get_positional()
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
positional_reader
.iter()
.map(|expression_reader| deserialize_expression(span, expression_reader))
.collect()
}
type NamedList = Vec<(Spanned<String>, Option<Expression>)>;
fn deserialize_named(span: Span, reader: call::Reader) -> Result<NamedList, PluginError> {
let named_reader = reader
.get_named()
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
let entries_list = named_reader
.get_entries()
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
let mut entries: Vec<(Spanned<String>, Option<Expression>)> =
Vec::with_capacity(entries_list.len() as usize);
for entry_reader in entries_list {
let item = entry_reader
.get_key()
.map_err(|e| PluginError::DecodingError(e.to_string()))?
.to_string();
let value_reader = entry_reader
.get_value()
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
let value = match value_reader.which() {
Ok(option::None(())) => None,
Ok(option::Some(expression_reader)) => {
let expression_reader =
expression_reader.map_err(|e| PluginError::DecodingError(e.to_string()))?;
let expression = deserialize_expression(span, expression_reader)
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
Some(expression)
}
Err(capnp::NotInSchema(_)) => None,
};
let key = Spanned { item, span };
entries.push((key, value))
}
Ok(entries)
}
fn deserialize_expression(
span: Span,
reader: expression::Reader,
) -> Result<Expression, PluginError> {
let expr = match reader.which() {
Ok(expression::Garbage(())) => Expr::Garbage,
Ok(expression::Bool(val)) => Expr::Bool(val),
Ok(expression::Int(val)) => Expr::Int(val),
Ok(expression::Float(val)) => Expr::Float(val),
Ok(expression::String(val)) => {
let string = val
.map_err(|e| PluginError::DecodingError(e.to_string()))?
.to_string();
Expr::String(string)
}
Ok(expression::List(values)) => {
let values = values.map_err(|e| PluginError::DecodingError(e.to_string()))?;
let values_list = values
.iter()
.map(|inner_reader| deserialize_expression(span, inner_reader))
.collect::<Result<Vec<Expression>, PluginError>>()?;
Expr::List(values_list)
}
Err(capnp::NotInSchema(_)) => Expr::Garbage,
};
Ok(Expression {
expr,
span,
ty: Type::Unknown,
custom_completion: None,
})
}
#[cfg(test)]
mod tests {
use capnp::serialize_packed;
use core::panic;
use super::*;
use nu_protocol::{
ast::{Call, Expr, Expression},
Span, Spanned,
};
fn write_buffer(call: &Call, writer: &mut impl std::io::Write) -> Result<(), PluginError> {
let mut message = ::capnp::message::Builder::new_default();
let builder = message.init_root::<call::Builder>();
serialize_call(call, builder)?;
serialize_packed::write_message(writer, &message)
.map_err(|e| PluginError::EncodingError(e.to_string()))
}
fn read_buffer(reader: &mut impl std::io::BufRead) -> Result<Call, PluginError> {
let message_reader =
serialize_packed::read_message(reader, ::capnp::message::ReaderOptions::new()).unwrap();
let reader = message_reader
.get_root::<call::Reader>()
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
deserialize_call(reader)
}
fn compare_expressions(lhs: &Expression, rhs: &Expression) {
match (&lhs.expr, &rhs.expr) {
(Expr::Bool(a), Expr::Bool(b)) => assert_eq!(a, b),
(Expr::Int(a), Expr::Int(b)) => assert_eq!(a, b),
(Expr::Float(a), Expr::Float(b)) => assert_eq!(a, b),
(Expr::String(a), Expr::String(b)) => assert_eq!(a, b),
_ => panic!("not matching values"),
}
}
#[test]
fn call_round_trip() {
let call = Call {
decl_id: 1,
head: Span { start: 0, end: 10 },
positional: vec![
Expression {
expr: Expr::Float(1.0),
span: Span { start: 0, end: 10 },
ty: nu_protocol::Type::Float,
custom_completion: None,
},
Expression {
expr: Expr::String("something".into()),
span: Span { start: 0, end: 10 },
ty: nu_protocol::Type::Float,
custom_completion: None,
},
],
named: vec![(
Spanned {
item: "name".to_string(),
span: Span { start: 0, end: 10 },
},
Some(Expression {
expr: Expr::Float(1.0),
span: Span { start: 0, end: 10 },
ty: nu_protocol::Type::Float,
custom_completion: None,
}),
)],
};
let mut buffer: Vec<u8> = Vec::new();
write_buffer(&call, &mut buffer).expect("unable to serialize message");
let returned_call = read_buffer(&mut buffer.as_slice()).expect("unable to read buffer");
assert_eq!(call.head, returned_call.head);
assert_eq!(call.positional.len(), returned_call.positional.len());
call.positional
.iter()
.zip(returned_call.positional.iter())
.for_each(|(lhs, rhs)| compare_expressions(lhs, rhs));
call.named
.iter()
.zip(returned_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)) => compare_expressions(a, b),
_ => panic!("not matching values"),
}
});
}
}

View file

@ -0,0 +1,3 @@
pub mod call;
pub mod signature;
pub mod value;

View file

@ -0,0 +1,313 @@
use crate::plugin::PluginError;
use crate::plugin_capnp::{argument, flag, option, signature, Shape};
use nu_protocol::{Flag, PositionalArg, Signature, SyntaxShape};
pub(crate) fn serialize_signature(signature: &Signature, mut builder: signature::Builder) {
builder.set_name(signature.name.as_str());
builder.set_usage(signature.usage.as_str());
builder.set_extra_usage(signature.extra_usage.as_str());
builder.set_is_filter(signature.is_filter);
// Serializing list of required arguments
let mut required_list = builder
.reborrow()
.init_required_positional(signature.required_positional.len() as u32);
for (index, arg) in signature.required_positional.iter().enumerate() {
let inner_builder = required_list.reborrow().get(index as u32);
serialize_argument(arg, inner_builder)
}
// Serializing list of optional arguments
let mut optional_list = builder
.reborrow()
.init_optional_positional(signature.optional_positional.len() as u32);
for (index, arg) in signature.optional_positional.iter().enumerate() {
let inner_builder = optional_list.reborrow().get(index as u32);
serialize_argument(arg, inner_builder)
}
// Serializing rest argument
let mut rest_argument = builder.reborrow().init_rest();
match &signature.rest_positional {
None => rest_argument.set_none(()),
Some(arg) => {
let inner_builder = rest_argument.init_some();
serialize_argument(arg, inner_builder)
}
}
// Serializing the named arguments
let mut named_list = builder.reborrow().init_named(signature.named.len() as u32);
for (index, arg) in signature.named.iter().enumerate() {
let inner_builder = named_list.reborrow().get(index as u32);
serialize_flag(arg, inner_builder)
}
}
fn serialize_argument(arg: &PositionalArg, mut builder: argument::Builder) {
builder.set_name(arg.name.as_str());
builder.set_desc(arg.desc.as_str());
match arg.shape {
SyntaxShape::Boolean => builder.set_shape(Shape::Boolean),
SyntaxShape::String => builder.set_shape(Shape::String),
SyntaxShape::Int => builder.set_shape(Shape::Int),
SyntaxShape::Number => builder.set_shape(Shape::Number),
_ => builder.set_shape(Shape::Any),
}
}
fn serialize_flag(arg: &Flag, mut builder: flag::Builder) {
builder.set_long(arg.long.as_str());
builder.set_required(arg.required);
builder.set_desc(arg.desc.as_str());
let mut short_builder = builder.reborrow().init_short();
match arg.short {
None => short_builder.set_none(()),
Some(val) => {
let mut inner_builder = short_builder.reborrow().initn_some(1);
inner_builder.push_str(format!("{}", val).as_str());
}
}
match &arg.arg {
None => builder.set_arg(Shape::None),
Some(shape) => match shape {
SyntaxShape::Boolean => builder.set_arg(Shape::Boolean),
SyntaxShape::String => builder.set_arg(Shape::String),
SyntaxShape::Int => builder.set_arg(Shape::Int),
SyntaxShape::Number => builder.set_arg(Shape::Number),
_ => builder.set_arg(Shape::Any),
},
}
}
pub(crate) fn deserialize_signature(reader: signature::Reader) -> Result<Signature, PluginError> {
let name = reader
.get_name()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
let usage = reader
.get_usage()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
let extra_usage = reader
.get_extra_usage()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
let is_filter = reader.get_is_filter();
// Deserializing required arguments
let required_list = reader
.get_required_positional()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
let required_positional = required_list
.iter()
.map(deserialize_argument)
.collect::<Result<Vec<PositionalArg>, PluginError>>()?;
// Deserializing optional arguments
let optional_list = reader
.get_optional_positional()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
let optional_positional = optional_list
.iter()
.map(deserialize_argument)
.collect::<Result<Vec<PositionalArg>, PluginError>>()?;
// Deserializing rest arguments
let rest_option = reader
.get_rest()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
let rest_positional = match rest_option.which() {
Err(capnp::NotInSchema(_)) => None,
Ok(option::None(())) => None,
Ok(option::Some(rest_reader)) => {
let rest_reader = rest_reader.map_err(|e| PluginError::EncodingError(e.to_string()))?;
Some(deserialize_argument(rest_reader)?)
}
};
// Deserializing named arguments
let named_list = reader
.get_named()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
let named = named_list
.iter()
.map(deserialize_flag)
.collect::<Result<Vec<Flag>, PluginError>>()?;
Ok(Signature {
name: name.to_string(),
usage: usage.to_string(),
extra_usage: extra_usage.to_string(),
required_positional,
optional_positional,
rest_positional,
named,
is_filter,
creates_scope: false,
})
}
fn deserialize_argument(reader: argument::Reader) -> Result<PositionalArg, PluginError> {
let name = reader
.get_name()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
let desc = reader
.get_desc()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
let shape = reader
.get_shape()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
let shape = match shape {
Shape::String => SyntaxShape::String,
Shape::Int => SyntaxShape::Int,
Shape::Number => SyntaxShape::Number,
Shape::Boolean => SyntaxShape::Boolean,
Shape::Any => SyntaxShape::Any,
Shape::None => SyntaxShape::Any,
};
Ok(PositionalArg {
name: name.to_string(),
desc: desc.to_string(),
shape,
var_id: None,
})
}
fn deserialize_flag(reader: flag::Reader) -> Result<Flag, PluginError> {
let long = reader
.get_long()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
let desc = reader
.get_desc()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
let required = reader.get_required();
let short = reader
.get_short()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
let short = match short.which() {
Err(capnp::NotInSchema(_)) => None,
Ok(option::None(())) => None,
Ok(option::Some(reader)) => {
let reader = reader.map_err(|e| PluginError::EncodingError(e.to_string()))?;
reader.chars().next()
}
};
let arg = reader
.get_arg()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
let arg = match arg {
Shape::None => None,
Shape::Any => Some(SyntaxShape::Any),
Shape::String => Some(SyntaxShape::String),
Shape::Int => Some(SyntaxShape::Int),
Shape::Number => Some(SyntaxShape::Number),
Shape::Boolean => Some(SyntaxShape::Boolean),
};
Ok(Flag {
long: long.to_string(),
short,
arg,
required,
desc: desc.to_string(),
var_id: None,
})
}
#[cfg(test)]
mod tests {
use super::*;
use capnp::serialize_packed;
use nu_protocol::{Signature, SyntaxShape};
pub fn write_buffer(
signature: &Signature,
writer: &mut impl std::io::Write,
) -> Result<(), PluginError> {
let mut message = ::capnp::message::Builder::new_default();
let builder = message.init_root::<signature::Builder>();
serialize_signature(signature, builder);
serialize_packed::write_message(writer, &message)
.map_err(|e| PluginError::EncodingError(e.to_string()))
}
pub fn read_buffer(reader: &mut impl std::io::BufRead) -> Result<Signature, PluginError> {
let message_reader =
serialize_packed::read_message(reader, ::capnp::message::ReaderOptions::new()).unwrap();
let reader = message_reader
.get_root::<signature::Reader>()
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
deserialize_signature(reader)
}
#[test]
fn value_round_trip() {
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 mut buffer: Vec<u8> = Vec::new();
write_buffer(&signature, &mut buffer).expect("unable to serialize message");
let returned_signature =
read_buffer(&mut buffer.as_slice()).expect("unable to deserialize message");
assert_eq!(signature.name, returned_signature.name);
assert_eq!(signature.usage, returned_signature.usage);
assert_eq!(signature.extra_usage, returned_signature.extra_usage);
assert_eq!(signature.is_filter, returned_signature.is_filter);
signature
.required_positional
.iter()
.zip(returned_signature.required_positional.iter())
.for_each(|(lhs, rhs)| assert_eq!(lhs, rhs));
signature
.optional_positional
.iter()
.zip(returned_signature.optional_positional.iter())
.for_each(|(lhs, rhs)| assert_eq!(lhs, rhs));
signature
.named
.iter()
.zip(returned_signature.named.iter())
.for_each(|(lhs, rhs)| assert_eq!(lhs, rhs));
assert_eq!(
signature.rest_positional,
returned_signature.rest_positional,
);
}
}

View file

@ -0,0 +1,265 @@
use crate::plugin::PluginError;
use crate::plugin_capnp::value;
use nu_protocol::{Span, Value};
pub(crate) fn serialize_value(value: &Value, mut builder: value::Builder) {
let value_span = match value {
Value::Nothing { span } => {
builder.set_void(());
*span
}
Value::Bool { val, span } => {
builder.set_bool(*val);
*span
}
Value::Int { val, span } => {
builder.set_int(*val);
*span
}
Value::Float { val, span } => {
builder.set_float(*val);
*span
}
Value::String { val, span } => {
builder.set_string(val);
*span
}
Value::List { vals, span } => {
let mut list_builder = builder.reborrow().init_list(vals.len() as u32);
for (index, value) in vals.iter().enumerate() {
let inner_builder = list_builder.reborrow().get(index as u32);
serialize_value(value, inner_builder);
}
*span
}
_ => {
// If there is the need to pass other type of value to the plugin
// we have to define the encoding for that object in this match
Span::unknown()
}
};
let mut span = builder.reborrow().init_span();
span.set_start(value_span.start as u64);
span.set_end(value_span.end as u64);
}
pub(crate) fn deserialize_value(reader: value::Reader) -> Result<Value, PluginError> {
let span_reader = reader
.get_span()
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
let span = Span {
start: span_reader.get_start() as usize,
end: span_reader.get_end() as usize,
};
match reader.which() {
Ok(value::Void(())) => Ok(Value::Nothing { span }),
Ok(value::Bool(val)) => Ok(Value::Bool { val, span }),
Ok(value::Int(val)) => Ok(Value::Int { val, span }),
Ok(value::Float(val)) => Ok(Value::Float { val, span }),
Ok(value::String(val)) => {
let string = val
.map_err(|e| PluginError::DecodingError(e.to_string()))?
.to_string();
Ok(Value::String { val: string, span })
}
Ok(value::List(vals)) => {
let values = vals.map_err(|e| PluginError::DecodingError(e.to_string()))?;
let values_list = values
.iter()
.map(deserialize_value)
.collect::<Result<Vec<Value>, PluginError>>()?;
Ok(Value::List {
vals: values_list,
span,
})
}
Err(capnp::NotInSchema(_)) => Ok(Value::Nothing {
span: Span::unknown(),
}),
}
}
#[cfg(test)]
mod tests {
use super::*;
use capnp::serialize_packed;
use nu_protocol::{Span, Value};
pub fn write_buffer(
value: &Value,
writer: &mut impl std::io::Write,
) -> Result<(), PluginError> {
let mut message = ::capnp::message::Builder::new_default();
let mut builder = message.init_root::<value::Builder>();
serialize_value(value, builder.reborrow());
serialize_packed::write_message(writer, &message)
.map_err(|e| PluginError::EncodingError(e.to_string()))
}
pub fn read_buffer(reader: &mut impl std::io::BufRead) -> Result<Value, PluginError> {
let message_reader =
serialize_packed::read_message(reader, ::capnp::message::ReaderOptions::new()).unwrap();
let reader = message_reader
.get_root::<value::Reader>()
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
deserialize_value(reader.reborrow())
}
#[test]
fn value_round_trip() {
let values = [
Value::Bool {
val: false,
span: Span { start: 1, end: 20 },
},
Value::Int {
val: 10,
span: Span { start: 2, end: 30 },
},
Value::Float {
val: 10.0,
span: Span { start: 3, end: 40 },
},
Value::String {
val: "a string".into(),
span: Span { start: 4, end: 50 },
},
];
for value in values {
let mut buffer: Vec<u8> = Vec::new();
write_buffer(&value, &mut buffer).expect("unable to serialize message");
let returned_value =
read_buffer(&mut buffer.as_slice()).expect("unable to deserialize message");
assert_eq!(value, returned_value)
}
}
#[test]
fn value_nothing_round_trip() {
// Since nothing doesn't implement PartialOrd, we only compare that the
// encoded and decoded spans are correct
let value = Value::Nothing {
span: Span { start: 0, end: 10 },
};
let mut buffer: Vec<u8> = Vec::new();
write_buffer(&value, &mut buffer).expect("unable to serialize message");
let returned_value =
read_buffer(&mut buffer.as_slice()).expect("unable to deserialize message");
assert_eq!(
value.span().expect("span"),
returned_value.span().expect("span")
)
}
#[test]
fn list_round_trip() {
let values = vec![
Value::Bool {
val: false,
span: Span { start: 1, end: 20 },
},
Value::Int {
val: 10,
span: Span { start: 2, end: 30 },
},
Value::Float {
val: 10.0,
span: Span { start: 3, end: 40 },
},
Value::String {
val: "a string".into(),
span: Span { start: 4, end: 50 },
},
];
let value = Value::List {
vals: values,
span: Span { start: 1, end: 10 },
};
let mut buffer: Vec<u8> = Vec::new();
write_buffer(&value, &mut buffer).expect("unable to serialize message");
let returned_value =
read_buffer(&mut buffer.as_slice()).expect("unable to deserialize message");
assert_eq!(
value.span().expect("span"),
returned_value.span().expect("span")
)
}
#[test]
fn nested_list_round_trip() {
let inner_values = vec![
Value::Bool {
val: false,
span: Span { start: 1, end: 20 },
},
Value::Int {
val: 10,
span: Span { start: 2, end: 30 },
},
Value::Float {
val: 10.0,
span: Span { start: 3, end: 40 },
},
Value::String {
val: "inner string".into(),
span: Span { start: 4, end: 50 },
},
];
let values = vec![
Value::Bool {
val: true,
span: Span { start: 1, end: 20 },
},
Value::Int {
val: 66,
span: Span { start: 2, end: 30 },
},
Value::Float {
val: 66.6,
span: Span { start: 3, end: 40 },
},
Value::String {
val: "a string".into(),
span: Span { start: 4, end: 50 },
},
Value::List {
vals: inner_values,
span: Span { start: 5, end: 60 },
},
];
let value = Value::List {
vals: values,
span: Span { start: 1, end: 10 },
};
let mut buffer: Vec<u8> = Vec::new();
write_buffer(&value, &mut buffer).expect("unable to serialize message");
let returned_value =
read_buffer(&mut buffer.as_slice()).expect("unable to deserialize message");
assert_eq!(
value.span().expect("span"),
returned_value.span().expect("span")
)
}
}

View file

@ -169,6 +169,9 @@ pub enum ShellError {
NoFileToBeMoved(),
#[error("No file to be copied")]
NoFileToBeCopied(),
#[error("Plugin error")]
PluginError(String),
}
impl From<std::io::Error> for ShellError {