call and response serializers

This commit is contained in:
Fernando Herrera 2021-10-31 08:17:01 +00:00
parent 37f7a36123
commit a390f66dbf
18 changed files with 680 additions and 274 deletions

1
Cargo.lock generated
View File

@ -685,6 +685,7 @@ name = "nu-parser"
version = "0.1.0"
dependencies = [
"miette",
"nu-plugin",
"nu-protocol",
"thiserror",
]

View File

@ -8,6 +8,7 @@ mod hide;
mod if_;
mod let_;
mod module;
mod register;
mod source;
mod use_;
@ -21,5 +22,6 @@ pub use hide::Hide;
pub use if_::If;
pub use let_::Let;
pub use module::Module;
pub use register::Register;
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

@ -58,6 +58,7 @@ pub fn create_default_context() -> EngineState {
Mv,
ParEach,
Ps,
Register,
Rm,
RunPlugin,
Select,

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

@ -174,4 +174,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;
use nu_protocol::{
ast::{Block, Call, Expr, Expression, ImportPatternMember, Pipeline, Statement},
engine::StateWorkingSet,
@ -854,3 +855,78 @@ 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);
// get signature from plugin
// create plugin command declaration (need struct impl Command)
// store declaration in working set
match get_signature(source_file) {
Err(err) => Some(ParseError::PluginError(format!("{}", err))),
Ok(_signature) => None,
}
} 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)]
@ -2987,6 +2988,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

@ -6,10 +6,17 @@
# required to comunicate with the plugins created for nushell
# Generic structs used as helpers for the encoding
struct Option(Value) {
struct Option(T) {
union {
none @0 :Void;
some @1 :Value;
some @1 :T;
}
}
struct Err(T) {
union {
err @0 :Text;
ok @1 :T;
}
}
@ -27,6 +34,7 @@ struct Span {
end @1 :UInt64;
}
# Resulting value from plugin
struct Value {
span @0: Span;
@ -40,6 +48,7 @@ struct Value {
}
}
# Structs required to define the plugin signature
struct Signature {
name @0 :Text;
usage @1 :Text;
@ -76,6 +85,7 @@ enum Shape {
boolean @5;
}
# The next structs define the call information sent to th plugin
struct Expression {
union {
garbage @0 :Void;
@ -100,3 +110,18 @@ 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 {
signature @0 :Signature;
value @1 :Value;
}
}

View File

@ -1,4 +1,5 @@
pub mod plugin;
pub mod plugin_call;
pub mod serializers;
pub mod plugin_capnp {

View File

@ -1,4 +1,91 @@
use std::process::{Command, Stdio};
use std::{fmt::Display, path::Path};
use nu_protocol::{ast::Call, Signature, Value};
//use nu_protocol::{ShellError, Value};
#[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 {
Signature(Box<Signature>),
Value(Box<Value>),
}
/// The `Plugin` trait defines the API which plugins may use to "hook" into nushell.
pub trait Plugin {}
#[derive(Debug)]
pub enum PluginError {
MissingSignature,
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::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<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 signature
// send message to plugin
// deserialize message with signature
match child.wait() {
Err(err) => Err(PluginError::UnableToSpawn(format!("{}", err))),
Ok(_) => Ok(Signature::build("testing")),
}
}
}
}
fn create_command(path: &Path) -> Command {
//TODO. The selection of shell could be modifiable from the config file.
if cfg!(windows) {
let mut process = Command::new("cmd");
process.arg("/c");
process.arg(path);
process
} else {
let mut process = Command::new("sh");
process.arg("-c").arg(path);
process
}
}

View File

@ -0,0 +1,310 @@
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::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::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::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::Signature(_) => panic!("returned wrong call type"),
PluginResponse::Value(returned_value) => {
assert_eq!(&value, returned_value.as_ref())
}
}
}
}

View File

@ -1,21 +1,11 @@
use crate::plugin::PluginError;
use crate::plugin_capnp::{call, expression, option};
use capnp::serialize_packed;
use nu_protocol::{
ast::{Call, Expr, Expression},
ShellError, Span, Spanned, Type,
Span, Spanned, Type,
};
pub fn write_buffer(call: &Call, writer: &mut impl std::io::Write) -> Result<(), ShellError> {
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| ShellError::EncodingError(e.to_string()))
}
pub(crate) fn serialize_call(call: &Call, mut builder: call::Builder) -> Result<(), ShellError> {
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);
@ -37,7 +27,7 @@ fn serialize_positional(positional: &[Expression], mut builder: call::Builder) {
fn serialize_named(
named: &[(Spanned<String>, Option<Expression>)],
mut builder: call::Builder,
) -> Result<(), ShellError> {
) -> Result<(), PluginError> {
let mut named_builder = builder
.reborrow()
.init_named()
@ -48,7 +38,7 @@ fn serialize_named(
entry_builder
.reborrow()
.set_key(key.item.as_str())
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
let mut value_builder = entry_builder.init_value();
match expression {
@ -84,21 +74,10 @@ fn serialize_expression(expression: &Expression, mut builder: expression::Builde
}
}
pub fn read_buffer(reader: &mut impl std::io::BufRead) -> Result<Call, ShellError> {
let message_reader =
serialize_packed::read_message(reader, ::capnp::message::ReaderOptions::new()).unwrap();
let reader = message_reader
.get_root::<call::Reader>()
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
deserialize_call(reader)
}
pub(crate) fn deserialize_call(reader: call::Reader) -> Result<Call, ShellError> {
pub(crate) fn deserialize_call(reader: call::Reader) -> Result<Call, PluginError> {
let head_reader = reader
.get_head()
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
let head = Span {
start: head_reader.get_start() as usize,
@ -119,10 +98,10 @@ pub(crate) fn deserialize_call(reader: call::Reader) -> Result<Call, ShellError>
fn deserialize_positionals(
span: Span,
reader: call::Reader,
) -> Result<Vec<Expression>, ShellError> {
) -> Result<Vec<Expression>, PluginError> {
let positional_reader = reader
.get_positional()
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
positional_reader
.iter()
@ -132,14 +111,14 @@ fn deserialize_positionals(
type NamedList = Vec<(Spanned<String>, Option<Expression>)>;
fn deserialize_named(span: Span, reader: call::Reader) -> Result<NamedList, ShellError> {
fn deserialize_named(span: Span, reader: call::Reader) -> Result<NamedList, PluginError> {
let named_reader = reader
.get_named()
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
let entries_list = named_reader
.get_entries()
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
let mut entries: Vec<(Spanned<String>, Option<Expression>)> =
Vec::with_capacity(entries_list.len() as usize);
@ -147,21 +126,21 @@ fn deserialize_named(span: Span, reader: call::Reader) -> Result<NamedList, Shel
for entry_reader in entries_list {
let item = entry_reader
.get_key()
.map_err(|e| ShellError::DecodingError(e.to_string()))?
.map_err(|e| PluginError::DecodingError(e.to_string()))?
.to_string();
let value_reader = entry_reader
.get_value()
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
.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| ShellError::DecodingError(e.to_string()))?;
expression_reader.map_err(|e| PluginError::DecodingError(e.to_string()))?;
let expression = deserialize_expression(span, expression_reader)
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
Some(expression)
}
@ -179,7 +158,7 @@ fn deserialize_named(span: Span, reader: call::Reader) -> Result<NamedList, Shel
fn deserialize_expression(
span: Span,
reader: expression::Reader,
) -> Result<Expression, ShellError> {
) -> Result<Expression, PluginError> {
let expr = match reader.which() {
Ok(expression::Garbage(())) => Expr::Garbage,
Ok(expression::Bool(val)) => Expr::Bool(val),
@ -187,18 +166,18 @@ fn deserialize_expression(
Ok(expression::Float(val)) => Expr::Float(val),
Ok(expression::String(val)) => {
let string = val
.map_err(|e| ShellError::DecodingError(e.to_string()))?
.map_err(|e| PluginError::DecodingError(e.to_string()))?
.to_string();
Expr::String(string)
}
Ok(expression::List(values)) => {
let values = values.map_err(|e| ShellError::DecodingError(e.to_string()))?;
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>, ShellError>>()?;
.collect::<Result<Vec<Expression>, PluginError>>()?;
Expr::List(values_list)
}
@ -215,6 +194,7 @@ fn deserialize_expression(
#[cfg(test)]
mod tests {
use capnp::serialize_packed;
use core::panic;
use super::*;
@ -223,6 +203,27 @@ mod tests {
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),

View File

@ -1,147 +0,0 @@
use super::{call, value};
use crate::plugin_capnp::call_info;
use capnp::serialize_packed;
use nu_protocol::{ast::Call, ShellError, Value};
#[derive(Debug)]
pub struct CallInfo {
pub call: Call,
pub input: Value,
}
pub fn write_buffer(
call: &Call,
input: &Value,
writer: &mut impl std::io::Write,
) -> Result<(), ShellError> {
let mut message = ::capnp::message::Builder::new_default();
let mut builder = message.init_root::<call_info::Builder>();
let value_builder = builder
.reborrow()
.get_input()
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
value::serialize_value(input, value_builder);
let call_builder = builder
.reborrow()
.get_call()
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
call::serialize_call(call, call_builder)
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
serialize_packed::write_message(writer, &message)
.map_err(|e| ShellError::EncodingError(e.to_string()))
}
pub fn read_buffer(reader: &mut impl std::io::BufRead) -> Result<CallInfo, ShellError> {
let message_reader =
serialize_packed::read_message(reader, ::capnp::message::ReaderOptions::new()).unwrap();
let reader = message_reader
.get_root::<call_info::Reader>()
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
let call_reader = reader
.get_call()
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
let call = call::deserialize_call(call_reader)
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
let value_reader = reader
.get_input()
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
let input = value::deserialize_value(value_reader)
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
Ok(CallInfo { call, input })
}
#[cfg(test)]
mod tests {
use super::*;
use nu_protocol::{
ast::{Call, Expr, Expression},
Span, Spanned, 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() {
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 mut buffer: Vec<u8> = Vec::new();
write_buffer(&call, &input, &mut buffer).expect("unable to serialize message");
let call_info = read_buffer(&mut buffer.as_slice()).expect("unable to read message");
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"),
}
});
}
}

View File

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

View File

@ -1,20 +1,6 @@
use crate::plugin::PluginError;
use crate::plugin_capnp::{argument, flag, option, signature, Shape};
use capnp::serialize_packed;
use nu_protocol::{Flag, PositionalArg, ShellError, Signature, SyntaxShape};
pub fn write_buffer(
signature: &Signature,
writer: &mut impl std::io::Write,
) -> Result<(), ShellError> {
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| ShellError::EncodingError(e.to_string()))
}
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());
@ -100,59 +86,48 @@ fn serialize_flag(arg: &Flag, mut builder: flag::Builder) {
}
}
pub fn read_buffer(reader: &mut impl std::io::BufRead) -> Result<Signature, ShellError> {
let message_reader =
serialize_packed::read_message(reader, ::capnp::message::ReaderOptions::new()).unwrap();
let reader = message_reader
.get_root::<signature::Reader>()
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
deserialize_signature(reader)
}
pub(crate) fn deserialize_signature(reader: signature::Reader) -> Result<Signature, ShellError> {
pub(crate) fn deserialize_signature(reader: signature::Reader) -> Result<Signature, PluginError> {
let name = reader
.get_name()
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
let usage = reader
.get_usage()
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
let extra_usage = reader
.get_extra_usage()
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
.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| ShellError::EncodingError(e.to_string()))?;
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
let required_positional = required_list
.iter()
.map(deserialize_argument)
.collect::<Result<Vec<PositionalArg>, ShellError>>()?;
.collect::<Result<Vec<PositionalArg>, PluginError>>()?;
// Deserializing optional arguments
let optional_list = reader
.get_optional_positional()
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
let optional_positional = optional_list
.iter()
.map(deserialize_argument)
.collect::<Result<Vec<PositionalArg>, ShellError>>()?;
.collect::<Result<Vec<PositionalArg>, PluginError>>()?;
// Deserializing rest arguments
let rest_option = reader
.get_rest()
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
.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| ShellError::EncodingError(e.to_string()))?;
let rest_reader = rest_reader.map_err(|e| PluginError::EncodingError(e.to_string()))?;
Some(deserialize_argument(rest_reader)?)
}
};
@ -160,12 +135,12 @@ pub(crate) fn deserialize_signature(reader: signature::Reader) -> Result<Signatu
// Deserializing named arguments
let named_list = reader
.get_named()
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
let named = named_list
.iter()
.map(deserialize_flag)
.collect::<Result<Vec<Flag>, ShellError>>()?;
.collect::<Result<Vec<Flag>, PluginError>>()?;
Ok(Signature {
name: name.to_string(),
@ -180,18 +155,18 @@ pub(crate) fn deserialize_signature(reader: signature::Reader) -> Result<Signatu
})
}
fn deserialize_argument(reader: argument::Reader) -> Result<PositionalArg, ShellError> {
fn deserialize_argument(reader: argument::Reader) -> Result<PositionalArg, PluginError> {
let name = reader
.get_name()
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
let desc = reader
.get_desc()
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
let shape = reader
.get_shape()
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
let shape = match shape {
Shape::String => SyntaxShape::String,
@ -210,33 +185,33 @@ fn deserialize_argument(reader: argument::Reader) -> Result<PositionalArg, Shell
})
}
fn deserialize_flag(reader: flag::Reader) -> Result<Flag, ShellError> {
fn deserialize_flag(reader: flag::Reader) -> Result<Flag, PluginError> {
let long = reader
.get_long()
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
let desc = reader
.get_desc()
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
let required = reader.get_required();
let short = reader
.get_short()
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
.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| ShellError::EncodingError(e.to_string()))?;
let reader = reader.map_err(|e| PluginError::EncodingError(e.to_string()))?;
reader.chars().next()
}
};
let arg = reader
.get_arg()
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
let arg = match arg {
Shape::None => None,
@ -260,8 +235,34 @@ fn deserialize_flag(reader: flag::Reader) -> Result<Flag, ShellError> {
#[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")

View File

@ -1,20 +1,6 @@
use crate::plugin::PluginError;
use crate::plugin_capnp::value;
use capnp::serialize_packed;
use nu_protocol::{ShellError, Span, Value};
pub fn write_buffer(value: &Value, writer: &mut impl std::io::Write) -> Result<(), ShellError> {
let mut message = ::capnp::message::Builder::new_default();
let mut builder = message.init_root::<value::Builder>();
let value_span = serialize_value(value, builder.reborrow());
let mut span = builder.reborrow().init_span();
span.set_start(value_span.start as u64);
span.set_end(value_span.end as u64);
serialize_packed::write_message(writer, &message)
.map_err(|e| ShellError::EncodingError(e.to_string()))
}
use nu_protocol::{Span, Value};
pub(crate) fn serialize_value(value: &Value, mut builder: value::Builder) -> Span {
match value {
@ -55,21 +41,10 @@ pub(crate) fn serialize_value(value: &Value, mut builder: value::Builder) -> Spa
}
}
pub fn read_buffer(reader: &mut impl std::io::BufRead) -> Result<Value, ShellError> {
let message_reader =
serialize_packed::read_message(reader, ::capnp::message::ReaderOptions::new()).unwrap();
let reader = message_reader
.get_root::<value::Reader>()
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
deserialize_value(reader.reborrow())
}
pub(crate) fn deserialize_value(reader: value::Reader) -> Result<Value, ShellError> {
pub(crate) fn deserialize_value(reader: value::Reader) -> Result<Value, PluginError> {
let span_reader = reader
.get_span()
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
let span = Span {
start: span_reader.get_start() as usize,
@ -83,17 +58,17 @@ pub(crate) fn deserialize_value(reader: value::Reader) -> Result<Value, ShellErr
Ok(value::Float(val)) => Ok(Value::Float { val, span }),
Ok(value::String(val)) => {
let string = val
.map_err(|e| ShellError::DecodingError(e.to_string()))?
.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| ShellError::DecodingError(e.to_string()))?;
let values = vals.map_err(|e| PluginError::DecodingError(e.to_string()))?;
let values_list = values
.iter()
.map(deserialize_value)
.collect::<Result<Vec<Value>, ShellError>>()?;
.collect::<Result<Vec<Value>, PluginError>>()?;
Ok(Value::List {
vals: values_list,
@ -109,8 +84,37 @@ pub(crate) fn deserialize_value(reader: value::Reader) -> Result<Value, ShellErr
#[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>();
let value_span = serialize_value(value, builder.reborrow());
let mut span = builder.reborrow().init_span();
span.set_start(value_span.start as u64);
span.set_end(value_span.end as u64);
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 = [