mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 03:54:58 +02:00
Convert PluginFailedToLoad to named fields (#11124)
# 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:
@ -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)?
|
||||
|
@ -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<EncodingType, ShellError> {
|
||||
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}"),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -83,7 +83,7 @@ impl From<ShellError> 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,
|
||||
|
@ -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)?
|
||||
|
Reference in New Issue
Block a user