mirror of
https://github.com/nushell/nushell
synced 2024-11-14 00:47:09 +00: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:
parent
e36f69bf3c
commit
a42fd3611a
6 changed files with 51 additions and 32 deletions
|
@ -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)?
|
||||
|
|
|
@ -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| {
|
||||
|
|
|
@ -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.
|
||||
///
|
||||
|
|
Loading…
Reference in a new issue