mirror of
https://github.com/nushell/nushell.git
synced 2025-08-18 23:27:48 +02:00
call and response serializers
This commit is contained in:
@@ -1,21 +1,11 @@
|
||||
use crate::plugin::PluginError;
|
||||
use crate::plugin_capnp::{call, expression, option};
|
||||
use capnp::serialize_packed;
|
||||
use nu_protocol::{
|
||||
ast::{Call, Expr, Expression},
|
||||
ShellError, Span, Spanned, Type,
|
||||
Span, Spanned, Type,
|
||||
};
|
||||
|
||||
pub fn write_buffer(call: &Call, writer: &mut impl std::io::Write) -> Result<(), ShellError> {
|
||||
let mut message = ::capnp::message::Builder::new_default();
|
||||
|
||||
let builder = message.init_root::<call::Builder>();
|
||||
serialize_call(call, builder)?;
|
||||
|
||||
serialize_packed::write_message(writer, &message)
|
||||
.map_err(|e| ShellError::EncodingError(e.to_string()))
|
||||
}
|
||||
|
||||
pub(crate) fn serialize_call(call: &Call, mut builder: call::Builder) -> Result<(), ShellError> {
|
||||
pub(crate) fn serialize_call(call: &Call, mut builder: call::Builder) -> Result<(), PluginError> {
|
||||
let mut head = builder.reborrow().init_head();
|
||||
head.set_start(call.head.start as u64);
|
||||
head.set_end(call.head.end as u64);
|
||||
@@ -37,7 +27,7 @@ fn serialize_positional(positional: &[Expression], mut builder: call::Builder) {
|
||||
fn serialize_named(
|
||||
named: &[(Spanned<String>, Option<Expression>)],
|
||||
mut builder: call::Builder,
|
||||
) -> Result<(), ShellError> {
|
||||
) -> Result<(), PluginError> {
|
||||
let mut named_builder = builder
|
||||
.reborrow()
|
||||
.init_named()
|
||||
@@ -48,7 +38,7 @@ fn serialize_named(
|
||||
entry_builder
|
||||
.reborrow()
|
||||
.set_key(key.item.as_str())
|
||||
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
|
||||
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
|
||||
|
||||
let mut value_builder = entry_builder.init_value();
|
||||
match expression {
|
||||
@@ -84,21 +74,10 @@ fn serialize_expression(expression: &Expression, mut builder: expression::Builde
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_buffer(reader: &mut impl std::io::BufRead) -> Result<Call, ShellError> {
|
||||
let message_reader =
|
||||
serialize_packed::read_message(reader, ::capnp::message::ReaderOptions::new()).unwrap();
|
||||
|
||||
let reader = message_reader
|
||||
.get_root::<call::Reader>()
|
||||
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
|
||||
|
||||
deserialize_call(reader)
|
||||
}
|
||||
|
||||
pub(crate) fn deserialize_call(reader: call::Reader) -> Result<Call, ShellError> {
|
||||
pub(crate) fn deserialize_call(reader: call::Reader) -> Result<Call, PluginError> {
|
||||
let head_reader = reader
|
||||
.get_head()
|
||||
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
|
||||
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
|
||||
|
||||
let head = Span {
|
||||
start: head_reader.get_start() as usize,
|
||||
@@ -119,10 +98,10 @@ pub(crate) fn deserialize_call(reader: call::Reader) -> Result<Call, ShellError>
|
||||
fn deserialize_positionals(
|
||||
span: Span,
|
||||
reader: call::Reader,
|
||||
) -> Result<Vec<Expression>, ShellError> {
|
||||
) -> Result<Vec<Expression>, PluginError> {
|
||||
let positional_reader = reader
|
||||
.get_positional()
|
||||
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
|
||||
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
|
||||
|
||||
positional_reader
|
||||
.iter()
|
||||
@@ -132,14 +111,14 @@ fn deserialize_positionals(
|
||||
|
||||
type NamedList = Vec<(Spanned<String>, Option<Expression>)>;
|
||||
|
||||
fn deserialize_named(span: Span, reader: call::Reader) -> Result<NamedList, ShellError> {
|
||||
fn deserialize_named(span: Span, reader: call::Reader) -> Result<NamedList, PluginError> {
|
||||
let named_reader = reader
|
||||
.get_named()
|
||||
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
|
||||
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
|
||||
|
||||
let entries_list = named_reader
|
||||
.get_entries()
|
||||
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
|
||||
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
|
||||
|
||||
let mut entries: Vec<(Spanned<String>, Option<Expression>)> =
|
||||
Vec::with_capacity(entries_list.len() as usize);
|
||||
@@ -147,21 +126,21 @@ fn deserialize_named(span: Span, reader: call::Reader) -> Result<NamedList, Shel
|
||||
for entry_reader in entries_list {
|
||||
let item = entry_reader
|
||||
.get_key()
|
||||
.map_err(|e| ShellError::DecodingError(e.to_string()))?
|
||||
.map_err(|e| PluginError::DecodingError(e.to_string()))?
|
||||
.to_string();
|
||||
|
||||
let value_reader = entry_reader
|
||||
.get_value()
|
||||
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
|
||||
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
|
||||
|
||||
let value = match value_reader.which() {
|
||||
Ok(option::None(())) => None,
|
||||
Ok(option::Some(expression_reader)) => {
|
||||
let expression_reader =
|
||||
expression_reader.map_err(|e| ShellError::DecodingError(e.to_string()))?;
|
||||
expression_reader.map_err(|e| PluginError::DecodingError(e.to_string()))?;
|
||||
|
||||
let expression = deserialize_expression(span, expression_reader)
|
||||
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
|
||||
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
|
||||
|
||||
Some(expression)
|
||||
}
|
||||
@@ -179,7 +158,7 @@ fn deserialize_named(span: Span, reader: call::Reader) -> Result<NamedList, Shel
|
||||
fn deserialize_expression(
|
||||
span: Span,
|
||||
reader: expression::Reader,
|
||||
) -> Result<Expression, ShellError> {
|
||||
) -> Result<Expression, PluginError> {
|
||||
let expr = match reader.which() {
|
||||
Ok(expression::Garbage(())) => Expr::Garbage,
|
||||
Ok(expression::Bool(val)) => Expr::Bool(val),
|
||||
@@ -187,18 +166,18 @@ fn deserialize_expression(
|
||||
Ok(expression::Float(val)) => Expr::Float(val),
|
||||
Ok(expression::String(val)) => {
|
||||
let string = val
|
||||
.map_err(|e| ShellError::DecodingError(e.to_string()))?
|
||||
.map_err(|e| PluginError::DecodingError(e.to_string()))?
|
||||
.to_string();
|
||||
|
||||
Expr::String(string)
|
||||
}
|
||||
Ok(expression::List(values)) => {
|
||||
let values = values.map_err(|e| ShellError::DecodingError(e.to_string()))?;
|
||||
let values = values.map_err(|e| PluginError::DecodingError(e.to_string()))?;
|
||||
|
||||
let values_list = values
|
||||
.iter()
|
||||
.map(|inner_reader| deserialize_expression(span, inner_reader))
|
||||
.collect::<Result<Vec<Expression>, ShellError>>()?;
|
||||
.collect::<Result<Vec<Expression>, PluginError>>()?;
|
||||
|
||||
Expr::List(values_list)
|
||||
}
|
||||
@@ -215,6 +194,7 @@ fn deserialize_expression(
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use capnp::serialize_packed;
|
||||
use core::panic;
|
||||
|
||||
use super::*;
|
||||
@@ -223,6 +203,27 @@ mod tests {
|
||||
Span, Spanned,
|
||||
};
|
||||
|
||||
fn write_buffer(call: &Call, writer: &mut impl std::io::Write) -> Result<(), PluginError> {
|
||||
let mut message = ::capnp::message::Builder::new_default();
|
||||
|
||||
let builder = message.init_root::<call::Builder>();
|
||||
serialize_call(call, builder)?;
|
||||
|
||||
serialize_packed::write_message(writer, &message)
|
||||
.map_err(|e| PluginError::EncodingError(e.to_string()))
|
||||
}
|
||||
|
||||
fn read_buffer(reader: &mut impl std::io::BufRead) -> Result<Call, PluginError> {
|
||||
let message_reader =
|
||||
serialize_packed::read_message(reader, ::capnp::message::ReaderOptions::new()).unwrap();
|
||||
|
||||
let reader = message_reader
|
||||
.get_root::<call::Reader>()
|
||||
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
|
||||
|
||||
deserialize_call(reader)
|
||||
}
|
||||
|
||||
fn compare_expressions(lhs: &Expression, rhs: &Expression) {
|
||||
match (&lhs.expr, &rhs.expr) {
|
||||
(Expr::Bool(a), Expr::Bool(b)) => assert_eq!(a, b),
|
||||
|
@@ -1,147 +0,0 @@
|
||||
use super::{call, value};
|
||||
use crate::plugin_capnp::call_info;
|
||||
use capnp::serialize_packed;
|
||||
use nu_protocol::{ast::Call, ShellError, Value};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CallInfo {
|
||||
pub call: Call,
|
||||
pub input: Value,
|
||||
}
|
||||
|
||||
pub fn write_buffer(
|
||||
call: &Call,
|
||||
input: &Value,
|
||||
writer: &mut impl std::io::Write,
|
||||
) -> Result<(), ShellError> {
|
||||
let mut message = ::capnp::message::Builder::new_default();
|
||||
|
||||
let mut builder = message.init_root::<call_info::Builder>();
|
||||
let value_builder = builder
|
||||
.reborrow()
|
||||
.get_input()
|
||||
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
|
||||
|
||||
value::serialize_value(input, value_builder);
|
||||
|
||||
let call_builder = builder
|
||||
.reborrow()
|
||||
.get_call()
|
||||
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
|
||||
|
||||
call::serialize_call(call, call_builder)
|
||||
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
|
||||
|
||||
serialize_packed::write_message(writer, &message)
|
||||
.map_err(|e| ShellError::EncodingError(e.to_string()))
|
||||
}
|
||||
|
||||
pub fn read_buffer(reader: &mut impl std::io::BufRead) -> Result<CallInfo, ShellError> {
|
||||
let message_reader =
|
||||
serialize_packed::read_message(reader, ::capnp::message::ReaderOptions::new()).unwrap();
|
||||
|
||||
let reader = message_reader
|
||||
.get_root::<call_info::Reader>()
|
||||
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
|
||||
|
||||
let call_reader = reader
|
||||
.get_call()
|
||||
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
|
||||
|
||||
let call = call::deserialize_call(call_reader)
|
||||
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
|
||||
|
||||
let value_reader = reader
|
||||
.get_input()
|
||||
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
|
||||
|
||||
let input = value::deserialize_value(value_reader)
|
||||
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
|
||||
|
||||
Ok(CallInfo { call, input })
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use nu_protocol::{
|
||||
ast::{Call, Expr, Expression},
|
||||
Span, Spanned, Value,
|
||||
};
|
||||
|
||||
fn compare_expressions(lhs: &Expression, rhs: &Expression) {
|
||||
match (&lhs.expr, &rhs.expr) {
|
||||
(Expr::Bool(a), Expr::Bool(b)) => assert_eq!(a, b),
|
||||
(Expr::Int(a), Expr::Int(b)) => assert_eq!(a, b),
|
||||
(Expr::Float(a), Expr::Float(b)) => assert_eq!(a, b),
|
||||
(Expr::String(a), Expr::String(b)) => assert_eq!(a, b),
|
||||
_ => panic!("not matching values"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn callinfo_round_trip() {
|
||||
let input = Value::Bool {
|
||||
val: false,
|
||||
span: Span { start: 1, end: 20 },
|
||||
};
|
||||
|
||||
let call = Call {
|
||||
decl_id: 1,
|
||||
head: Span { start: 0, end: 10 },
|
||||
positional: vec![
|
||||
Expression {
|
||||
expr: Expr::Float(1.0),
|
||||
span: Span { start: 0, end: 10 },
|
||||
ty: nu_protocol::Type::Float,
|
||||
custom_completion: None,
|
||||
},
|
||||
Expression {
|
||||
expr: Expr::String("something".into()),
|
||||
span: Span { start: 0, end: 10 },
|
||||
ty: nu_protocol::Type::Float,
|
||||
custom_completion: None,
|
||||
},
|
||||
],
|
||||
named: vec![(
|
||||
Spanned {
|
||||
item: "name".to_string(),
|
||||
span: Span { start: 0, end: 10 },
|
||||
},
|
||||
Some(Expression {
|
||||
expr: Expr::Float(1.0),
|
||||
span: Span { start: 0, end: 10 },
|
||||
ty: nu_protocol::Type::Float,
|
||||
custom_completion: None,
|
||||
}),
|
||||
)],
|
||||
};
|
||||
|
||||
let mut buffer: Vec<u8> = Vec::new();
|
||||
write_buffer(&call, &input, &mut buffer).expect("unable to serialize message");
|
||||
let call_info = read_buffer(&mut buffer.as_slice()).expect("unable to read message");
|
||||
|
||||
assert_eq!(input, call_info.input);
|
||||
assert_eq!(call.head, call_info.call.head);
|
||||
assert_eq!(call.positional.len(), call_info.call.positional.len());
|
||||
|
||||
call.positional
|
||||
.iter()
|
||||
.zip(call_info.call.positional.iter())
|
||||
.for_each(|(lhs, rhs)| compare_expressions(lhs, rhs));
|
||||
|
||||
call.named
|
||||
.iter()
|
||||
.zip(call_info.call.named.iter())
|
||||
.for_each(|(lhs, rhs)| {
|
||||
// Comparing the keys
|
||||
assert_eq!(lhs.0.item, rhs.0.item);
|
||||
|
||||
match (&lhs.1, &rhs.1) {
|
||||
(None, None) => {}
|
||||
(Some(a), Some(b)) => compare_expressions(a, b),
|
||||
_ => panic!("not matching values"),
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@@ -1,4 +1,3 @@
|
||||
pub mod call;
|
||||
pub mod callinfo;
|
||||
pub mod signature;
|
||||
pub mod value;
|
||||
|
@@ -1,20 +1,6 @@
|
||||
use crate::plugin::PluginError;
|
||||
use crate::plugin_capnp::{argument, flag, option, signature, Shape};
|
||||
use capnp::serialize_packed;
|
||||
use nu_protocol::{Flag, PositionalArg, ShellError, Signature, SyntaxShape};
|
||||
|
||||
pub fn write_buffer(
|
||||
signature: &Signature,
|
||||
writer: &mut impl std::io::Write,
|
||||
) -> Result<(), ShellError> {
|
||||
let mut message = ::capnp::message::Builder::new_default();
|
||||
|
||||
let builder = message.init_root::<signature::Builder>();
|
||||
|
||||
serialize_signature(signature, builder);
|
||||
|
||||
serialize_packed::write_message(writer, &message)
|
||||
.map_err(|e| ShellError::EncodingError(e.to_string()))
|
||||
}
|
||||
use nu_protocol::{Flag, PositionalArg, Signature, SyntaxShape};
|
||||
|
||||
pub(crate) fn serialize_signature(signature: &Signature, mut builder: signature::Builder) {
|
||||
builder.set_name(signature.name.as_str());
|
||||
@@ -100,59 +86,48 @@ fn serialize_flag(arg: &Flag, mut builder: flag::Builder) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_buffer(reader: &mut impl std::io::BufRead) -> Result<Signature, ShellError> {
|
||||
let message_reader =
|
||||
serialize_packed::read_message(reader, ::capnp::message::ReaderOptions::new()).unwrap();
|
||||
|
||||
let reader = message_reader
|
||||
.get_root::<signature::Reader>()
|
||||
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
|
||||
|
||||
deserialize_signature(reader)
|
||||
}
|
||||
|
||||
pub(crate) fn deserialize_signature(reader: signature::Reader) -> Result<Signature, ShellError> {
|
||||
pub(crate) fn deserialize_signature(reader: signature::Reader) -> Result<Signature, PluginError> {
|
||||
let name = reader
|
||||
.get_name()
|
||||
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
|
||||
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
|
||||
let usage = reader
|
||||
.get_usage()
|
||||
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
|
||||
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
|
||||
let extra_usage = reader
|
||||
.get_extra_usage()
|
||||
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
|
||||
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
|
||||
let is_filter = reader.get_is_filter();
|
||||
|
||||
// Deserializing required arguments
|
||||
let required_list = reader
|
||||
.get_required_positional()
|
||||
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
|
||||
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
|
||||
|
||||
let required_positional = required_list
|
||||
.iter()
|
||||
.map(deserialize_argument)
|
||||
.collect::<Result<Vec<PositionalArg>, ShellError>>()?;
|
||||
.collect::<Result<Vec<PositionalArg>, PluginError>>()?;
|
||||
|
||||
// Deserializing optional arguments
|
||||
let optional_list = reader
|
||||
.get_optional_positional()
|
||||
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
|
||||
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
|
||||
|
||||
let optional_positional = optional_list
|
||||
.iter()
|
||||
.map(deserialize_argument)
|
||||
.collect::<Result<Vec<PositionalArg>, ShellError>>()?;
|
||||
.collect::<Result<Vec<PositionalArg>, PluginError>>()?;
|
||||
|
||||
// Deserializing rest arguments
|
||||
let rest_option = reader
|
||||
.get_rest()
|
||||
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
|
||||
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
|
||||
|
||||
let rest_positional = match rest_option.which() {
|
||||
Err(capnp::NotInSchema(_)) => None,
|
||||
Ok(option::None(())) => None,
|
||||
Ok(option::Some(rest_reader)) => {
|
||||
let rest_reader = rest_reader.map_err(|e| ShellError::EncodingError(e.to_string()))?;
|
||||
let rest_reader = rest_reader.map_err(|e| PluginError::EncodingError(e.to_string()))?;
|
||||
Some(deserialize_argument(rest_reader)?)
|
||||
}
|
||||
};
|
||||
@@ -160,12 +135,12 @@ pub(crate) fn deserialize_signature(reader: signature::Reader) -> Result<Signatu
|
||||
// Deserializing named arguments
|
||||
let named_list = reader
|
||||
.get_named()
|
||||
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
|
||||
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
|
||||
|
||||
let named = named_list
|
||||
.iter()
|
||||
.map(deserialize_flag)
|
||||
.collect::<Result<Vec<Flag>, ShellError>>()?;
|
||||
.collect::<Result<Vec<Flag>, PluginError>>()?;
|
||||
|
||||
Ok(Signature {
|
||||
name: name.to_string(),
|
||||
@@ -180,18 +155,18 @@ pub(crate) fn deserialize_signature(reader: signature::Reader) -> Result<Signatu
|
||||
})
|
||||
}
|
||||
|
||||
fn deserialize_argument(reader: argument::Reader) -> Result<PositionalArg, ShellError> {
|
||||
fn deserialize_argument(reader: argument::Reader) -> Result<PositionalArg, PluginError> {
|
||||
let name = reader
|
||||
.get_name()
|
||||
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
|
||||
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
|
||||
|
||||
let desc = reader
|
||||
.get_desc()
|
||||
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
|
||||
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
|
||||
|
||||
let shape = reader
|
||||
.get_shape()
|
||||
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
|
||||
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
|
||||
|
||||
let shape = match shape {
|
||||
Shape::String => SyntaxShape::String,
|
||||
@@ -210,33 +185,33 @@ fn deserialize_argument(reader: argument::Reader) -> Result<PositionalArg, Shell
|
||||
})
|
||||
}
|
||||
|
||||
fn deserialize_flag(reader: flag::Reader) -> Result<Flag, ShellError> {
|
||||
fn deserialize_flag(reader: flag::Reader) -> Result<Flag, PluginError> {
|
||||
let long = reader
|
||||
.get_long()
|
||||
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
|
||||
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
|
||||
|
||||
let desc = reader
|
||||
.get_desc()
|
||||
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
|
||||
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
|
||||
|
||||
let required = reader.get_required();
|
||||
|
||||
let short = reader
|
||||
.get_short()
|
||||
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
|
||||
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
|
||||
|
||||
let short = match short.which() {
|
||||
Err(capnp::NotInSchema(_)) => None,
|
||||
Ok(option::None(())) => None,
|
||||
Ok(option::Some(reader)) => {
|
||||
let reader = reader.map_err(|e| ShellError::EncodingError(e.to_string()))?;
|
||||
let reader = reader.map_err(|e| PluginError::EncodingError(e.to_string()))?;
|
||||
reader.chars().next()
|
||||
}
|
||||
};
|
||||
|
||||
let arg = reader
|
||||
.get_arg()
|
||||
.map_err(|e| ShellError::EncodingError(e.to_string()))?;
|
||||
.map_err(|e| PluginError::EncodingError(e.to_string()))?;
|
||||
|
||||
let arg = match arg {
|
||||
Shape::None => None,
|
||||
@@ -260,8 +235,34 @@ fn deserialize_flag(reader: flag::Reader) -> Result<Flag, ShellError> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use capnp::serialize_packed;
|
||||
use nu_protocol::{Signature, SyntaxShape};
|
||||
|
||||
pub fn write_buffer(
|
||||
signature: &Signature,
|
||||
writer: &mut impl std::io::Write,
|
||||
) -> Result<(), PluginError> {
|
||||
let mut message = ::capnp::message::Builder::new_default();
|
||||
|
||||
let builder = message.init_root::<signature::Builder>();
|
||||
|
||||
serialize_signature(signature, builder);
|
||||
|
||||
serialize_packed::write_message(writer, &message)
|
||||
.map_err(|e| PluginError::EncodingError(e.to_string()))
|
||||
}
|
||||
|
||||
pub fn read_buffer(reader: &mut impl std::io::BufRead) -> Result<Signature, PluginError> {
|
||||
let message_reader =
|
||||
serialize_packed::read_message(reader, ::capnp::message::ReaderOptions::new()).unwrap();
|
||||
|
||||
let reader = message_reader
|
||||
.get_root::<signature::Reader>()
|
||||
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
|
||||
|
||||
deserialize_signature(reader)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn value_round_trip() {
|
||||
let signature = Signature::build("nu-plugin")
|
||||
|
@@ -1,20 +1,6 @@
|
||||
use crate::plugin::PluginError;
|
||||
use crate::plugin_capnp::value;
|
||||
use capnp::serialize_packed;
|
||||
use nu_protocol::{ShellError, Span, Value};
|
||||
|
||||
pub fn write_buffer(value: &Value, writer: &mut impl std::io::Write) -> Result<(), ShellError> {
|
||||
let mut message = ::capnp::message::Builder::new_default();
|
||||
|
||||
let mut builder = message.init_root::<value::Builder>();
|
||||
|
||||
let value_span = serialize_value(value, builder.reborrow());
|
||||
let mut span = builder.reborrow().init_span();
|
||||
span.set_start(value_span.start as u64);
|
||||
span.set_end(value_span.end as u64);
|
||||
|
||||
serialize_packed::write_message(writer, &message)
|
||||
.map_err(|e| ShellError::EncodingError(e.to_string()))
|
||||
}
|
||||
use nu_protocol::{Span, Value};
|
||||
|
||||
pub(crate) fn serialize_value(value: &Value, mut builder: value::Builder) -> Span {
|
||||
match value {
|
||||
@@ -55,21 +41,10 @@ pub(crate) fn serialize_value(value: &Value, mut builder: value::Builder) -> Spa
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_buffer(reader: &mut impl std::io::BufRead) -> Result<Value, ShellError> {
|
||||
let message_reader =
|
||||
serialize_packed::read_message(reader, ::capnp::message::ReaderOptions::new()).unwrap();
|
||||
|
||||
let reader = message_reader
|
||||
.get_root::<value::Reader>()
|
||||
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
|
||||
|
||||
deserialize_value(reader.reborrow())
|
||||
}
|
||||
|
||||
pub(crate) fn deserialize_value(reader: value::Reader) -> Result<Value, ShellError> {
|
||||
pub(crate) fn deserialize_value(reader: value::Reader) -> Result<Value, PluginError> {
|
||||
let span_reader = reader
|
||||
.get_span()
|
||||
.map_err(|e| ShellError::DecodingError(e.to_string()))?;
|
||||
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
|
||||
|
||||
let span = Span {
|
||||
start: span_reader.get_start() as usize,
|
||||
@@ -83,17 +58,17 @@ pub(crate) fn deserialize_value(reader: value::Reader) -> Result<Value, ShellErr
|
||||
Ok(value::Float(val)) => Ok(Value::Float { val, span }),
|
||||
Ok(value::String(val)) => {
|
||||
let string = val
|
||||
.map_err(|e| ShellError::DecodingError(e.to_string()))?
|
||||
.map_err(|e| PluginError::DecodingError(e.to_string()))?
|
||||
.to_string();
|
||||
Ok(Value::String { val: string, span })
|
||||
}
|
||||
Ok(value::List(vals)) => {
|
||||
let values = vals.map_err(|e| ShellError::DecodingError(e.to_string()))?;
|
||||
let values = vals.map_err(|e| PluginError::DecodingError(e.to_string()))?;
|
||||
|
||||
let values_list = values
|
||||
.iter()
|
||||
.map(deserialize_value)
|
||||
.collect::<Result<Vec<Value>, ShellError>>()?;
|
||||
.collect::<Result<Vec<Value>, PluginError>>()?;
|
||||
|
||||
Ok(Value::List {
|
||||
vals: values_list,
|
||||
@@ -109,8 +84,37 @@ pub(crate) fn deserialize_value(reader: value::Reader) -> Result<Value, ShellErr
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use capnp::serialize_packed;
|
||||
use nu_protocol::{Span, Value};
|
||||
|
||||
pub fn write_buffer(
|
||||
value: &Value,
|
||||
writer: &mut impl std::io::Write,
|
||||
) -> Result<(), PluginError> {
|
||||
let mut message = ::capnp::message::Builder::new_default();
|
||||
|
||||
let mut builder = message.init_root::<value::Builder>();
|
||||
|
||||
let value_span = serialize_value(value, builder.reborrow());
|
||||
let mut span = builder.reborrow().init_span();
|
||||
span.set_start(value_span.start as u64);
|
||||
span.set_end(value_span.end as u64);
|
||||
|
||||
serialize_packed::write_message(writer, &message)
|
||||
.map_err(|e| PluginError::EncodingError(e.to_string()))
|
||||
}
|
||||
|
||||
pub fn read_buffer(reader: &mut impl std::io::BufRead) -> Result<Value, PluginError> {
|
||||
let message_reader =
|
||||
serialize_packed::read_message(reader, ::capnp::message::ReaderOptions::new()).unwrap();
|
||||
|
||||
let reader = message_reader
|
||||
.get_root::<value::Reader>()
|
||||
.map_err(|e| PluginError::DecodingError(e.to_string()))?;
|
||||
|
||||
deserialize_value(reader.reborrow())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn value_round_trip() {
|
||||
let values = [
|
||||
|
Reference in New Issue
Block a user