mirror of
https://github.com/nushell/nushell
synced 2024-11-14 17:07:07 +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 {
|
let stdout_reader = match &mut child.stdout {
|
||||||
Some(out) => out,
|
Some(out) => out,
|
||||||
None => {
|
None => {
|
||||||
return Err(ShellError::PluginFailedToLoad(
|
return Err(ShellError::PluginFailedToLoad {
|
||||||
"Plugin missing stdout reader".into(),
|
msg: "Plugin missing stdout reader".into(),
|
||||||
))
|
})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
get_plugin_encoding(stdout_reader)?
|
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
|
let mut stdin_writer = child
|
||||||
.stdin
|
.stdin
|
||||||
.take()
|
.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
|
let mut stdout_reader = child
|
||||||
.stdout
|
.stdout
|
||||||
.take()
|
.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)?;
|
let encoding = get_plugin_encoding(&mut stdout_reader)?;
|
||||||
|
|
||||||
// Create message to plugin to indicate that signature is required and
|
// Create message to plugin to indicate that signature is required and
|
||||||
|
@ -177,14 +181,16 @@ pub fn get_signature(
|
||||||
let signatures = match response {
|
let signatures = match response {
|
||||||
PluginResponse::Signature(sign) => Ok(sign),
|
PluginResponse::Signature(sign) => Ok(sign),
|
||||||
PluginResponse::Error(err) => Err(err.into()),
|
PluginResponse::Error(err) => Err(err.into()),
|
||||||
_ => Err(ShellError::PluginFailedToLoad(
|
_ => Err(ShellError::PluginFailedToLoad {
|
||||||
"Plugin missing signature".into(),
|
msg: "Plugin missing signature".into(),
|
||||||
)),
|
}),
|
||||||
}?;
|
}?;
|
||||||
|
|
||||||
match child.wait() {
|
match child.wait() {
|
||||||
Ok(_) => Ok(signatures),
|
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> {
|
pub fn get_plugin_encoding(child_stdout: &mut ChildStdout) -> Result<EncodingType, ShellError> {
|
||||||
let mut length_buf = [0u8; 1];
|
let mut length_buf = [0u8; 1];
|
||||||
child_stdout.read_exact(&mut length_buf).map_err(|e| {
|
child_stdout
|
||||||
ShellError::PluginFailedToLoad(format!("unable to get encoding from plugin: {e}"))
|
.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];
|
let mut buf = vec![0u8; length_buf[0] as usize];
|
||||||
child_stdout.read_exact(&mut buf).map_err(|e| {
|
child_stdout
|
||||||
ShellError::PluginFailedToLoad(format!("unable to get encoding from plugin: {e}"))
|
.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(|| {
|
EncodingType::try_from_bytes(&buf).ok_or_else(|| {
|
||||||
let encoding_for_debug = String::from_utf8_lossy(&buf);
|
let encoding_for_debug = String::from_utf8_lossy(&buf);
|
||||||
ShellError::PluginFailedToLoad(format!(
|
ShellError::PluginFailedToLoad {
|
||||||
"get unsupported plugin encoding: {encoding_for_debug}"
|
msg: format!("get unsupported plugin encoding: {encoding_for_debug}"),
|
||||||
))
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -83,7 +83,7 @@ impl From<ShellError> for LabeledError {
|
||||||
msg: format!("did you mean '{suggestion}'?"),
|
msg: format!("did you mean '{suggestion}'?"),
|
||||||
span: Some(span),
|
span: Some(span),
|
||||||
},
|
},
|
||||||
ShellError::PluginFailedToLoad(msg) => LabeledError {
|
ShellError::PluginFailedToLoad { msg } => LabeledError {
|
||||||
label: "Plugin failed to load".into(),
|
label: "Plugin failed to load".into(),
|
||||||
msg,
|
msg,
|
||||||
span: None,
|
span: None,
|
||||||
|
|
|
@ -68,9 +68,9 @@ impl CustomValue for PluginCustomValue {
|
||||||
let stdout_reader = match &mut child.stdout {
|
let stdout_reader = match &mut child.stdout {
|
||||||
Some(out) => out,
|
Some(out) => out,
|
||||||
None => {
|
None => {
|
||||||
return Err(ShellError::PluginFailedToLoad(
|
return Err(ShellError::PluginFailedToLoad {
|
||||||
"Plugin missing stdout reader".into(),
|
msg: "Plugin missing stdout reader".into(),
|
||||||
))
|
})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
get_plugin_encoding(stdout_reader)?
|
get_plugin_encoding(stdout_reader)?
|
||||||
|
|
|
@ -454,11 +454,16 @@ impl EngineState {
|
||||||
// Updating the signatures plugin file with the added signatures
|
// Updating the signatures plugin file with the added signatures
|
||||||
self.plugin_signatures
|
self.plugin_signatures
|
||||||
.as_ref()
|
.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| {
|
.and_then(|plugin_path| {
|
||||||
// Always create the file, which will erase previous signatures
|
// Always create the file, which will erase previous signatures
|
||||||
std::fs::File::create(plugin_path.as_path())
|
std::fs::File::create(plugin_path.as_path()).map_err(|err| {
|
||||||
.map_err(|err| ShellError::PluginFailedToLoad(err.to_string()))
|
ShellError::PluginFailedToLoad {
|
||||||
|
msg: err.to_string(),
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
.and_then(|mut plugin_file| {
|
.and_then(|mut plugin_file| {
|
||||||
// Plugin definitions with parsed signature
|
// Plugin definitions with parsed signature
|
||||||
|
@ -510,11 +515,15 @@ impl EngineState {
|
||||||
// information when nushell starts
|
// information when nushell starts
|
||||||
format!("register {file_name} {shell_str} {signature}\n\n")
|
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| {
|
.and_then(|line| {
|
||||||
plugin_file
|
plugin_file.write_all(line.as_bytes()).map_err(|err| {
|
||||||
.write_all(line.as_bytes())
|
ShellError::PluginFailedToLoad {
|
||||||
.map_err(|err| ShellError::PluginFailedToLoad(err.to_string()))
|
msg: err.to_string(),
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
.and_then(|_| {
|
.and_then(|_| {
|
||||||
plugin_file.flush().map_err(|err| {
|
plugin_file.flush().map_err(|err| {
|
||||||
|
|
|
@ -736,9 +736,9 @@ pub enum ShellError {
|
||||||
/// ## Resolution
|
/// ## Resolution
|
||||||
///
|
///
|
||||||
/// This is a fairly generic error. Refer to the specific error message for further details.
|
/// 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))]
|
#[diagnostic(code(nu::shell::plugin_failed_to_load))]
|
||||||
PluginFailedToLoad(String),
|
PluginFailedToLoad { msg: String },
|
||||||
|
|
||||||
/// A message from a plugin failed to encode.
|
/// A message from a plugin failed to encode.
|
||||||
///
|
///
|
||||||
|
|
Loading…
Reference in a new issue