diff --git a/crates/nu-plugin/schema/plugin.capnp b/crates/nu-plugin/schema/plugin.capnp index ed4782260..2bc2bfd93 100644 --- a/crates/nu-plugin/schema/plugin.capnp +++ b/crates/nu-plugin/schema/plugin.capnp @@ -9,14 +9,6 @@ # 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 -# Generic structs used as helpers for the encoding -struct Option(T) { - union { - none @0 :Void; - some @1 :T; - } -} - struct Err(T) { union { err @0 :Text; @@ -59,14 +51,14 @@ struct Signature { extraUsage @2 :Text; requiredPositional @3 :List(Argument); optionalPositional @4 :List(Argument); - rest @5 :Option(Argument); + rest @5 :Argument; # Optional value. Check for existence when deserializing named @6 :List(Flag); isFilter @7 :Bool; } struct Flag { long @0 :Text; - short @1 :Option(Text); + short @1 :Text; # Optional value. Check for existence when deserializing arg @2 :Shape; required @3 :Bool; desc @4 :Text; @@ -107,7 +99,9 @@ struct Expression { struct Call { head @0: Span; positional @1 :List(Expression); - named @2 :Map(Text, Option(Expression)); + # The expression in the map can be optional + # Check for existence when deserializing + named @2 :Map(Text, Expression); } struct CallInfo { diff --git a/crates/nu-plugin/src/plugin.rs b/crates/nu-plugin/src/plugin.rs index efa14094d..9e5a68af5 100644 --- a/crates/nu-plugin/src/plugin.rs +++ b/crates/nu-plugin/src/plugin.rs @@ -65,47 +65,42 @@ impl Display for PluginError { pub fn get_signature(path: &Path) -> Result, PluginError> { let mut plugin_cmd = create_command(path); - // Both stdout and stdin are piped so we can get the information from the plugin - plugin_cmd.stdout(Stdio::piped()); - plugin_cmd.stdin(Stdio::piped()); + let mut child = plugin_cmd + .spawn() + .map_err(|err| PluginError::UnableToSpawn(format!("{}", err)))?; - match plugin_cmd.spawn() { - Err(err) => Err(PluginError::UnableToSpawn(format!("{}", err))), - Ok(mut child) => { - // 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() { - plugin_call::encode_call(&PluginCall::Signature, &mut stdin_writer)? - } - - // deserialize response from plugin to extract the signature - let signature = if let Some(stdout_reader) = child.stdout.take() { - let mut buf_read = BufReader::with_capacity(OUTPUT_BUFFER_SIZE, stdout_reader); - let response = plugin_call::decode_response(&mut buf_read)?; - - match response { - PluginResponse::Signature(sign) => Ok(sign), - PluginResponse::Error(msg) => Err(PluginError::DecodingError(msg)), - _ => Err(PluginError::DecodingError("signature not found".into())), - } - } else { - Err(PluginError::UnableToGetStdout) - }?; - - match child.wait() { - Err(err) => Err(PluginError::UnableToSpawn(format!("{}", err))), - Ok(_) => Ok(signature), - } - } + // Create message to plugin to indicate that signature is required and + // send call to plugin asking for signature + if let Some(stdin_writer) = &mut child.stdin { + let mut writer = stdin_writer; + plugin_call::encode_call(&PluginCall::Signature, &mut writer)? } + + // deserialize response from plugin to extract the signature + let signature = 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 = plugin_call::decode_response(&mut buf_read)?; + + match response { + PluginResponse::Signature(sign) => Ok(sign), + PluginResponse::Error(msg) => Err(PluginError::DecodingError(msg)), + _ => Err(PluginError::DecodingError("signature not found".into())), + } + } else { + Err(PluginError::UnableToGetStdout) + }?; + + // There is no need to wait for the child process to finish since the + // signature has being collected + Ok(signature) } fn create_command(path: &Path) -> CommandSys { //TODO. The selection of shell could be modifiable from the config file. - if cfg!(windows) { + let mut process = if cfg!(windows) { let mut process = CommandSys::new("cmd"); - process.arg("/c"); - process.arg(path); + process.arg("/c").arg(path); process } else { @@ -113,7 +108,12 @@ fn create_command(path: &Path) -> CommandSys { process.arg("-c").arg(path); process - } + }; + + // Both stdout and stdin are piped so we can receive information from the plugin + process.stdout(Stdio::piped()).stdin(Stdio::piped()); + + process } #[derive(Debug, Clone)] @@ -159,65 +159,60 @@ impl Command for PluginDeclaration { let source_file = Path::new(&self.filename); let mut plugin_cmd = create_command(source_file); - // Both stdout and stdin are piped so we can get the information from the plugin - plugin_cmd.stdout(Stdio::piped()); - plugin_cmd.stdin(Stdio::piped()); + let mut child = plugin_cmd + .spawn() + .map_err(|err| ShellError::PluginError(format!("{}", err)))?; - match plugin_cmd.spawn() { - Err(err) => Err(ShellError::PluginError(format!("{}", err))), - Ok(mut child) => { - let input = match input { - PipelineData::Value(value) => value, - PipelineData::Stream(stream) => { - let values = stream.collect::>(); + let input = match input { + PipelineData::Value(value) => value, + PipelineData::Stream(stream) => { + let values = stream.collect::>(); - Value::List { - vals: values, - span: call.head, - } - } - }; - - // PluginCall information - let plugin_call = PluginCall::CallInfo(Box::new(CallInfo { - name: self.name.clone(), - call: call.clone(), - input, - })); - - // 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() { - plugin_call::encode_call(&plugin_call, &mut stdin_writer) - .map_err(|err| ShellError::PluginError(err.to_string()))? - } - - // Deserialize response from plugin to extract the resulting value - let pipeline_data = if let Some(stdout_reader) = child.stdout.take() { - let mut buf_read = BufReader::with_capacity(OUTPUT_BUFFER_SIZE, stdout_reader); - let response = plugin_call::decode_response(&mut buf_read) - .map_err(|err| ShellError::PluginError(err.to_string()))?; - - match response { - PluginResponse::Value(value) => { - Ok(PipelineData::Value(value.as_ref().clone())) - } - PluginResponse::Error(msg) => Err(PluginError::DecodingError(msg)), - _ => Err(PluginError::DecodingError( - "result value from plugin not found".into(), - )), - } - } else { - Err(PluginError::UnableToGetStdout) - } - .map_err(|err| ShellError::PluginError(err.to_string()))?; - - match child.wait() { - Err(err) => Err(ShellError::PluginError(format!("{}", err))), - Ok(_) => Ok(pipeline_data), + Value::List { + vals: values, + span: call.head, } } + }; + + // Create message to plugin to indicate that signature is required and + // send call to plugin asking for signature + if let Some(stdin_writer) = &mut child.stdin { + // PluginCall information + let plugin_call = PluginCall::CallInfo(Box::new(CallInfo { + name: self.name.clone(), + call: call.clone(), + input, + })); + + let mut writer = stdin_writer; + + plugin_call::encode_call(&plugin_call, &mut writer) + .map_err(|err| ShellError::PluginError(err.to_string()))?; } + + // 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 = plugin_call::decode_response(&mut buf_read) + .map_err(|err| ShellError::PluginError(err.to_string()))?; + + match response { + PluginResponse::Value(value) => Ok(PipelineData::Value(value.as_ref().clone())), + PluginResponse::Error(msg) => Err(PluginError::DecodingError(msg)), + _ => Err(PluginError::DecodingError( + "result value from plugin not found".into(), + )), + } + } else { + Err(PluginError::UnableToGetStdout) + } + .map_err(|err| ShellError::PluginError(err.to_string()))?; + + // There is no need to wait for the child process to finish + // The response has been collected from the plugin call + Ok(pipeline_data) } fn is_plugin(&self) -> bool { diff --git a/crates/nu-plugin/src/plugin_call.rs b/crates/nu-plugin/src/plugin_call.rs index 19e0c73d6..4feb8a928 100644 --- a/crates/nu-plugin/src/plugin_call.rs +++ b/crates/nu-plugin/src/plugin_call.rs @@ -2,7 +2,7 @@ use crate::plugin::{CallInfo, PluginCall, PluginError, PluginResponse}; use crate::plugin_capnp::{plugin_call, plugin_response}; use crate::serializers::signature::deserialize_signature; use crate::serializers::{call, signature, value}; -use capnp::serialize_packed; +use capnp::serialize; use nu_protocol::Signature; pub fn encode_call( @@ -40,13 +40,13 @@ pub fn encode_call( } }; - serialize_packed::write_message(writer, &message) + serialize::write_message(writer, &message) .map_err(|e| PluginError::EncodingError(e.to_string())) } pub fn decode_call(reader: &mut impl std::io::BufRead) -> Result { - let message_reader = - serialize_packed::read_message(reader, ::capnp::message::ReaderOptions::new()).unwrap(); + let message_reader = serialize::read_message(reader, ::capnp::message::ReaderOptions::new()) + .map_err(|e| PluginError::DecodingError(e.to_string()))?; let reader = message_reader .get_root::() @@ -110,13 +110,13 @@ pub fn encode_response( } }; - serialize_packed::write_message(writer, &message) + serialize::write_message(writer, &message) .map_err(|e| PluginError::EncodingError(e.to_string())) } pub fn decode_response(reader: &mut impl std::io::BufRead) -> Result { - let message_reader = - serialize_packed::read_message(reader, ::capnp::message::ReaderOptions::new()).unwrap(); + let message_reader = serialize::read_message(reader, ::capnp::message::ReaderOptions::new()) + .map_err(|e| PluginError::DecodingError(e.to_string()))?; let reader = message_reader .get_root::() diff --git a/crates/nu-plugin/src/plugin_capnp.rs b/crates/nu-plugin/src/plugin_capnp.rs index 4aca078fb..19f22aa71 100644 --- a/crates/nu-plugin/src/plugin_capnp.rs +++ b/crates/nu-plugin/src/plugin_capnp.rs @@ -2,309 +2,6 @@ // DO NOT EDIT. // source: plugin.capnp -pub mod option { - /* T */ - pub use self::Which::{None, Some}; - - #[derive(Copy, Clone)] - pub struct Owned { - _phantom: ::core::marker::PhantomData, - } - impl<'a, T> ::capnp::traits::Owned<'a> for Owned - where - T: for<'c> ::capnp::traits::Owned<'c>, - { - type Reader = Reader<'a, T>; - type Builder = Builder<'a, T>; - } - impl<'a, T> ::capnp::traits::OwnedStruct<'a> for Owned - where - T: for<'c> ::capnp::traits::Owned<'c>, - { - type Reader = Reader<'a, T>; - type Builder = Builder<'a, T>; - } - impl ::capnp::traits::Pipelined for Owned - where - T: for<'c> ::capnp::traits::Owned<'c>, - { - type Pipeline = Pipeline; - } - - #[derive(Clone, Copy)] - pub struct Reader<'a, T> - where - T: for<'c> ::capnp::traits::Owned<'c>, - { - reader: ::capnp::private::layout::StructReader<'a>, - _phantom: ::core::marker::PhantomData, - } - - impl<'a, T> ::capnp::traits::HasTypeId for Reader<'a, T> - where - T: for<'c> ::capnp::traits::Owned<'c>, - { - #[inline] - fn type_id() -> u64 { - _private::TYPE_ID - } - } - impl<'a, T> ::capnp::traits::FromStructReader<'a> for Reader<'a, T> - where - T: for<'c> ::capnp::traits::Owned<'c>, - { - fn new(reader: ::capnp::private::layout::StructReader<'a>) -> Reader<'a, T> { - Reader { - reader, - _phantom: ::core::marker::PhantomData, - } - } - } - - impl<'a, T> ::capnp::traits::FromPointerReader<'a> for Reader<'a, T> - where - T: for<'c> ::capnp::traits::Owned<'c>, - { - fn get_from_pointer( - reader: &::capnp::private::layout::PointerReader<'a>, - default: ::core::option::Option<&'a [capnp::Word]>, - ) -> ::capnp::Result> { - ::core::result::Result::Ok(::capnp::traits::FromStructReader::new( - reader.get_struct(default)?, - )) - } - } - - impl<'a, T> ::capnp::traits::IntoInternalStructReader<'a> for Reader<'a, T> - where - T: for<'c> ::capnp::traits::Owned<'c>, - { - fn into_internal_struct_reader(self) -> ::capnp::private::layout::StructReader<'a> { - self.reader - } - } - - impl<'a, T> ::capnp::traits::Imbue<'a> for Reader<'a, T> - where - T: for<'c> ::capnp::traits::Owned<'c>, - { - fn imbue(&mut self, cap_table: &'a ::capnp::private::layout::CapTable) { - self.reader - .imbue(::capnp::private::layout::CapTableReader::Plain(cap_table)) - } - } - - impl<'a, T> Reader<'a, T> - where - T: for<'c> ::capnp::traits::Owned<'c>, - { - pub fn reborrow(&self) -> Reader<'_, T> { - Reader { ..*self } - } - - pub fn total_size(&self) -> ::capnp::Result<::capnp::MessageSize> { - self.reader.total_size() - } - pub fn has_some(&self) -> bool { - if self.reader.get_data_field::(0) != 1 { - return false; - } - !self.reader.get_pointer_field(0).is_null() - } - #[inline] - pub fn which(self) -> ::core::result::Result, ::capnp::NotInSchema> { - match self.reader.get_data_field::(0) { - 0 => ::core::result::Result::Ok(None(())), - 1 => ::core::result::Result::Ok(Some( - ::capnp::traits::FromPointerReader::get_from_pointer( - &self.reader.get_pointer_field(0), - ::core::option::Option::None, - ), - )), - x => ::core::result::Result::Err(::capnp::NotInSchema(x)), - } - } - } - - pub struct Builder<'a, T> - where - T: for<'c> ::capnp::traits::Owned<'c>, - { - builder: ::capnp::private::layout::StructBuilder<'a>, - _phantom: ::core::marker::PhantomData, - } - impl<'a, T> ::capnp::traits::HasStructSize for Builder<'a, T> - where - T: for<'c> ::capnp::traits::Owned<'c>, - { - #[inline] - fn struct_size() -> ::capnp::private::layout::StructSize { - _private::STRUCT_SIZE - } - } - impl<'a, T> ::capnp::traits::HasTypeId for Builder<'a, T> - where - T: for<'c> ::capnp::traits::Owned<'c>, - { - #[inline] - fn type_id() -> u64 { - _private::TYPE_ID - } - } - impl<'a, T> ::capnp::traits::FromStructBuilder<'a> for Builder<'a, T> - where - T: for<'c> ::capnp::traits::Owned<'c>, - { - fn new(builder: ::capnp::private::layout::StructBuilder<'a>) -> Builder<'a, T> { - Builder { - builder, - _phantom: ::core::marker::PhantomData, - } - } - } - - impl<'a, T> ::capnp::traits::ImbueMut<'a> for Builder<'a, T> - where - T: for<'c> ::capnp::traits::Owned<'c>, - { - fn imbue_mut(&mut self, cap_table: &'a mut ::capnp::private::layout::CapTable) { - self.builder - .imbue(::capnp::private::layout::CapTableBuilder::Plain(cap_table)) - } - } - - impl<'a, T> ::capnp::traits::FromPointerBuilder<'a> for Builder<'a, T> - where - T: for<'c> ::capnp::traits::Owned<'c>, - { - fn init_pointer( - builder: ::capnp::private::layout::PointerBuilder<'a>, - _size: u32, - ) -> Builder<'a, T> { - ::capnp::traits::FromStructBuilder::new(builder.init_struct(_private::STRUCT_SIZE)) - } - fn get_from_pointer( - builder: ::capnp::private::layout::PointerBuilder<'a>, - default: ::core::option::Option<&'a [capnp::Word]>, - ) -> ::capnp::Result> { - ::core::result::Result::Ok(::capnp::traits::FromStructBuilder::new( - builder.get_struct(_private::STRUCT_SIZE, default)?, - )) - } - } - - impl<'a, T> ::capnp::traits::SetPointerBuilder for Reader<'a, T> - where - T: for<'c> ::capnp::traits::Owned<'c>, - { - fn set_pointer_builder<'b>( - pointer: ::capnp::private::layout::PointerBuilder<'b>, - value: Reader<'a, T>, - canonicalize: bool, - ) -> ::capnp::Result<()> { - pointer.set_struct(&value.reader, canonicalize) - } - } - - impl<'a, T> Builder<'a, T> - where - T: for<'c> ::capnp::traits::Owned<'c>, - { - pub fn into_reader(self) -> Reader<'a, T> { - ::capnp::traits::FromStructReader::new(self.builder.into_reader()) - } - pub fn reborrow(&mut self) -> Builder<'_, T> { - Builder { ..*self } - } - pub fn reborrow_as_reader(&self) -> Reader<'_, T> { - ::capnp::traits::FromStructReader::new(self.builder.into_reader()) - } - - pub fn total_size(&self) -> ::capnp::Result<::capnp::MessageSize> { - self.builder.into_reader().total_size() - } - #[inline] - pub fn set_none(&mut self, _value: ()) { - self.builder.set_data_field::(0, 0); - } - #[inline] - pub fn initn_some(self, length: u32) -> >::Builder { - self.builder.set_data_field::(0, 1); - ::capnp::any_pointer::Builder::new(self.builder.get_pointer_field(0)).initn_as(length) - } - #[inline] - pub fn set_some( - &mut self, - value: >::Reader, - ) -> ::capnp::Result<()> { - self.builder.set_data_field::(0, 1); - ::capnp::traits::SetPointerBuilder::set_pointer_builder( - self.builder.get_pointer_field(0), - value, - false, - ) - } - #[inline] - pub fn init_some(self) -> >::Builder { - self.builder.set_data_field::(0, 1); - ::capnp::any_pointer::Builder::new(self.builder.get_pointer_field(0)).init_as() - } - pub fn has_some(&self) -> bool { - if self.builder.get_data_field::(0) != 1 { - return false; - } - !self.builder.get_pointer_field(0).is_null() - } - #[inline] - pub fn which(self) -> ::core::result::Result, ::capnp::NotInSchema> { - match self.builder.get_data_field::(0) { - 0 => ::core::result::Result::Ok(None(())), - 1 => ::core::result::Result::Ok(Some( - ::capnp::traits::FromPointerBuilder::get_from_pointer( - self.builder.get_pointer_field(0), - ::core::option::Option::None, - ), - )), - x => ::core::result::Result::Err(::capnp::NotInSchema(x)), - } - } - } - - pub struct Pipeline { - _typeless: ::capnp::any_pointer::Pipeline, - _phantom: ::core::marker::PhantomData, - } - impl ::capnp::capability::FromTypelessPipeline for Pipeline { - fn new(typeless: ::capnp::any_pointer::Pipeline) -> Pipeline { - Pipeline { - _typeless: typeless, - _phantom: ::core::marker::PhantomData, - } - } - } - impl Pipeline - where - T: ::capnp::traits::Pipelined, - ::Pipeline: ::capnp::capability::FromTypelessPipeline, - { - } - mod _private { - use capnp::private::layout; - pub const STRUCT_SIZE: layout::StructSize = layout::StructSize { - data: 1, - pointers: 1, - }; - pub const TYPE_ID: u64 = 0xd0d8_5bbb_e991_4dd9; - } - pub enum Which { - None(()), - Some(A0), - } - pub type WhichReader<'a, T> = Which<::capnp::Result<>::Reader>>; - pub type WhichBuilder<'a, T> = - Which<::capnp::Result<>::Builder>>; -} - pub mod err { /* T */ pub use self::Which::{Err, Ok}; @@ -1929,11 +1626,7 @@ pub mod signature { !self.reader.get_pointer_field(4).is_null() } #[inline] - pub fn get_rest( - self, - ) -> ::capnp::Result< - crate::plugin_capnp::option::Reader<'a, crate::plugin_capnp::argument::Owned>, - > { + pub fn get_rest(self) -> ::capnp::Result> { ::capnp::traits::FromPointerReader::get_from_pointer( &self.reader.get_pointer_field(5), ::core::option::Option::None, @@ -2153,11 +1846,7 @@ pub mod signature { !self.builder.get_pointer_field(4).is_null() } #[inline] - pub fn get_rest( - self, - ) -> ::capnp::Result< - crate::plugin_capnp::option::Builder<'a, crate::plugin_capnp::argument::Owned>, - > { + pub fn get_rest(self) -> ::capnp::Result> { ::capnp::traits::FromPointerBuilder::get_from_pointer( self.builder.get_pointer_field(5), ::core::option::Option::None, @@ -2166,15 +1855,16 @@ pub mod signature { #[inline] pub fn set_rest( &mut self, - value: crate::plugin_capnp::option::Reader<'_, crate::plugin_capnp::argument::Owned>, + value: crate::plugin_capnp::argument::Reader<'_>, ) -> ::capnp::Result<()> { - as ::capnp::traits::SetPointerBuilder>::set_pointer_builder(self.builder.get_pointer_field(5), value, false) + ::capnp::traits::SetPointerBuilder::set_pointer_builder( + self.builder.get_pointer_field(5), + value, + false, + ) } #[inline] - pub fn init_rest( - self, - ) -> crate::plugin_capnp::option::Builder<'a, crate::plugin_capnp::argument::Owned> - { + pub fn init_rest(self) -> crate::plugin_capnp::argument::Builder<'a> { ::capnp::traits::FromPointerBuilder::init_pointer(self.builder.get_pointer_field(5), 0) } pub fn has_rest(&self) -> bool { @@ -2235,9 +1925,7 @@ pub mod signature { } } impl Pipeline { - pub fn get_rest( - &self, - ) -> crate::plugin_capnp::option::Pipeline { + pub fn get_rest(&self) -> crate::plugin_capnp::argument::Pipeline { ::capnp::capability::FromTypelessPipeline::new(self._typeless.get_pointer_field(5)) } } @@ -2326,10 +2014,7 @@ pub mod flag { !self.reader.get_pointer_field(0).is_null() } #[inline] - pub fn get_short( - self, - ) -> ::capnp::Result> - { + pub fn get_short(self) -> ::capnp::Result<::capnp::text::Reader<'a>> { ::capnp::traits::FromPointerReader::get_from_pointer( &self.reader.get_pointer_field(1), ::core::option::Option::None, @@ -2448,25 +2133,19 @@ pub mod flag { !self.builder.get_pointer_field(0).is_null() } #[inline] - pub fn get_short( - self, - ) -> ::capnp::Result> - { + pub fn get_short(self) -> ::capnp::Result<::capnp::text::Builder<'a>> { ::capnp::traits::FromPointerBuilder::get_from_pointer( self.builder.get_pointer_field(1), ::core::option::Option::None, ) } #[inline] - pub fn set_short( - &mut self, - value: crate::plugin_capnp::option::Reader<'_, ::capnp::text::Owned>, - ) -> ::capnp::Result<()> { - as ::capnp::traits::SetPointerBuilder>::set_pointer_builder(self.builder.get_pointer_field(1), value, false) + pub fn set_short(&mut self, value: ::capnp::text::Reader<'_>) { + self.builder.get_pointer_field(1).set_text(value); } #[inline] - pub fn init_short(self) -> crate::plugin_capnp::option::Builder<'a, ::capnp::text::Owned> { - ::capnp::traits::FromPointerBuilder::init_pointer(self.builder.get_pointer_field(1), 0) + pub fn init_short(self, size: u32) -> ::capnp::text::Builder<'a> { + self.builder.get_pointer_field(1).init_text(size) } pub fn has_short(&self) -> bool { !self.builder.get_pointer_field(1).is_null() @@ -2519,11 +2198,7 @@ pub mod flag { } } } - impl Pipeline { - pub fn get_short(&self) -> crate::plugin_capnp::option::Pipeline<::capnp::text::Owned> { - ::capnp::capability::FromTypelessPipeline::new(self._typeless.get_pointer_field(1)) - } - } + impl Pipeline {} mod _private { use capnp::private::layout; pub const STRUCT_SIZE: layout::StructSize = layout::StructSize { @@ -3191,7 +2866,7 @@ pub mod call { crate::plugin_capnp::map::Reader< 'a, ::capnp::text::Owned, - crate::plugin_capnp::option::Owned, + crate::plugin_capnp::expression::Owned, >, > { ::capnp::traits::FromPointerReader::get_from_pointer( @@ -3340,7 +3015,7 @@ pub mod call { crate::plugin_capnp::map::Builder< 'a, ::capnp::text::Owned, - crate::plugin_capnp::option::Owned, + crate::plugin_capnp::expression::Owned, >, > { ::capnp::traits::FromPointerBuilder::get_from_pointer( @@ -3354,13 +3029,13 @@ pub mod call { value: crate::plugin_capnp::map::Reader< '_, ::capnp::text::Owned, - crate::plugin_capnp::option::Owned, + crate::plugin_capnp::expression::Owned, >, ) -> ::capnp::Result<()> { , + crate::plugin_capnp::expression::Owned, > as ::capnp::traits::SetPointerBuilder>::set_pointer_builder( self.builder.get_pointer_field(2), value, @@ -3373,7 +3048,7 @@ pub mod call { ) -> crate::plugin_capnp::map::Builder< 'a, ::capnp::text::Owned, - crate::plugin_capnp::option::Owned, + crate::plugin_capnp::expression::Owned, > { ::capnp::traits::FromPointerBuilder::init_pointer(self.builder.get_pointer_field(2), 0) } @@ -3400,7 +3075,7 @@ pub mod call { &self, ) -> crate::plugin_capnp::map::Pipeline< ::capnp::text::Owned, - crate::plugin_capnp::option::Owned, + crate::plugin_capnp::expression::Owned, > { ::capnp::capability::FromTypelessPipeline::new(self._typeless.get_pointer_field(2)) } diff --git a/crates/nu-plugin/src/serializers/call.rs b/crates/nu-plugin/src/serializers/call.rs index ab7492c4f..55512f194 100644 --- a/crates/nu-plugin/src/serializers/call.rs +++ b/crates/nu-plugin/src/serializers/call.rs @@ -1,5 +1,5 @@ use crate::plugin::PluginError; -use crate::plugin_capnp::{call, expression, option}; +use crate::plugin_capnp::{call, expression}; use nu_protocol::{ ast::{Call, Expr, Expression}, Span, Spanned, Type, @@ -40,13 +40,9 @@ fn serialize_named( .set_key(key.item.as_str()) .map_err(|e| PluginError::EncodingError(e.to_string()))?; - let mut value_builder = entry_builder.init_value(); - match expression { - None => value_builder.set_none(()), - Some(expr) => { - let expression_builder = value_builder.init_some(); - serialize_expression(expr, expression_builder); - } + if let Some(expr) = expression { + let value_builder = entry_builder.init_value(); + serialize_expression(expr, value_builder); } } @@ -129,22 +125,17 @@ fn deserialize_named(span: Span, reader: call::Reader) -> Result None, - Ok(option::Some(expression_reader)) => { - let expression_reader = - expression_reader.map_err(|e| PluginError::DecodingError(e.to_string()))?; + let expression = deserialize_expression(span, value_reader) + .map_err(|e| PluginError::DecodingError(e.to_string()))?; - let expression = deserialize_expression(span, expression_reader) - .map_err(|e| PluginError::DecodingError(e.to_string()))?; - - Some(expression) - } - Err(capnp::NotInSchema(_)) => None, + Some(expression) + } else { + None }; let key = Spanned { item, span }; @@ -194,7 +185,7 @@ fn deserialize_expression( #[cfg(test)] mod tests { - use capnp::serialize_packed; + use capnp::serialize; use core::panic; use super::*; @@ -209,13 +200,13 @@ mod tests { let builder = message.init_root::(); serialize_call(call, builder)?; - serialize_packed::write_message(writer, &message) + serialize::write_message(writer, &message) .map_err(|e| PluginError::EncodingError(e.to_string())) } fn read_buffer(reader: &mut impl std::io::BufRead) -> Result { let message_reader = - serialize_packed::read_message(reader, ::capnp::message::ReaderOptions::new()).unwrap(); + serialize::read_message(reader, ::capnp::message::ReaderOptions::new()).unwrap(); let reader = message_reader .get_root::() @@ -253,18 +244,27 @@ mod tests { 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, - }), - )], + 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, + }), + ), + ( + Spanned { + item: "flag".to_string(), + span: Span { start: 0, end: 10 }, + }, + None, + ), + ], }; let mut buffer: Vec = Vec::new(); diff --git a/crates/nu-plugin/src/serializers/signature.rs b/crates/nu-plugin/src/serializers/signature.rs index 7b98281a0..4aaf9ea36 100644 --- a/crates/nu-plugin/src/serializers/signature.rs +++ b/crates/nu-plugin/src/serializers/signature.rs @@ -1,5 +1,5 @@ use crate::plugin::PluginError; -use crate::plugin_capnp::{argument, flag, option, signature, Shape}; +use crate::plugin_capnp::{argument, flag, signature, Shape}; use nu_protocol::{Flag, PositionalArg, Signature, SyntaxShape}; pub(crate) fn serialize_signature(signature: &Signature, mut builder: signature::Builder) { @@ -29,13 +29,9 @@ pub(crate) fn serialize_signature(signature: &Signature, mut builder: signature: } // Serializing rest argument - let mut rest_argument = builder.reborrow().init_rest(); - match &signature.rest_positional { - None => rest_argument.set_none(()), - Some(arg) => { - let inner_builder = rest_argument.init_some(); - serialize_argument(arg, inner_builder) - } + let rest_argument = builder.reborrow().init_rest(); + if let Some(arg) = &signature.rest_positional { + serialize_argument(arg, rest_argument) } // Serializing the named arguments @@ -65,13 +61,9 @@ fn serialize_flag(arg: &Flag, mut builder: flag::Builder) { builder.set_required(arg.required); builder.set_desc(arg.desc.as_str()); - let mut short_builder = builder.reborrow().init_short(); - match arg.short { - None => short_builder.set_none(()), - Some(val) => { - let mut inner_builder = short_builder.reborrow().initn_some(1); - inner_builder.push_str(format!("{}", val).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 { @@ -119,17 +111,14 @@ pub(crate) fn deserialize_signature(reader: signature::Reader) -> Result, PluginError>>()?; // Deserializing rest arguments - let rest_option = reader - .get_rest() - .map_err(|e| PluginError::EncodingError(e.to_string()))?; + let rest_positional = if reader.has_rest() { + let argument_reader = reader + .get_rest() + .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| PluginError::EncodingError(e.to_string()))?; - Some(deserialize_argument(rest_reader)?) - } + Some(deserialize_argument(argument_reader)?) + } else { + None }; // Deserializing named arguments @@ -196,17 +185,14 @@ fn deserialize_flag(reader: flag::Reader) -> Result { let required = reader.get_required(); - let short = reader - .get_short() - .map_err(|e| PluginError::EncodingError(e.to_string()))?; + let short = if reader.has_short() { + let short_reader = reader + .get_short() + .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| PluginError::EncodingError(e.to_string()))?; - reader.chars().next() - } + short_reader.chars().next() + } else { + None }; let arg = reader @@ -235,7 +221,7 @@ fn deserialize_flag(reader: flag::Reader) -> Result { #[cfg(test)] mod tests { use super::*; - use capnp::serialize_packed; + use capnp::serialize; use nu_protocol::{Signature, SyntaxShape}; pub fn write_buffer( @@ -248,13 +234,13 @@ mod tests { serialize_signature(signature, builder); - serialize_packed::write_message(writer, &message) + serialize::write_message(writer, &message) .map_err(|e| PluginError::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(); + serialize::read_message(reader, ::capnp::message::ReaderOptions::new()).unwrap(); let reader = message_reader .get_root::() @@ -275,6 +261,7 @@ mod tests { "second named", Some('s'), ) + .switch("switch", "some switch", None) .rest("remaining", SyntaxShape::Int, "remaining"); let mut buffer: Vec = Vec::new(); diff --git a/crates/nu-plugin/src/serializers/value.rs b/crates/nu-plugin/src/serializers/value.rs index d5e1bef7d..f13a5417a 100644 --- a/crates/nu-plugin/src/serializers/value.rs +++ b/crates/nu-plugin/src/serializers/value.rs @@ -88,7 +88,7 @@ pub(crate) fn deserialize_value(reader: value::Reader) -> Result Result { let message_reader = - serialize_packed::read_message(reader, ::capnp::message::ReaderOptions::new()).unwrap(); + serialize::read_message(reader, ::capnp::message::ReaderOptions::new()).unwrap(); let reader = message_reader .get_root::()