Plugin with evaluated call (#393)

* plugin trait

* impl of trait

* record and absolute path

* plugin example crate

* clippy error

* correcting cargo

* evaluated call for plugin
This commit is contained in:
Fernando Herrera
2021-12-02 05:42:56 +00:00
committed by GitHub
parent 2bbba3f5da
commit 56307553ae
29 changed files with 1140 additions and 770 deletions

View File

@@ -1,11 +1,11 @@
use crate::plugin::PluginError;
use crate::plugin_capnp::{call, expression};
use nu_protocol::{
ast::{Call, Expr, Expression},
Span, Spanned, Type,
};
use super::value;
use crate::{evaluated_call::EvaluatedCall, plugin_capnp::evaluated_call};
use nu_protocol::{ShellError, Span, Spanned, Value};
pub(crate) fn serialize_call(call: &Call, mut builder: call::Builder) -> Result<(), PluginError> {
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);
@@ -16,18 +16,18 @@ pub(crate) fn serialize_call(call: &Call, mut builder: call::Builder) -> Result<
Ok(())
}
fn serialize_positional(positional: &[Expression], mut builder: call::Builder) {
fn serialize_positional(positional: &[Value], mut builder: evaluated_call::Builder) {
let mut positional_builder = builder.reborrow().init_positional(positional.len() as u32);
for (index, expression) in positional.iter().enumerate() {
serialize_expression(expression, positional_builder.reborrow().get(index as u32))
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<Expression>)],
mut builder: call::Builder,
) -> Result<(), PluginError> {
named: &[(Spanned<String>, Option<Value>)],
mut builder: evaluated_call::Builder,
) -> Result<(), ShellError> {
let mut named_builder = builder
.reborrow()
.init_named()
@@ -38,42 +38,23 @@ fn serialize_named(
entry_builder
.reborrow()
.set_key(key.item.as_str())
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
.map_err(|e| ShellError::InternalError(e.to_string()))?;
if let Some(expr) = expression {
if let Some(value) = expression {
let value_builder = entry_builder.init_value();
serialize_expression(expr, value_builder);
value::serialize_value(value, value_builder);
}
}
Ok(())
}
fn serialize_expression(expression: &Expression, mut builder: expression::Builder) {
match &expression.expr {
Expr::Garbage => builder.set_garbage(()),
Expr::Bool(val) => builder.set_bool(*val),
Expr::Int(val) => builder.set_int(*val),
Expr::Float(val) => builder.set_float(*val),
Expr::String(val) => builder.set_string(val),
Expr::List(values) => {
let mut list_builder = builder.reborrow().init_list(values.len() as u32);
for (index, expression) in values.iter().enumerate() {
let inner_builder = list_builder.reborrow().get(index as u32);
serialize_expression(expression, inner_builder)
}
}
_ => {
// If there is the need to pass other type of argument to the plugin
// we have to define the encoding for that parameter in this match
}
}
}
pub(crate) fn deserialize_call(reader: call::Reader) -> Result<Call, PluginError> {
pub(crate) fn deserialize_call(
reader: evaluated_call::Reader,
) -> Result<EvaluatedCall, ShellError> {
let head_reader = reader
.get_head()
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
.map_err(|e| ShellError::InternalError(e.to_string()))?;
let head = Span {
start: head_reader.get_start() as usize,
@@ -83,8 +64,7 @@ pub(crate) fn deserialize_call(reader: call::Reader) -> Result<Call, PluginError
let positional = deserialize_positionals(head, reader)?;
let named = deserialize_named(head, reader)?;
Ok(Call {
decl_id: 0,
Ok(EvaluatedCall {
head,
positional,
named,
@@ -92,48 +72,48 @@ pub(crate) fn deserialize_call(reader: call::Reader) -> Result<Call, PluginError
}
fn deserialize_positionals(
span: Span,
reader: call::Reader,
) -> Result<Vec<Expression>, PluginError> {
_span: Span,
reader: evaluated_call::Reader,
) -> Result<Vec<Value>, ShellError> {
let positional_reader = reader
.get_positional()
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
.map_err(|e| ShellError::InternalError(e.to_string()))?;
positional_reader
.iter()
.map(|expression_reader| deserialize_expression(span, expression_reader))
.map(value::deserialize_value)
.collect()
}
type NamedList = Vec<(Spanned<String>, Option<Expression>)>;
type NamedList = Vec<(Spanned<String>, Option<Value>)>;
fn deserialize_named(span: Span, reader: call::Reader) -> Result<NamedList, PluginError> {
fn deserialize_named(span: Span, reader: evaluated_call::Reader) -> Result<NamedList, ShellError> {
let named_reader = reader
.get_named()
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
.map_err(|e| ShellError::InternalError(e.to_string()))?;
let entries_list = named_reader
.get_entries()
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
.map_err(|e| ShellError::InternalError(e.to_string()))?;
let mut entries: Vec<(Spanned<String>, Option<Expression>)> =
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| PluginError::DecodingError(e.to_string()))?
.map_err(|e| ShellError::InternalError(e.to_string()))?
.to_string();
let value = if entry_reader.has_value() {
let value_reader = entry_reader
.get_value()
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
.map_err(|e| ShellError::InternalError(e.to_string()))?;
let expression = deserialize_expression(span, value_reader)
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
let value = value::deserialize_value(value_reader)
.map_err(|e| ShellError::InternalError(e.to_string()))?;
Some(expression)
Some(value)
} else {
None
};
@@ -146,102 +126,50 @@ fn deserialize_named(span: Span, reader: call::Reader) -> Result<NamedList, Plug
Ok(entries)
}
fn deserialize_expression(
span: Span,
reader: expression::Reader,
) -> Result<Expression, PluginError> {
let expr = match reader.which() {
Ok(expression::Garbage(())) => Expr::Garbage,
Ok(expression::Bool(val)) => Expr::Bool(val),
Ok(expression::Int(val)) => Expr::Int(val),
Ok(expression::Float(val)) => Expr::Float(val),
Ok(expression::String(val)) => {
let string = val
.map_err(|e| PluginError::DecodingError(e.to_string()))?
.to_string();
Expr::String(string)
}
Ok(expression::List(values)) => {
let values = values.map_err(|e| PluginError::DecodingError(e.to_string()))?;
let values_list = values
.iter()
.map(|inner_reader| deserialize_expression(span, inner_reader))
.collect::<Result<Vec<Expression>, PluginError>>()?;
Expr::List(values_list)
}
Err(capnp::NotInSchema(_)) => Expr::Garbage,
};
Ok(Expression {
expr,
span,
ty: Type::Unknown,
custom_completion: None,
})
}
#[cfg(test)]
mod tests {
use capnp::serialize;
use core::panic;
use super::*;
use nu_protocol::{
ast::{Call, Expr, Expression},
Span, Spanned,
};
use nu_protocol::{Span, Spanned, Value};
fn write_buffer(call: &Call, writer: &mut impl std::io::Write) -> Result<(), PluginError> {
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::<call::Builder>();
let builder = message.init_root::<evaluated_call::Builder>();
serialize_call(call, builder)?;
serialize::write_message(writer, &message)
.map_err(|e| PluginError::EncodingError(e.to_string()))
.map_err(|e| ShellError::InternalError(e.to_string()))
}
fn read_buffer(reader: &mut impl std::io::BufRead) -> Result<Call, PluginError> {
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::<call::Reader>()
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
.get_root::<evaluated_call::Reader>()
.map_err(|e| ShellError::InternalError(e.to_string()))?;
deserialize_call(reader)
}
fn compare_expressions(lhs: &Expression, rhs: &Expression) {
match (&lhs.expr, &rhs.expr) {
(Expr::Bool(a), Expr::Bool(b)) => assert_eq!(a, b),
(Expr::Int(a), Expr::Int(b)) => assert_eq!(a, b),
(Expr::Float(a), Expr::Float(b)) => assert!((a - b).abs() < f64::EPSILON),
(Expr::String(a), Expr::String(b)) => assert_eq!(a, b),
_ => panic!("not matching values"),
}
}
#[test]
fn call_round_trip() {
let call = Call {
decl_id: 1,
let call = EvaluatedCall {
head: Span { start: 0, end: 10 },
positional: vec![
Expression {
expr: Expr::Float(1.0),
Value::Float {
val: 1.0,
span: Span { start: 0, end: 10 },
ty: nu_protocol::Type::Float,
custom_completion: None,
},
Expression {
expr: Expr::String("something".into()),
Value::String {
val: "something".into(),
span: Span { start: 0, end: 10 },
ty: nu_protocol::Type::Float,
custom_completion: None,
},
],
named: vec![
@@ -250,11 +178,9 @@ mod tests {
item: "name".to_string(),
span: Span { start: 0, end: 10 },
},
Some(Expression {
expr: Expr::Float(1.0),
Some(Value::Float {
val: 1.0,
span: Span { start: 0, end: 10 },
ty: nu_protocol::Type::Float,
custom_completion: None,
}),
),
(
@@ -277,7 +203,7 @@ mod tests {
call.positional
.iter()
.zip(returned_call.positional.iter())
.for_each(|(lhs, rhs)| compare_expressions(lhs, rhs));
.for_each(|(lhs, rhs)| assert_eq!(lhs, rhs));
call.named
.iter()
@@ -288,7 +214,7 @@ mod tests {
match (&lhs.1, &rhs.1) {
(None, None) => {}
(Some(a), Some(b)) => compare_expressions(a, b),
(Some(a), Some(b)) => assert_eq!(a, b),
_ => panic!("not matching values"),
}
});

View File

@@ -1,6 +1,5 @@
use crate::plugin::PluginError;
use crate::plugin_capnp::{argument, flag, signature, Category as PluginCategory, Shape};
use nu_protocol::{Category, Flag, PositionalArg, Signature, SyntaxShape};
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());
@@ -46,14 +45,13 @@ pub(crate) fn serialize_signature(signature: &Signature, mut builder: signature:
}
// Serializing rest argument
let rest_argument = builder.reborrow().init_rest();
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)
@@ -95,21 +93,21 @@ fn serialize_flag(arg: &Flag, mut builder: flag::Builder) {
}
}
pub(crate) fn deserialize_signature(reader: signature::Reader) -> Result<Signature, PluginError> {
pub(crate) fn deserialize_signature(reader: signature::Reader) -> Result<Signature, ShellError> {
let name = reader
.get_name()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
.map_err(|e| ShellError::InternalError(e.to_string()))?;
let usage = reader
.get_usage()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
.map_err(|e| ShellError::InternalError(e.to_string()))?;
let extra_usage = reader
.get_extra_usage()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
.map_err(|e| ShellError::InternalError(e.to_string()))?;
let is_filter = reader.get_is_filter();
let category = match reader
.get_category()
.map_err(|e| PluginError::EncodingError(e.to_string()))?
.map_err(|e| ShellError::InternalError(e.to_string()))?
{
PluginCategory::Default => Category::Default,
PluginCategory::Conversions => Category::Conversions,
@@ -129,28 +127,28 @@ pub(crate) fn deserialize_signature(reader: signature::Reader) -> Result<Signatu
// Deserializing required arguments
let required_list = reader
.get_required_positional()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
.map_err(|e| ShellError::InternalError(e.to_string()))?;
let required_positional = required_list
.iter()
.map(deserialize_argument)
.collect::<Result<Vec<PositionalArg>, PluginError>>()?;
.collect::<Result<Vec<PositionalArg>, ShellError>>()?;
// Deserializing optional arguments
let optional_list = reader
.get_optional_positional()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
.map_err(|e| ShellError::InternalError(e.to_string()))?;
let optional_positional = optional_list
.iter()
.map(deserialize_argument)
.collect::<Result<Vec<PositionalArg>, PluginError>>()?;
.collect::<Result<Vec<PositionalArg>, ShellError>>()?;
// Deserializing rest arguments
let rest_positional = if reader.has_rest() {
let argument_reader = reader
.get_rest()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
.map_err(|e| ShellError::InternalError(e.to_string()))?;
Some(deserialize_argument(argument_reader)?)
} else {
@@ -160,12 +158,12 @@ pub(crate) fn deserialize_signature(reader: signature::Reader) -> Result<Signatu
// Deserializing named arguments
let named_list = reader
.get_named()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
.map_err(|e| ShellError::InternalError(e.to_string()))?;
let named = named_list
.iter()
.map(deserialize_flag)
.collect::<Result<Vec<Flag>, PluginError>>()?;
.collect::<Result<Vec<Flag>, ShellError>>()?;
Ok(Signature {
name: name.to_string(),
@@ -181,18 +179,18 @@ pub(crate) fn deserialize_signature(reader: signature::Reader) -> Result<Signatu
})
}
fn deserialize_argument(reader: argument::Reader) -> Result<PositionalArg, PluginError> {
fn deserialize_argument(reader: argument::Reader) -> Result<PositionalArg, ShellError> {
let name = reader
.get_name()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
.map_err(|e| ShellError::InternalError(e.to_string()))?;
let desc = reader
.get_desc()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
.map_err(|e| ShellError::InternalError(e.to_string()))?;
let shape = reader
.get_shape()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
.map_err(|e| ShellError::InternalError(e.to_string()))?;
let shape = match shape {
Shape::String => SyntaxShape::String,
@@ -211,21 +209,21 @@ fn deserialize_argument(reader: argument::Reader) -> Result<PositionalArg, Plugi
})
}
fn deserialize_flag(reader: flag::Reader) -> Result<Flag, PluginError> {
fn deserialize_flag(reader: flag::Reader) -> Result<Flag, ShellError> {
let long = reader
.get_long()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
.map_err(|e| ShellError::InternalError(e.to_string()))?;
let desc = reader
.get_desc()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
.map_err(|e| ShellError::InternalError(e.to_string()))?;
let required = reader.get_required();
let short = if reader.has_short() {
let short_reader = reader
.get_short()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
.map_err(|e| ShellError::InternalError(e.to_string()))?;
short_reader.chars().next()
} else {
@@ -234,7 +232,7 @@ fn deserialize_flag(reader: flag::Reader) -> Result<Flag, PluginError> {
let arg = reader
.get_arg()
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
.map_err(|e| ShellError::InternalError(e.to_string()))?;
let arg = match arg {
Shape::None => None,
@@ -264,7 +262,7 @@ mod tests {
pub fn write_buffer(
signature: &Signature,
writer: &mut impl std::io::Write,
) -> Result<(), PluginError> {
) -> Result<(), ShellError> {
let mut message = ::capnp::message::Builder::new_default();
let builder = message.init_root::<signature::Builder>();
@@ -272,16 +270,16 @@ mod tests {
serialize_signature(signature, builder);
serialize::write_message(writer, &message)
.map_err(|e| PluginError::EncodingError(e.to_string()))
.map_err(|e| ShellError::InternalError(e.to_string()))
}
pub fn read_buffer(reader: &mut impl std::io::BufRead) -> Result<Signature, PluginError> {
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| PluginError::DecodingError(e.to_string()))?;
.map_err(|e| ShellError::InternalError(e.to_string()))?;
deserialize_signature(reader)
}
@@ -292,12 +290,9 @@ mod tests {
.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'),
)
.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);
@@ -336,4 +331,50 @@ mod tests {
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

@@ -1,6 +1,5 @@
use crate::plugin::PluginError;
use crate::plugin_capnp::value;
use nu_protocol::{Span, Value};
use nu_protocol::{ShellError, Span, Value};
pub(crate) fn serialize_value(value: &Value, mut builder: value::Builder) {
let value_span = match value {
@@ -24,6 +23,22 @@ pub(crate) fn serialize_value(value: &Value, mut builder: value::Builder) {
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() {
@@ -45,10 +60,10 @@ pub(crate) fn serialize_value(value: &Value, mut builder: value::Builder) {
span.set_end(value_span.end as u64);
}
pub(crate) fn deserialize_value(reader: value::Reader) -> Result<Value, PluginError> {
pub(crate) fn deserialize_value(reader: value::Reader) -> Result<Value, ShellError> {
let span_reader = reader
.get_span()
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
.map_err(|e| ShellError::InternalError(e.to_string()))?;
let span = Span {
start: span_reader.get_start() as usize,
@@ -62,17 +77,39 @@ pub(crate) fn deserialize_value(reader: value::Reader) -> Result<Value, PluginEr
Ok(value::Float(val)) => Ok(Value::Float { val, span }),
Ok(value::String(val)) => {
let string = val
.map_err(|e| PluginError::DecodingError(e.to_string()))?
.map_err(|e| ShellError::InternalError(e.to_string()))?
.to_string();
Ok(Value::String { val: string, span })
}
Ok(value::Record(record)) => {
let record = record.map_err(|e| ShellError::InternalError(e.to_string()))?;
let cols = record
.get_cols()
.map_err(|e| ShellError::InternalError(e.to_string()))?
.iter()
.map(|col| {
col.map_err(|e| ShellError::InternalError(e.to_string()))
.map(|col| col.to_string())
})
.collect::<Result<Vec<String>, ShellError>>()?;
let vals = record
.get_vals()
.map_err(|e| ShellError::InternalError(e.to_string()))?
.iter()
.map(deserialize_value)
.collect::<Result<Vec<Value>, ShellError>>()?;
Ok(Value::Record { cols, vals, span })
}
Ok(value::List(vals)) => {
let values = vals.map_err(|e| PluginError::DecodingError(e.to_string()))?;
let values = vals.map_err(|e| ShellError::InternalError(e.to_string()))?;
let values_list = values
.iter()
.map(deserialize_value)
.collect::<Result<Vec<Value>, PluginError>>()?;
.collect::<Result<Vec<Value>, ShellError>>()?;
Ok(Value::List {
vals: values_list,
@@ -91,10 +128,7 @@ mod tests {
use capnp::serialize;
use nu_protocol::{Span, Value};
pub fn write_buffer(
value: &Value,
writer: &mut impl std::io::Write,
) -> Result<(), PluginError> {
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>();
@@ -102,16 +136,16 @@ mod tests {
serialize_value(value, builder.reborrow());
serialize::write_message(writer, &message)
.map_err(|e| PluginError::EncodingError(e.to_string()))
.map_err(|e| ShellError::InternalError(e.to_string()))
}
pub fn read_buffer(reader: &mut impl std::io::BufRead) -> Result<Value, PluginError> {
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| PluginError::DecodingError(e.to_string()))?;
.map_err(|e| ShellError::InternalError(e.to_string()))?;
deserialize_value(reader.reborrow())
}
@@ -262,4 +296,70 @@ mod tests {
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)
}
}