Convert PluginFailedToEncode to named fields (#11125)

# Description

Part of #10700

# User-Facing Changes

None

# Tests + Formatting
- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`

# After Submitting

N/A
This commit is contained in:
Eric Hodel 2023-11-21 15:38:58 -08:00 committed by GitHub
parent a42fd3611a
commit 64288b4350
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 16 deletions

View File

@ -345,7 +345,10 @@ pub fn serve_plugin(plugin: &mut impl Plugin, encoder: impl PluginEncoder) {
PluginResponse::PluginData(name, PluginData { data, span }) PluginResponse::PluginData(name, PluginData { data, span })
} }
Err(err) => PluginResponse::Error( Err(err) => PluginResponse::Error(
ShellError::PluginFailedToEncode(err.to_string()).into(), ShellError::PluginFailedToEncode {
msg: err.to_string(),
}
.into(),
), ),
}, },
value => PluginResponse::Value(Box::new(value)), value => PluginResponse::Value(Box::new(value)),

View File

@ -88,7 +88,7 @@ impl From<ShellError> for LabeledError {
msg, msg,
span: None, span: None,
}, },
ShellError::PluginFailedToEncode(msg) => LabeledError { ShellError::PluginFailedToEncode { msg } => LabeledError {
label: "Plugin failed to encode".into(), label: "Plugin failed to encode".into(),
msg, msg,
span: None, span: None,

View File

@ -17,16 +17,18 @@ impl PluginEncoder for JsonSerializer {
plugin_call: &crate::protocol::PluginCall, plugin_call: &crate::protocol::PluginCall,
writer: &mut impl std::io::Write, writer: &mut impl std::io::Write,
) -> Result<(), nu_protocol::ShellError> { ) -> Result<(), nu_protocol::ShellError> {
serde_json::to_writer(writer, plugin_call) serde_json::to_writer(writer, plugin_call).map_err(|err| ShellError::PluginFailedToEncode {
.map_err(|err| ShellError::PluginFailedToEncode(err.to_string())) msg: err.to_string(),
})
} }
fn decode_call( fn decode_call(
&self, &self,
reader: &mut impl std::io::BufRead, reader: &mut impl std::io::BufRead,
) -> Result<crate::protocol::PluginCall, nu_protocol::ShellError> { ) -> Result<crate::protocol::PluginCall, nu_protocol::ShellError> {
serde_json::from_reader(reader) serde_json::from_reader(reader).map_err(|err| ShellError::PluginFailedToEncode {
.map_err(|err| ShellError::PluginFailedToEncode(err.to_string())) msg: err.to_string(),
})
} }
fn encode_response( fn encode_response(
@ -34,16 +36,20 @@ impl PluginEncoder for JsonSerializer {
plugin_response: &PluginResponse, plugin_response: &PluginResponse,
writer: &mut impl std::io::Write, writer: &mut impl std::io::Write,
) -> Result<(), ShellError> { ) -> Result<(), ShellError> {
serde_json::to_writer(writer, plugin_response) serde_json::to_writer(writer, plugin_response).map_err(|err| {
.map_err(|err| ShellError::PluginFailedToEncode(err.to_string())) ShellError::PluginFailedToEncode {
msg: err.to_string(),
}
})
} }
fn decode_response( fn decode_response(
&self, &self,
reader: &mut impl std::io::BufRead, reader: &mut impl std::io::BufRead,
) -> Result<PluginResponse, ShellError> { ) -> Result<PluginResponse, ShellError> {
serde_json::from_reader(reader) serde_json::from_reader(reader).map_err(|err| ShellError::PluginFailedToEncode {
.map_err(|err| ShellError::PluginFailedToEncode(err.to_string())) msg: err.to_string(),
})
} }
} }

View File

@ -16,8 +16,11 @@ impl PluginEncoder for MsgPackSerializer {
plugin_call: &crate::protocol::PluginCall, plugin_call: &crate::protocol::PluginCall,
writer: &mut impl std::io::Write, writer: &mut impl std::io::Write,
) -> Result<(), nu_protocol::ShellError> { ) -> Result<(), nu_protocol::ShellError> {
rmp_serde::encode::write(writer, plugin_call) rmp_serde::encode::write(writer, plugin_call).map_err(|err| {
.map_err(|err| ShellError::PluginFailedToEncode(err.to_string())) ShellError::PluginFailedToEncode {
msg: err.to_string(),
}
})
} }
fn decode_call( fn decode_call(
@ -33,8 +36,11 @@ impl PluginEncoder for MsgPackSerializer {
plugin_response: &PluginResponse, plugin_response: &PluginResponse,
writer: &mut impl std::io::Write, writer: &mut impl std::io::Write,
) -> Result<(), ShellError> { ) -> Result<(), ShellError> {
rmp_serde::encode::write(writer, plugin_response) rmp_serde::encode::write(writer, plugin_response).map_err(|err| {
.map_err(|err| ShellError::PluginFailedToEncode(err.to_string())) ShellError::PluginFailedToEncode {
msg: err.to_string(),
}
})
} }
fn decode_response( fn decode_response(

View File

@ -745,9 +745,9 @@ pub enum ShellError {
/// ## Resolution /// ## Resolution
/// ///
/// This is likely a bug with the plugin itself. /// This is likely a bug with the plugin itself.
#[error("Plugin failed to encode: {0}")] #[error("Plugin failed to encode: {msg}")]
#[diagnostic(code(nu::shell::plugin_failed_to_encode))] #[diagnostic(code(nu::shell::plugin_failed_to_encode))]
PluginFailedToEncode(String), PluginFailedToEncode { msg: String },
/// A message to a plugin failed to decode. /// A message to a plugin failed to decode.
/// ///