From 776df7cd931c27849a193ed61b1cd68a7a1d9f72 Mon Sep 17 00:00:00 2001 From: Eric Hodel Date: Wed, 22 Nov 2023 03:56:04 -0800 Subject: [PATCH] Convert PluginFailedToDecode to named fields (#11126) # Description Part of #10700 # User-Facing Changes None # Tests + Formatting - :green_circle: `toolkit fmt` - :green_circle: `toolkit clippy` - :green_circle: `toolkit test` - :green_circle: `toolkit test stdlib` # After Submitting N/A --- crates/nu-plugin/src/plugin/mod.rs | 8 ++++++-- crates/nu-plugin/src/protocol/mod.rs | 2 +- crates/nu-plugin/src/serializers/msgpack.rs | 10 ++++++---- crates/nu-protocol/src/shell_error.rs | 4 ++-- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/crates/nu-plugin/src/plugin/mod.rs b/crates/nu-plugin/src/plugin/mod.rs index 1fb650309d..e1349b7529 100644 --- a/crates/nu-plugin/src/plugin/mod.rs +++ b/crates/nu-plugin/src/plugin/mod.rs @@ -326,7 +326,9 @@ pub fn serve_plugin(plugin: &mut impl Plugin, encoder: impl PluginEncoder) { .map(|custom_value| { Value::custom_value(custom_value, plugin_data.span) }) - .map_err(|err| ShellError::PluginFailedToDecode(err.to_string())) + .map_err(|err| ShellError::PluginFailedToDecode { + msg: err.to_string(), + }) } }; @@ -362,7 +364,9 @@ pub fn serve_plugin(plugin: &mut impl Plugin, encoder: impl PluginEncoder) { } PluginCall::CollapseCustomValue(plugin_data) => { let response = bincode::deserialize::>(&plugin_data.data) - .map_err(|err| ShellError::PluginFailedToDecode(err.to_string())) + .map_err(|err| ShellError::PluginFailedToDecode { + msg: err.to_string(), + }) .and_then(|val| val.to_base_value(plugin_data.span)) .map(Box::new) .map_err(LabeledError::from) diff --git a/crates/nu-plugin/src/protocol/mod.rs b/crates/nu-plugin/src/protocol/mod.rs index 6a03604d04..f37c6c2d0e 100644 --- a/crates/nu-plugin/src/protocol/mod.rs +++ b/crates/nu-plugin/src/protocol/mod.rs @@ -93,7 +93,7 @@ impl From for LabeledError { msg, span: None, }, - ShellError::PluginFailedToDecode(msg) => LabeledError { + ShellError::PluginFailedToDecode { msg } => LabeledError { label: "Plugin failed to decode".into(), msg, span: None, diff --git a/crates/nu-plugin/src/serializers/msgpack.rs b/crates/nu-plugin/src/serializers/msgpack.rs index b6b5776fd4..e88be0b209 100644 --- a/crates/nu-plugin/src/serializers/msgpack.rs +++ b/crates/nu-plugin/src/serializers/msgpack.rs @@ -27,8 +27,9 @@ impl PluginEncoder for MsgPackSerializer { &self, reader: &mut impl std::io::BufRead, ) -> Result { - rmp_serde::from_read(reader) - .map_err(|err| ShellError::PluginFailedToDecode(err.to_string())) + rmp_serde::from_read(reader).map_err(|err| ShellError::PluginFailedToDecode { + msg: err.to_string(), + }) } fn encode_response( @@ -47,8 +48,9 @@ impl PluginEncoder for MsgPackSerializer { &self, reader: &mut impl std::io::BufRead, ) -> Result { - rmp_serde::from_read(reader) - .map_err(|err| ShellError::PluginFailedToDecode(err.to_string())) + rmp_serde::from_read(reader).map_err(|err| ShellError::PluginFailedToDecode { + msg: err.to_string(), + }) } } diff --git a/crates/nu-protocol/src/shell_error.rs b/crates/nu-protocol/src/shell_error.rs index 295e3ec931..7712e0a0e4 100644 --- a/crates/nu-protocol/src/shell_error.rs +++ b/crates/nu-protocol/src/shell_error.rs @@ -754,9 +754,9 @@ pub enum ShellError { /// ## Resolution /// /// This is either an issue with the inputs to a plugin (bad JSON?) or a bug in the plugin itself. Fix or report as appropriate. - #[error("Plugin failed to decode: {0}")] + #[error("Plugin failed to decode: {msg}")] #[diagnostic(code(nu::shell::plugin_failed_to_decode))] - PluginFailedToDecode(String), + PluginFailedToDecode { msg: String }, /// I/O operation interrupted. ///