From 9838154ad1e30c1bd50561abf3875fcdaeb615bf Mon Sep 17 00:00:00 2001 From: Fernando Herrera Date: Sat, 30 Oct 2021 11:19:16 +0100 Subject: [PATCH] round trip call info --- .../nu-command/src/experimental/run_plugin.rs | 12 ++-- crates/nu-plugin/src/serializers/callinfo.rs | 70 ++++++++++++++++++- 2 files changed, 74 insertions(+), 8 deletions(-) diff --git a/crates/nu-command/src/experimental/run_plugin.rs b/crates/nu-command/src/experimental/run_plugin.rs index d54c3c94b8..f98c359529 100644 --- a/crates/nu-command/src/experimental/run_plugin.rs +++ b/crates/nu-command/src/experimental/run_plugin.rs @@ -1,7 +1,8 @@ use nu_protocol::ast::Call; -use nu_protocol::engine::{Command, EvaluationContext}; -use nu_protocol::{ShellError, Signature, Value}; +use nu_protocol::engine::{Command, EngineState, Stack}; +use nu_protocol::{PipelineData, ShellError, Signature}; +#[derive(Clone)] pub struct RunPlugin; impl Command for RunPlugin { @@ -19,10 +20,11 @@ impl Command for RunPlugin { fn run( &self, - _context: &EvaluationContext, + _context: &EngineState, + _stack: &mut Stack, _call: &Call, - _input: Value, - ) -> Result { + _input: PipelineData, + ) -> Result { Err(ShellError::InternalError("plugin".into())) } } diff --git a/crates/nu-plugin/src/serializers/callinfo.rs b/crates/nu-plugin/src/serializers/callinfo.rs index 97bc1ae760..ecd0cff2be 100644 --- a/crates/nu-plugin/src/serializers/callinfo.rs +++ b/crates/nu-plugin/src/serializers/callinfo.rs @@ -3,6 +3,12 @@ use crate::value_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, @@ -30,6 +36,31 @@ pub fn write_buffer( .map_err(|e| ShellError::EncodingError(e.to_string())) } +pub fn read_buffer(reader: &mut impl std::io::BufRead) -> Result { + let message_reader = + serialize_packed::read_message(reader, ::capnp::message::ReaderOptions::new()).unwrap(); + + let reader = message_reader + .get_root::() + .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::*; @@ -38,9 +69,19 @@ mod tests { 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 value = Value::Bool { + let input = Value::Bool { val: false, span: Span { start: 1, end: 20 }, }; @@ -77,7 +118,30 @@ mod tests { }; let mut buffer: Vec = Vec::new(); - write_buffer(&call, &value, &mut buffer).expect("unable to serialize message"); - println!("{:?}", buffer); + 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"), + } + }); } }