From a42fd3611a5fcaf2bbbe5c0dd3fabeb7fde501e6 Mon Sep 17 00:00:00 2001 From: Eric Hodel Date: Tue, 21 Nov 2023 15:30:52 -0800 Subject: [PATCH] Convert PluginFailedToLoad to named fields (#11124) # 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/declaration.rs | 6 +-- crates/nu-plugin/src/plugin/mod.rs | 42 ++++++++++++------- crates/nu-plugin/src/protocol/mod.rs | 2 +- .../src/protocol/plugin_custom_value.rs | 6 +-- crates/nu-protocol/src/engine/engine_state.rs | 23 ++++++---- crates/nu-protocol/src/shell_error.rs | 4 +- 6 files changed, 51 insertions(+), 32 deletions(-) diff --git a/crates/nu-plugin/src/plugin/declaration.rs b/crates/nu-plugin/src/plugin/declaration.rs index 84f3a49acb..365228a41d 100644 --- a/crates/nu-plugin/src/plugin/declaration.rs +++ b/crates/nu-plugin/src/plugin/declaration.rs @@ -136,9 +136,9 @@ impl Command for PluginDeclaration { let stdout_reader = match &mut child.stdout { Some(out) => out, None => { - return Err(ShellError::PluginFailedToLoad( - "Plugin missing stdout reader".into(), - )) + return Err(ShellError::PluginFailedToLoad { + msg: "Plugin missing stdout reader".into(), + }) } }; get_plugin_encoding(stdout_reader)? diff --git a/crates/nu-plugin/src/plugin/mod.rs b/crates/nu-plugin/src/plugin/mod.rs index a68bff60c3..7ecaa0effd 100644 --- a/crates/nu-plugin/src/plugin/mod.rs +++ b/crates/nu-plugin/src/plugin/mod.rs @@ -146,17 +146,21 @@ pub fn get_signature( } }; - ShellError::PluginFailedToLoad(error_msg) + ShellError::PluginFailedToLoad { msg: error_msg } })?; let mut stdin_writer = child .stdin .take() - .ok_or_else(|| ShellError::PluginFailedToLoad("plugin missing stdin writer".into()))?; + .ok_or_else(|| ShellError::PluginFailedToLoad { + msg: "plugin missing stdin writer".into(), + })?; let mut stdout_reader = child .stdout .take() - .ok_or_else(|| ShellError::PluginFailedToLoad("Plugin missing stdout reader".into()))?; + .ok_or_else(|| ShellError::PluginFailedToLoad { + msg: "Plugin missing stdout reader".into(), + })?; let encoding = get_plugin_encoding(&mut stdout_reader)?; // Create message to plugin to indicate that signature is required and @@ -177,14 +181,16 @@ pub fn get_signature( let signatures = match response { PluginResponse::Signature(sign) => Ok(sign), PluginResponse::Error(err) => Err(err.into()), - _ => Err(ShellError::PluginFailedToLoad( - "Plugin missing signature".into(), - )), + _ => Err(ShellError::PluginFailedToLoad { + msg: "Plugin missing signature".into(), + }), }?; match child.wait() { Ok(_) => Ok(signatures), - Err(err) => Err(ShellError::PluginFailedToLoad(format!("{err}"))), + Err(err) => Err(ShellError::PluginFailedToLoad { + msg: format!("{err}"), + }), } } @@ -438,19 +444,23 @@ fn print_help(plugin: &mut impl Plugin, encoder: impl PluginEncoder) { pub fn get_plugin_encoding(child_stdout: &mut ChildStdout) -> Result { let mut length_buf = [0u8; 1]; - child_stdout.read_exact(&mut length_buf).map_err(|e| { - ShellError::PluginFailedToLoad(format!("unable to get encoding from plugin: {e}")) - })?; + child_stdout + .read_exact(&mut length_buf) + .map_err(|e| ShellError::PluginFailedToLoad { + msg: format!("unable to get encoding from plugin: {e}"), + })?; let mut buf = vec![0u8; length_buf[0] as usize]; - child_stdout.read_exact(&mut buf).map_err(|e| { - ShellError::PluginFailedToLoad(format!("unable to get encoding from plugin: {e}")) - })?; + child_stdout + .read_exact(&mut buf) + .map_err(|e| ShellError::PluginFailedToLoad { + msg: format!("unable to get encoding from plugin: {e}"), + })?; EncodingType::try_from_bytes(&buf).ok_or_else(|| { let encoding_for_debug = String::from_utf8_lossy(&buf); - ShellError::PluginFailedToLoad(format!( - "get unsupported plugin encoding: {encoding_for_debug}" - )) + ShellError::PluginFailedToLoad { + msg: format!("get unsupported plugin encoding: {encoding_for_debug}"), + } }) } diff --git a/crates/nu-plugin/src/protocol/mod.rs b/crates/nu-plugin/src/protocol/mod.rs index 57a0c1bb35..e74b350add 100644 --- a/crates/nu-plugin/src/protocol/mod.rs +++ b/crates/nu-plugin/src/protocol/mod.rs @@ -83,7 +83,7 @@ impl From for LabeledError { msg: format!("did you mean '{suggestion}'?"), span: Some(span), }, - ShellError::PluginFailedToLoad(msg) => LabeledError { + ShellError::PluginFailedToLoad { msg } => LabeledError { label: "Plugin failed to load".into(), msg, span: None, diff --git a/crates/nu-plugin/src/protocol/plugin_custom_value.rs b/crates/nu-plugin/src/protocol/plugin_custom_value.rs index 6c2410b3fe..c4dd341a00 100644 --- a/crates/nu-plugin/src/protocol/plugin_custom_value.rs +++ b/crates/nu-plugin/src/protocol/plugin_custom_value.rs @@ -68,9 +68,9 @@ impl CustomValue for PluginCustomValue { let stdout_reader = match &mut child.stdout { Some(out) => out, None => { - return Err(ShellError::PluginFailedToLoad( - "Plugin missing stdout reader".into(), - )) + return Err(ShellError::PluginFailedToLoad { + msg: "Plugin missing stdout reader".into(), + }) } }; get_plugin_encoding(stdout_reader)? diff --git a/crates/nu-protocol/src/engine/engine_state.rs b/crates/nu-protocol/src/engine/engine_state.rs index d7b29f2d4a..cffc3b6983 100644 --- a/crates/nu-protocol/src/engine/engine_state.rs +++ b/crates/nu-protocol/src/engine/engine_state.rs @@ -454,11 +454,16 @@ impl EngineState { // Updating the signatures plugin file with the added signatures self.plugin_signatures .as_ref() - .ok_or_else(|| ShellError::PluginFailedToLoad("Plugin file not found".into())) + .ok_or_else(|| ShellError::PluginFailedToLoad { + msg: "Plugin file not found".into(), + }) .and_then(|plugin_path| { // Always create the file, which will erase previous signatures - std::fs::File::create(plugin_path.as_path()) - .map_err(|err| ShellError::PluginFailedToLoad(err.to_string())) + std::fs::File::create(plugin_path.as_path()).map_err(|err| { + ShellError::PluginFailedToLoad { + msg: err.to_string(), + } + }) }) .and_then(|mut plugin_file| { // Plugin definitions with parsed signature @@ -510,11 +515,15 @@ impl EngineState { // information when nushell starts format!("register {file_name} {shell_str} {signature}\n\n") }) - .map_err(|err| ShellError::PluginFailedToLoad(err.to_string())) + .map_err(|err| ShellError::PluginFailedToLoad { + msg: err.to_string(), + }) .and_then(|line| { - plugin_file - .write_all(line.as_bytes()) - .map_err(|err| ShellError::PluginFailedToLoad(err.to_string())) + plugin_file.write_all(line.as_bytes()).map_err(|err| { + ShellError::PluginFailedToLoad { + msg: err.to_string(), + } + }) }) .and_then(|_| { plugin_file.flush().map_err(|err| { diff --git a/crates/nu-protocol/src/shell_error.rs b/crates/nu-protocol/src/shell_error.rs index 9832ae64a7..6ff218411d 100644 --- a/crates/nu-protocol/src/shell_error.rs +++ b/crates/nu-protocol/src/shell_error.rs @@ -736,9 +736,9 @@ pub enum ShellError { /// ## Resolution /// /// This is a fairly generic error. Refer to the specific error message for further details. - #[error("Plugin failed to load: {0}")] + #[error("Plugin failed to load: {msg}")] #[diagnostic(code(nu::shell::plugin_failed_to_load))] - PluginFailedToLoad(String), + PluginFailedToLoad { msg: String }, /// A message from a plugin failed to encode. ///