engine-q merge

This commit is contained in:
Fernando Herrera
2022-02-07 19:11:34 +00:00
1965 changed files with 119062 additions and 20 deletions

View File

@ -1,48 +0,0 @@
use nu_protocol::{CallInfo, Value};
use serde::{Deserialize, Serialize};
use std::io::Write;
#[derive(Debug, Serialize, Deserialize)]
pub struct JsonRpc<T> {
jsonrpc: String,
pub method: String,
pub params: T,
}
impl<T> JsonRpc<T> {
pub fn new<U: Into<String>>(method: U, params: T) -> Self {
JsonRpc {
jsonrpc: "2.0".into(),
method: method.into(),
params,
}
}
}
pub fn send_response<T: Serialize>(result: T) {
let response = JsonRpc::new("response", result);
let response_raw = serde_json::to_string(&response);
let mut stdout = std::io::stdout();
match response_raw {
Ok(response) => {
let _ = writeln!(stdout, "{}", response);
}
Err(err) => {
let _ = writeln!(stdout, "{}", err);
}
};
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "method")]
#[allow(non_camel_case_types)]
pub enum NuCommand {
config,
begin_filter { params: CallInfo },
filter { params: Value },
end_filter,
sink { params: (CallInfo, Vec<Value>) },
quit,
}

View File

@ -1,6 +1,19 @@
<<<<<<< HEAD
pub mod jsonrpc;
mod plugin;
pub mod test_helpers;
pub use crate::plugin::{serve_plugin, Plugin};
=======
mod plugin;
mod protocol;
mod serializers;
#[allow(dead_code)]
mod plugin_capnp;
pub use plugin::{get_signature, serve_plugin, Plugin, PluginDeclaration};
pub use protocol::{EvaluatedCall, LabeledError};
pub use serializers::{capnp::CapnpSerializer, json::JsonSerializer, EncodingType};
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce

View File

@ -1,131 +0,0 @@
use crate::jsonrpc::{send_response, NuCommand};
use nu_errors::ShellError;
use nu_protocol::{CallInfo, ReturnValue, Signature, Value};
use std::io;
/// The `Plugin` trait defines the API which plugins may use to "hook" into nushell.
pub trait Plugin {
/// The `config` method is used to configure a plugin's user interface / signature.
///
/// This is where the "name" of the plugin (ex `fetch`), description, any required/optional fields, and flags
/// can be defined. This information will displayed in nushell when running help <plugin name>
fn config(&mut self) -> Result<Signature, ShellError>;
/// `begin_filter` is the first method to be called if the `Signature` of the plugin is configured to be filterable.
/// Any setup required for the plugin such as parsing arguments from `CallInfo` or initializing data structures
/// can be done here. The `CallInfo` parameter will contain data configured in the `config` method of the Plugin trait.
fn begin_filter(&mut self, _call_info: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
Ok(vec![])
}
/// `filter` is called for every `Value` that is processed by the plugin.
/// This method requires the plugin `Signature` to be configured as filterable.
fn filter(&mut self, _input: Value) -> Result<Vec<ReturnValue>, ShellError> {
Ok(vec![])
}
/// `end_filter` is the last method to be called by the plugin after all `Value`s are processed by the plugin.
/// This method requires the plugin `Signature` to be configured as filterable.
fn end_filter(&mut self) -> Result<Vec<ReturnValue>, ShellError> {
Ok(vec![])
}
/// `sink` consumes the `Value`s that are passed in, preventing further processing.
/// This method requires the plugin `Signature` to be configured without filtering.
fn sink(&mut self, _call_info: CallInfo, _input: Vec<Value>) {}
fn quit(&mut self) {}
}
pub fn serve_plugin(plugin: &mut dyn Plugin) {
let mut args = std::env::args();
if args.len() > 1 {
let input = args.nth(1);
let input = match input {
Some(arg) => std::fs::read_to_string(arg),
None => {
send_response(ShellError::untagged_runtime_error("No input given."));
return;
}
};
if let Ok(input) = input {
let command = serde_json::from_str::<NuCommand>(&input);
match command {
Ok(NuCommand::config) => {
send_response(plugin.config());
}
Ok(NuCommand::begin_filter { params }) => {
send_response(plugin.begin_filter(params));
}
Ok(NuCommand::filter { params }) => {
send_response(plugin.filter(params));
}
Ok(NuCommand::end_filter) => {
send_response(plugin.end_filter());
}
Ok(NuCommand::sink { params }) => {
plugin.sink(params.0, params.1);
}
Ok(NuCommand::quit) => {
plugin.quit();
}
e => {
send_response(ShellError::untagged_runtime_error(format!(
"Could not handle plugin message: {} {:?}",
input, e
)));
}
}
}
} else {
loop {
let mut input = String::new();
match io::stdin().read_line(&mut input) {
Ok(_) => {
let command = serde_json::from_str::<NuCommand>(&input);
match command {
Ok(NuCommand::config) => {
send_response(plugin.config());
break;
}
Ok(NuCommand::begin_filter { params }) => {
send_response(plugin.begin_filter(params));
}
Ok(NuCommand::filter { params }) => {
send_response(plugin.filter(params));
}
Ok(NuCommand::end_filter) => {
send_response(plugin.end_filter());
break;
}
Ok(NuCommand::sink { params }) => {
plugin.sink(params.0, params.1);
break;
}
Ok(NuCommand::quit) => {
plugin.quit();
break;
}
e => {
send_response(ShellError::untagged_runtime_error(format!(
"Could not handle plugin message: {} {:?}",
input, e
)));
break;
}
}
}
e => {
send_response(ShellError::untagged_runtime_error(format!(
"Could not handle plugin message: {:?}",
e,
)));
break;
}
}
}
}
}

View File

@ -0,0 +1,133 @@
use crate::{EncodingType, EvaluatedCall};
use super::{create_command, OUTPUT_BUFFER_SIZE};
use crate::protocol::{CallInfo, PluginCall, PluginResponse};
use std::io::BufReader;
use std::path::{Path, PathBuf};
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ast::Call, Signature};
use nu_protocol::{PipelineData, ShellError};
#[derive(Clone)]
pub struct PluginDeclaration {
name: String,
signature: Signature,
filename: PathBuf,
shell: Option<PathBuf>,
encoding: EncodingType,
}
impl PluginDeclaration {
pub fn new(
filename: PathBuf,
signature: Signature,
encoding: EncodingType,
shell: Option<PathBuf>,
) -> Self {
Self {
name: signature.name.clone(),
signature,
filename,
encoding,
shell,
}
}
}
impl Command for PluginDeclaration {
fn name(&self) -> &str {
&self.name
}
fn signature(&self) -> Signature {
self.signature.clone()
}
fn usage(&self) -> &str {
self.signature.usage.as_str()
}
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, &self.shell);
let mut child = plugin_cmd.spawn().map_err(|err| {
let decl = engine_state.get_decl(call.decl_id);
ShellError::SpannedLabeledError(
format!("Unable to spawn plugin for {}", decl.name()),
format!("{}", err),
call.head,
)
})?;
let input = input.into_value(call.head);
// 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() {
let encoding_clone = self.encoding.clone();
let plugin_call = PluginCall::CallInfo(Box::new(CallInfo {
name: self.name.clone(),
call: EvaluatedCall::try_from_call(call, engine_state, stack)?,
input,
}));
std::thread::spawn(move || {
// PluginCall information
encoding_clone.encode_call(&plugin_call, &mut stdin_writer)
});
}
// Deserialize response from plugin to extract the resulting value
let pipeline_data = if let Some(stdout_reader) = &mut child.stdout {
let reader = stdout_reader;
let mut buf_read = BufReader::with_capacity(OUTPUT_BUFFER_SIZE, reader);
let response = self.encoding.decode_response(&mut buf_read).map_err(|err| {
let decl = engine_state.get_decl(call.decl_id);
ShellError::SpannedLabeledError(
format!("Unable to decode call for {}", decl.name()),
err.to_string(),
call.head,
)
});
match response {
Ok(PluginResponse::Value(value)) => {
Ok(PipelineData::Value(value.as_ref().clone(), None))
}
Ok(PluginResponse::Error(err)) => Err(err.into()),
Ok(PluginResponse::Signature(..)) => Err(ShellError::SpannedLabeledError(
"Plugin missing value".into(),
"Received a signature from plugin instead of value".into(),
call.head,
)),
Err(err) => Err(err),
}
} else {
Err(ShellError::SpannedLabeledError(
"Error with stdout reader".into(),
"no stdout reader".into(),
call.head,
))
};
// We need to call .wait() on the child, or we'll risk summoning the zombie horde
let _ = child.wait();
pipeline_data
}
fn is_plugin(&self) -> Option<(&PathBuf, &str, &Option<PathBuf>)> {
Some((&self.filename, self.encoding.to_str(), &self.shell))
}
}

View File

View File

@ -0,0 +1,188 @@
mod declaration;
pub use declaration::PluginDeclaration;
use crate::protocol::{LabeledError, PluginCall, PluginResponse};
use crate::EncodingType;
use std::io::BufReader;
use std::path::{Path, PathBuf};
use std::process::{Command as CommandSys, Stdio};
use nu_protocol::ShellError;
use nu_protocol::{Signature, Value};
use super::EvaluatedCall;
const OUTPUT_BUFFER_SIZE: usize = 8192;
pub trait PluginEncoder: Clone {
fn encode_call(
&self,
plugin_call: &PluginCall,
writer: &mut impl std::io::Write,
) -> Result<(), ShellError>;
fn decode_call(&self, reader: &mut impl std::io::BufRead) -> Result<PluginCall, ShellError>;
fn encode_response(
&self,
plugin_response: &PluginResponse,
writer: &mut impl std::io::Write,
) -> Result<(), ShellError>;
fn decode_response(
&self,
reader: &mut impl std::io::BufRead,
) -> Result<PluginResponse, ShellError>;
}
fn create_command(path: &Path, shell: &Option<PathBuf>) -> CommandSys {
let mut process = match (path.extension(), shell) {
(_, Some(shell)) => {
let mut process = std::process::Command::new(shell);
process.arg(path);
process
}
(Some(extension), None) => {
let (shell, separator) = match extension.to_str() {
Some("cmd") | Some("bat") => (Some("cmd"), Some("/c")),
Some("sh") => (Some("sh"), Some("-c")),
Some("py") => (Some("python"), None),
_ => (None, None),
};
match (shell, separator) {
(Some(shell), Some(separator)) => {
let mut process = std::process::Command::new(shell);
process.arg(separator);
process.arg(path);
process
}
(Some(shell), None) => {
let mut process = std::process::Command::new(shell);
process.arg(path);
process
}
_ => std::process::Command::new(path),
}
}
(None, None) => std::process::Command::new(path),
};
// Both stdout and stdin are piped so we can receive information from the plugin
process.stdout(Stdio::piped()).stdin(Stdio::piped());
process
}
pub fn get_signature(
path: &Path,
encoding: &EncodingType,
shell: &Option<PathBuf>,
) -> Result<Vec<Signature>, ShellError> {
let mut plugin_cmd = create_command(path, shell);
let mut child = plugin_cmd.spawn().map_err(|err| {
ShellError::PluginFailedToLoad(format!("Error spawning child process: {}", err))
})?;
// 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() {
let encoding_clone = encoding.clone();
std::thread::spawn(move || {
encoding_clone.encode_call(&PluginCall::Signature, &mut stdin_writer)
});
}
// deserialize response from plugin to extract the signature
let signatures = if let Some(stdout_reader) = &mut child.stdout {
let reader = stdout_reader;
let mut buf_read = BufReader::with_capacity(OUTPUT_BUFFER_SIZE, reader);
let response = encoding.decode_response(&mut buf_read)?;
match response {
PluginResponse::Signature(sign) => Ok(sign),
PluginResponse::Error(err) => Err(err.into()),
_ => Err(ShellError::PluginFailedToLoad(
"Plugin missing signature".into(),
)),
}
} else {
Err(ShellError::PluginFailedToLoad(
"Plugin missing stdout reader".into(),
))
}?;
match child.wait() {
Ok(_) => Ok(signatures),
Err(err) => Err(ShellError::PluginFailedToLoad(format!("{}", err))),
}
}
// The next trait and functions are part of the plugin that is being created
// The `Plugin` trait defines the API which plugins use to "hook" into nushell.
pub trait Plugin {
fn signature(&self) -> Vec<Signature>;
fn run(
&mut self,
name: &str,
call: &EvaluatedCall,
input: &Value,
) -> Result<Value, LabeledError>;
}
// Function used in the plugin definition for the communication protocol between
// nushell and the external plugin.
// When creating a new plugin you have to use this function as the main
// entry point for the plugin, e.g.
//
// fn main() {
// serve_plugin(plugin)
// }
//
// where plugin is your struct that implements the Plugin trait
//
// Note. When defining a plugin in other language but Rust, you will have to compile
// the plugin.capnp schema to create the object definitions that will be returned from
// the plugin.
// The object that is expected to be received by nushell is the PluginResponse struct.
// That should be encoded correctly and sent to StdOut for nushell to decode and
// and present its result
pub fn serve_plugin(plugin: &mut impl Plugin, encoder: impl PluginEncoder) {
let mut stdin_buf = BufReader::with_capacity(OUTPUT_BUFFER_SIZE, std::io::stdin());
let plugin_call = encoder.decode_call(&mut stdin_buf);
match plugin_call {
Err(err) => {
let response = PluginResponse::Error(err.into());
encoder
.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(plugin.signature());
encoder
.encode_response(&response, &mut std::io::stdout())
.expect("Error encoding response");
}
PluginCall::CallInfo(call_info) => {
let value = plugin.run(&call_info.name, &call_info.call, &call_info.input);
let response = match value {
Ok(value) => PluginResponse::Value(Box::new(value)),
Err(err) => PluginResponse::Error(err),
};
encoder
.encode_response(&response, &mut std::io::stdout())
.expect("Error encoding response");
}
}
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,163 @@
use nu_engine::eval_expression;
use nu_protocol::{
ast::Call,
engine::{EngineState, Stack},
FromValue, ShellError, Span, Spanned, Value,
};
use serde::{Deserialize, Serialize};
// The evaluated call is used with the Plugins because the plugin doesn't have
// access to the Stack and the EngineState. For that reason, before encoding the
// message to the plugin all the arguments to the original call (which are expressions)
// are evaluated and passed to Values
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EvaluatedCall {
pub head: Span,
pub positional: Vec<Value>,
pub named: Vec<(Spanned<String>, Option<Value>)>,
}
impl EvaluatedCall {
pub fn try_from_call(
call: &Call,
engine_state: &EngineState,
stack: &mut Stack,
) -> Result<Self, ShellError> {
let positional = call
.positional
.iter()
.map(|expr| eval_expression(engine_state, stack, expr))
.collect::<Result<Vec<Value>, ShellError>>()?;
let mut named = Vec::with_capacity(call.named.len());
for (string, expr) in call.named.iter() {
let value = match expr {
None => None,
Some(expr) => Some(eval_expression(engine_state, stack, expr)?),
};
named.push((string.clone(), value))
}
Ok(Self {
head: call.head,
positional,
named,
})
}
pub fn has_flag(&self, flag_name: &str) -> bool {
for name in &self.named {
if flag_name == name.0.item {
return true;
}
}
false
}
pub fn get_flag_value(&self, flag_name: &str) -> Option<Value> {
for name in &self.named {
if flag_name == name.0.item {
return name.1.clone();
}
}
None
}
pub fn nth(&self, pos: usize) -> Option<Value> {
self.positional.get(pos).cloned()
}
pub fn get_flag<T: FromValue>(&self, name: &str) -> Result<Option<T>, ShellError> {
if let Some(value) = self.get_flag_value(name) {
FromValue::from_value(&value).map(Some)
} else {
Ok(None)
}
}
pub fn rest<T: FromValue>(&self, starting_pos: usize) -> Result<Vec<T>, ShellError> {
self.positional
.iter()
.skip(starting_pos)
.map(|value| FromValue::from_value(value))
.collect()
}
pub fn opt<T: FromValue>(&self, pos: usize) -> Result<Option<T>, ShellError> {
if let Some(value) = self.nth(pos) {
FromValue::from_value(&value).map(Some)
} else {
Ok(None)
}
}
pub fn req<T: FromValue>(&self, pos: usize) -> Result<T, ShellError> {
if let Some(value) = self.nth(pos) {
FromValue::from_value(&value)
} else {
Err(ShellError::AccessBeyondEnd(
self.positional.len(),
self.head,
))
}
}
}
#[cfg(test)]
mod test {
use super::*;
use nu_protocol::{Span, Spanned, Value};
#[test]
fn call_to_value() {
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 },
}),
),
(
Spanned {
item: "flag".to_string(),
span: Span { start: 0, end: 10 },
},
None,
),
],
};
let name: Option<f64> = call.get_flag("name").unwrap();
assert_eq!(name, Some(1.0));
assert!(call.has_flag("flag"));
let required: f64 = call.req(0).unwrap();
assert!((required - 1.0).abs() < f64::EPSILON);
let optional: Option<String> = call.opt(1).unwrap();
assert_eq!(optional, Some("something".to_string()));
let rest: Vec<String> = call.rest(1).unwrap();
assert_eq!(rest, vec!["something".to_string()]);
}
}

View File

@ -0,0 +1,90 @@
mod evaluated_call;
pub use evaluated_call::EvaluatedCall;
use nu_protocol::{ShellError, Signature, Span, Value};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct CallInfo {
pub name: String,
pub call: EvaluatedCall,
pub input: Value,
}
// Information sent to the plugin
#[derive(Serialize, Deserialize, Debug)]
pub enum PluginCall {
Signature,
CallInfo(Box<CallInfo>),
}
#[derive(Clone, PartialEq, Serialize, Deserialize, Debug)]
pub struct LabeledError {
pub label: String,
pub msg: String,
pub span: Option<Span>,
}
impl From<LabeledError> for ShellError {
fn from(error: LabeledError) -> Self {
match error.span {
Some(span) => ShellError::SpannedLabeledError(error.label, error.msg, span),
None => ShellError::LabeledError(error.label, error.msg),
}
}
}
impl From<ShellError> for LabeledError {
fn from(error: ShellError) -> Self {
match error {
ShellError::SpannedLabeledError(label, msg, span) => LabeledError {
label,
msg,
span: Some(span),
},
ShellError::LabeledError(label, msg) => LabeledError {
label,
msg,
span: None,
},
ShellError::CantConvert(expected, input, span) => LabeledError {
label: format!("Can't convert to {}", expected),
msg: format!("can't convert {} to {}", expected, input),
span: Some(span),
},
ShellError::DidYouMean(suggestion, span) => LabeledError {
label: "Name not found".into(),
msg: format!("did you mean '{}'", suggestion),
span: Some(span),
},
ShellError::PluginFailedToLoad(msg) => LabeledError {
label: "Plugin failed to load".into(),
msg,
span: None,
},
ShellError::PluginFailedToEncode(msg) => LabeledError {
label: "Plugin failed to encode".into(),
msg,
span: None,
},
ShellError::PluginFailedToDecode(msg) => LabeledError {
label: "Plugin failed to decode".into(),
msg,
span: None,
},
err => LabeledError {
label: "Error - Add to LabeledError From<ShellError>".into(),
msg: err.to_string(),
span: None,
},
}
}
}
// Information received from the plugin
#[derive(Serialize, Deserialize)]
pub enum PluginResponse {
Error(LabeledError),
Signature(Vec<Signature>),
Value(Box<Value>),
}

View File

@ -0,0 +1,222 @@
use super::value;
use crate::{plugin_capnp::evaluated_call, EvaluatedCall};
use nu_protocol::{ShellError, Span, Spanned, Value};
pub(crate) fn serialize_call(
call: &EvaluatedCall,
mut builder: evaluated_call::Builder,
) -> Result<(), ShellError> {
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: &[Value], mut builder: evaluated_call::Builder) {
let mut positional_builder = builder.reborrow().init_positional(positional.len() as u32);
for (index, value) in positional.iter().enumerate() {
value::serialize_value(value, positional_builder.reborrow().get(index as u32))
}
}
fn serialize_named(
named: &[(Spanned<String>, Option<Value>)],
mut builder: evaluated_call::Builder,
) -> Result<(), ShellError> {
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| ShellError::PluginFailedToEncode(e.to_string()))?;
if let Some(value) = expression {
let value_builder = entry_builder.init_value();
value::serialize_value(value, value_builder);
}
}
Ok(())
}
pub(crate) fn deserialize_call(
reader: evaluated_call::Reader,
) -> Result<EvaluatedCall, ShellError> {
let head_reader = reader
.get_head()
.map_err(|e| ShellError::PluginFailedToDecode(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(EvaluatedCall {
head,
positional,
named,
})
}
fn deserialize_positionals(
span: Span,
reader: evaluated_call::Reader,
) -> Result<Vec<Value>, ShellError> {
let positional_reader = reader
.get_positional()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
positional_reader
.iter()
.map(move |x| value::deserialize_value(x, span))
.collect()
}
type NamedList = Vec<(Spanned<String>, Option<Value>)>;
fn deserialize_named(span: Span, reader: evaluated_call::Reader) -> Result<NamedList, ShellError> {
let named_reader = reader
.get_named()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
let entries_list = named_reader
.get_entries()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
let mut entries: Vec<(Spanned<String>, Option<Value>)> =
Vec::with_capacity(entries_list.len() as usize);
for entry_reader in entries_list {
let item = entry_reader
.get_key()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?
.to_string();
let value = if entry_reader.has_value() {
let value_reader = entry_reader
.get_value()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
let value = value::deserialize_value(value_reader, span)
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
Some(value)
} else {
None
};
let key = Spanned { item, span };
entries.push((key, value))
}
Ok(entries)
}
#[cfg(test)]
mod tests {
use capnp::serialize;
use core::panic;
use super::*;
use nu_protocol::{Span, Spanned, Value};
fn write_buffer(
call: &EvaluatedCall,
writer: &mut impl std::io::Write,
) -> Result<(), ShellError> {
let mut message = ::capnp::message::Builder::new_default();
let builder = message.init_root::<evaluated_call::Builder>();
serialize_call(call, builder)?;
serialize::write_message(writer, &message)
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))
}
fn read_buffer(reader: &mut impl std::io::BufRead) -> Result<EvaluatedCall, ShellError> {
let message_reader =
serialize::read_message(reader, ::capnp::message::ReaderOptions::new()).unwrap();
let reader = message_reader
.get_root::<evaluated_call::Reader>()
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
deserialize_call(reader)
}
#[test]
fn call_round_trip() {
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 },
}),
),
(
Spanned {
item: "flag".to_string(),
span: Span { start: 0, end: 10 },
},
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)| assert_eq!(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)) => assert_eq!(a, b),
_ => panic!("not matching values"),
}
});
}
}

View 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)
}
}

View File

@ -0,0 +1,416 @@
use super::signature::deserialize_signature;
use super::{call, signature, value};
use crate::plugin_capnp::{plugin_call, plugin_response};
use crate::protocol::{CallInfo, LabeledError, PluginCall, PluginResponse};
use capnp::serialize;
use nu_protocol::{ShellError, Signature, Span};
pub fn encode_call(
plugin_call: &PluginCall,
writer: &mut impl std::io::Write,
) -> Result<(), ShellError> {
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 name from the call
call_info_builder.set_name(call_info.name.as_str());
// Serializing argument information from the call
let call_builder = call_info_builder
.reborrow()
.get_call()
.map_err(|e| ShellError::PluginFailedToEncode(e.to_string()))?;
call::serialize_call(&call_info.call, call_builder)
.map_err(|e| ShellError::PluginFailedToEncode(e.to_string()))?;
// Serializing the input value from the call info
let value_builder = call_info_builder
.reborrow()
.get_input()
.map_err(|e| ShellError::PluginFailedToEncode(e.to_string()))?;
value::serialize_value(&call_info.input, value_builder);
}
};
serialize::write_message(writer, &message)
.map_err(|e| ShellError::PluginFailedToEncode(e.to_string()))
}
pub fn decode_call(reader: &mut impl std::io::BufRead) -> Result<PluginCall, ShellError> {
let message_reader = serialize::read_message(reader, ::capnp::message::ReaderOptions::new())
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
let reader = message_reader
.get_root::<plugin_call::Reader>()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
match reader.which() {
Err(capnp::NotInSchema(_)) => Err(ShellError::PluginFailedToDecode(
"value not in schema".into(),
)),
Ok(plugin_call::Signature(())) => Ok(PluginCall::Signature),
Ok(plugin_call::CallInfo(reader)) => {
let reader = reader.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
let name = reader
.get_name()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
let call_reader = reader
.get_call()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
let call = call::deserialize_call(call_reader)?;
let input_reader = reader
.get_input()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
let input = value::deserialize_value(input_reader, call.head)?;
Ok(PluginCall::CallInfo(Box::new(CallInfo {
name: name.to_string(),
call,
input,
})))
}
}
}
pub fn encode_response(
plugin_response: &PluginResponse,
writer: &mut impl std::io::Write,
) -> Result<(), ShellError> {
let mut message = ::capnp::message::Builder::new_default();
let mut builder = message.init_root::<plugin_response::Builder>();
match &plugin_response {
PluginResponse::Error(msg) => {
let mut error_builder = builder.reborrow().init_error();
error_builder.set_label(&msg.label);
error_builder.set_msg(&msg.msg);
if let Some(span) = msg.span {
let mut span_builder = error_builder.reborrow().init_span();
span_builder.set_start(span.start as u64);
span_builder.set_end(span.end as u64);
}
}
PluginResponse::Signature(signatures) => {
let mut signature_list_builder =
builder.reborrow().init_signature(signatures.len() as u32);
for (index, signature) in signatures.iter().enumerate() {
let signature_builder = signature_list_builder.reborrow().get(index as u32);
signature::serialize_signature(signature, signature_builder)
}
}
PluginResponse::Value(val) => {
let value_builder = builder.reborrow().init_value();
value::serialize_value(val, value_builder);
}
};
serialize::write_message(writer, &message)
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))
}
pub fn decode_response(reader: &mut impl std::io::BufRead) -> Result<PluginResponse, ShellError> {
let message_reader = serialize::read_message(reader, ::capnp::message::ReaderOptions::new())
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
let reader = message_reader
.get_root::<plugin_response::Reader>()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
match reader.which() {
Err(capnp::NotInSchema(_)) => Err(ShellError::PluginFailedToDecode(
"value not in schema".into(),
)),
Ok(plugin_response::Error(reader)) => {
let reader = reader.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
let msg = reader
.get_msg()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
let label = reader
.get_label()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
let span = if reader.has_span() {
let span = reader
.get_span()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
Some(Span {
start: span.get_start() as usize,
end: span.get_end() as usize,
})
} else {
None
};
let error = LabeledError {
label: label.into(),
msg: msg.into(),
span,
};
Ok(PluginResponse::Error(error))
}
Ok(plugin_response::Signature(reader)) => {
let reader = reader.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
let signatures = reader
.iter()
.map(deserialize_signature)
.collect::<Result<Vec<Signature>, ShellError>>()?;
Ok(PluginResponse::Signature(signatures))
}
Ok(plugin_response::Value(reader)) => {
let reader = reader.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
let span = reader
.get_span()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
let val = value::deserialize_value(
reader,
Span {
start: span.get_start() as usize,
end: span.get_end() as usize,
},
)
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
Ok(PluginResponse::Value(Box::new(val)))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::protocol::{EvaluatedCall, LabeledError, PluginCall, PluginResponse};
use nu_protocol::{Signature, Span, Spanned, SyntaxShape, Value};
#[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 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 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!(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 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!(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 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 error = LabeledError {
label: "label".into(),
msg: "msg".into(),
span: Some(Span { start: 2, end: 30 }),
};
let response = PluginResponse::Error(error.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!(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 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!(error, msg),
PluginResponse::Signature(_) => panic!("returned wrong call type"),
PluginResponse::Value(_) => panic!("returned wrong call type"),
}
}
}

View File

@ -0,0 +1,150 @@
@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;
network @10;
random @11;
platform @12;
shells @13;
strings @14;
system @15;
viewers @16;
hash @17;
generators @18;
}
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;
}

View File

@ -0,0 +1,392 @@
use crate::plugin_capnp::{argument, flag, signature, Category as PluginCategory, Shape};
use nu_protocol::{Category, Flag, PositionalArg, ShellError, 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);
match signature.category {
Category::Default => builder.set_category(PluginCategory::Default),
Category::Conversions => builder.set_category(PluginCategory::Conversions),
Category::Core => builder.set_category(PluginCategory::Core),
Category::Date => builder.set_category(PluginCategory::Date),
Category::Env => builder.set_category(PluginCategory::Env),
Category::Experimental => builder.set_category(PluginCategory::Experimental),
Category::FileSystem => builder.set_category(PluginCategory::Filesystem),
Category::Filters => builder.set_category(PluginCategory::Filters),
Category::Formats => builder.set_category(PluginCategory::Formats),
Category::Math => builder.set_category(PluginCategory::Math),
Category::Network => builder.set_category(PluginCategory::Network),
Category::Random => builder.set_category(PluginCategory::Random),
Category::Platform => builder.set_category(PluginCategory::Platform),
Category::Shells => builder.set_category(PluginCategory::Shells),
Category::Strings => builder.set_category(PluginCategory::Strings),
Category::System => builder.set_category(PluginCategory::System),
Category::Viewers => builder.set_category(PluginCategory::Viewers),
Category::Hash => builder.set_category(PluginCategory::Hash),
Category::Generators => builder.set_category(PluginCategory::Generators),
_ => builder.set_category(PluginCategory::Default),
}
// 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
if let Some(arg) = &signature.rest_positional {
let rest_argument = builder.reborrow().init_rest();
serialize_argument(arg, rest_argument)
}
// 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());
if let Some(val) = arg.short {
let mut inner_builder = builder.reborrow().init_short(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, ShellError> {
let name = reader
.get_name()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
let usage = reader
.get_usage()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
let extra_usage = reader
.get_extra_usage()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
let is_filter = reader.get_is_filter();
let category = match reader
.get_category()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?
{
PluginCategory::Default => Category::Default,
PluginCategory::Conversions => Category::Conversions,
PluginCategory::Core => Category::Core,
PluginCategory::Date => Category::Date,
PluginCategory::Env => Category::Env,
PluginCategory::Experimental => Category::Experimental,
PluginCategory::Filesystem => Category::FileSystem,
PluginCategory::Filters => Category::Filters,
PluginCategory::Formats => Category::Formats,
PluginCategory::Math => Category::Math,
PluginCategory::Strings => Category::Strings,
PluginCategory::System => Category::System,
PluginCategory::Viewers => Category::Viewers,
PluginCategory::Network => Category::Network,
PluginCategory::Random => Category::Random,
PluginCategory::Platform => Category::Platform,
PluginCategory::Shells => Category::Shells,
PluginCategory::Hash => Category::Hash,
PluginCategory::Generators => Category::Generators,
};
// Deserializing required arguments
let required_list = reader
.get_required_positional()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
let required_positional = required_list
.iter()
.map(deserialize_argument)
.collect::<Result<Vec<PositionalArg>, ShellError>>()?;
// Deserializing optional arguments
let optional_list = reader
.get_optional_positional()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
let optional_positional = optional_list
.iter()
.map(deserialize_argument)
.collect::<Result<Vec<PositionalArg>, ShellError>>()?;
// Deserializing rest arguments
let rest_positional = if reader.has_rest() {
let argument_reader = reader
.get_rest()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
Some(deserialize_argument(argument_reader)?)
} else {
None
};
// Deserializing named arguments
let named_list = reader
.get_named()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
let named = named_list
.iter()
.map(deserialize_flag)
.collect::<Result<Vec<Flag>, ShellError>>()?;
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,
category,
})
}
fn deserialize_argument(reader: argument::Reader) -> Result<PositionalArg, ShellError> {
let name = reader
.get_name()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
let desc = reader
.get_desc()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
let shape = reader
.get_shape()
.map_err(|e| ShellError::PluginFailedToDecode(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, ShellError> {
let long = reader
.get_long()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
let desc = reader
.get_desc()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
let required = reader.get_required();
let short = if reader.has_short() {
let short_reader = reader
.get_short()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
short_reader.chars().next()
} else {
None
};
let arg = reader
.get_arg()
.map_err(|e| ShellError::PluginFailedToDecode(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;
use nu_protocol::{Category, 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::write_message(writer, &message)
.map_err(|e| ShellError::PluginFailedToEncode(e.to_string()))
}
pub fn read_buffer(reader: &mut impl std::io::BufRead) -> Result<Signature, ShellError> {
let message_reader =
serialize::read_message(reader, ::capnp::message::ReaderOptions::new()).unwrap();
let reader = message_reader
.get_root::<signature::Reader>()
.map_err(|e| ShellError::PluginFailedToEncode(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::Int, "first named", Some('s'))
.required_named("name", SyntaxShape::String, "first named", Some('n'))
.required_named("string", SyntaxShape::String, "second named", Some('x'))
.switch("switch", "some switch", None)
.rest("remaining", SyntaxShape::Int, "remaining")
.category(Category::Conversions);
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);
assert_eq!(signature.category, returned_signature.category);
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 value_round_trip_2() {
let signature = Signature::build("test-1")
.desc("Signature test 1 for plugin. Returns Value::Nothing")
.required("a", SyntaxShape::Int, "required integer value")
.required("b", SyntaxShape::String, "required string value")
.optional("opt", SyntaxShape::Boolean, "Optional boolean")
.switch("flag", "a flag for the signature", Some('f'))
.named("named", SyntaxShape::String, "named string", Some('n'))
.category(Category::Experimental);
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);
assert_eq!(signature.category, returned_signature.category);
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,375 @@
use crate::plugin_capnp::value;
use nu_protocol::{ShellError, 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::Record { cols, vals, span } => {
let mut record_builder = builder.reborrow().init_record();
let mut cols_builder = record_builder.reborrow().init_cols(cols.len() as u32);
cols.iter()
.enumerate()
.for_each(|(index, col)| cols_builder.set(index as u32, col.as_str()));
let mut values_builder = record_builder.reborrow().init_vals(vals.len() as u32);
vals.iter().enumerate().for_each(|(index, value)| {
let inner_builder = values_builder.reborrow().get(index as u32);
serialize_value(value, inner_builder);
});
*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
// FIXME: put this in a proper span
Span { start: 0, end: 0 }
}
};
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, head: Span) -> Result<Value, ShellError> {
let span_reader = reader
.get_span()
.map_err(|e| ShellError::PluginFailedToDecode(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| ShellError::PluginFailedToDecode(e.to_string()))?
.to_string();
Ok(Value::String { val: string, span })
}
Ok(value::Record(record)) => {
let record = record.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
let cols = record
.get_cols()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?
.iter()
.map(|col| {
col.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))
.map(|col| col.to_string())
})
.collect::<Result<Vec<String>, ShellError>>()?;
let vals = record
.get_vals()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?
.iter()
.map(move |x| deserialize_value(x, span))
.collect::<Result<Vec<Value>, ShellError>>()?;
Ok(Value::Record { cols, vals, span })
}
Ok(value::List(vals)) => {
let values = vals.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
let values_list = values
.iter()
.map(move |x| deserialize_value(x, span))
.collect::<Result<Vec<Value>, ShellError>>()?;
Ok(Value::List {
vals: values_list,
span,
})
}
Err(capnp::NotInSchema(_)) => Ok(Value::Nothing { span: head }),
}
}
#[cfg(test)]
mod tests {
use super::*;
use capnp::serialize;
use nu_protocol::{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>();
serialize_value(value, builder.reborrow());
serialize::write_message(writer, &message)
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))
}
pub fn read_buffer(reader: &mut impl std::io::BufRead) -> Result<Value, ShellError> {
let message_reader =
serialize::read_message(reader, ::capnp::message::ReaderOptions::new()).unwrap();
let reader = message_reader
.get_root::<value::Reader>()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
let span = reader
.get_span()
.map_err(|e| ShellError::PluginFailedToDecode(e.to_string()))?;
deserialize_value(
reader.reborrow(),
Span {
start: span.get_start() as usize,
end: span.get_end() as usize,
},
)
}
#[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")
)
}
#[test]
fn record_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 vals = 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 cols = vec![
"bool".to_string(),
"int".to_string(),
"float".to_string(),
"string".to_string(),
"list".to_string(),
];
let record = Value::Record {
cols,
vals,
span: Span { start: 1, end: 20 },
};
let mut buffer: Vec<u8> = Vec::new();
write_buffer(&record, &mut buffer).expect("unable to serialize message");
let returned_record =
read_buffer(&mut buffer.as_slice()).expect("unable to deserialize message");
assert_eq!(record, returned_record)
}
}

View 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"),
}
}
}

View File

@ -0,0 +1,74 @@
use nu_protocol::ShellError;
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",
}
}
}

View File

@ -1,155 +0,0 @@
use crate::Plugin;
use indexmap::IndexMap;
use nu_errors::ShellError;
use nu_protocol::{
CallInfo, EvaluatedArgs, Primitive, ReturnSuccess, ReturnValue, UntaggedValue, Value,
};
use nu_source::Tag;
use nu_test_support::value::column_path;
use nu_value_ext::ValueExt;
pub struct PluginTest<'a, T: Plugin> {
plugin: &'a mut T,
call_info: CallInfo,
input: Value,
}
impl<'a, T: Plugin> PluginTest<'a, T> {
pub fn for_plugin(plugin: &'a mut T) -> Self {
PluginTest {
plugin,
call_info: CallStub::new().create(),
input: UntaggedValue::nothing().into_value(Tag::unknown()),
}
}
pub fn args(&mut self, call_info: CallInfo) -> &mut PluginTest<'a, T> {
self.call_info = call_info;
self
}
pub fn configure(&mut self, callback: impl FnOnce(Vec<String>)) -> &mut PluginTest<'a, T> {
let signature = self
.plugin
.config()
.expect("There was a problem configuring the plugin.");
callback(signature.named.keys().map(String::from).collect());
self
}
pub fn input(&mut self, value: Value) -> &mut PluginTest<'a, T> {
self.input = value;
self
}
pub fn test(&mut self) -> Result<Vec<ReturnValue>, ShellError> {
let return_values = self.plugin.filter(self.input.clone());
let mut return_values = return_values?;
let end = self.plugin.end_filter();
return_values.extend(end?);
self.plugin.quit();
Ok(return_values)
}
pub fn setup(
&mut self,
callback: impl FnOnce(&mut T, Result<Vec<ReturnValue>, ShellError>),
) -> &mut PluginTest<'a, T> {
let call_stub = self.call_info.clone();
self.configure(|flags_configured| {
let flags_registered = &call_stub.args.named;
let flag_passed = flags_registered
.as_ref()
.map(|names| names.keys().map(String::from).collect::<Vec<String>>());
if let Some(flags) = flag_passed {
for flag in flags {
assert!(
flags_configured.iter().any(|f| *f == flag),
"The flag you passed is not configured in the plugin.",
);
}
}
});
let return_values = self.plugin.begin_filter(call_stub);
callback(self.plugin, return_values);
self
}
}
pub fn plugin<T: Plugin>(plugin: &mut T) -> PluginTest<T> {
PluginTest::for_plugin(plugin)
}
#[derive(Default)]
pub struct CallStub {
positionals: Vec<Value>,
flags: IndexMap<String, Value>,
}
impl CallStub {
pub fn new() -> Self {
Default::default()
}
pub fn with_named_parameter(&mut self, name: &str, value: Value) -> &mut Self {
self.flags.insert(name.to_string(), value);
self
}
pub fn with_long_flag(&mut self, name: &str) -> &mut Self {
self.flags.insert(
name.to_string(),
UntaggedValue::boolean(true).into_value(Tag::unknown()),
);
self
}
pub fn with_parameter(&mut self, name: &str) -> Result<&mut Self, ShellError> {
let cp = column_path(name)
.as_column_path()
.expect("Failed! Expected valid column path.");
let cp = UntaggedValue::Primitive(Primitive::ColumnPath(cp.item)).into_value(cp.tag);
self.positionals.push(cp);
Ok(self)
}
pub fn create(&self) -> CallInfo {
CallInfo {
args: EvaluatedArgs::new(Some(self.positionals.clone()), Some(self.flags.clone())),
name_tag: Tag::unknown(),
}
}
}
pub fn expect_return_value_at(
for_results: Result<Vec<Result<ReturnSuccess, ShellError>>, ShellError>,
at: usize,
) -> Value {
let return_values = for_results
.expect("Failed! This seems to be an error getting back the results from the plugin.");
for (idx, item) in return_values.iter().enumerate() {
let item = match item {
Ok(return_value) => return_value,
Err(_) => panic!("Unexpected value"),
};
if idx == at {
if let Some(value) = item.raw_value() {
return value;
} else {
panic!("Internal error: could not get raw value in expect_return_value_at")
}
}
}
panic!("Couldn't get return value from stream.")
}