nushell/crates/nu-plugin/src/protocol/mod.rs
Stefan Holderbach 62575c9a4f
Document and critically review ShellError variants - Ep. 3 (#8340)
Continuation of #8229 and #8326

# Description

The `ShellError` enum at the moment is kind of messy. 

Many variants are basic tuple structs where you always have to reference
the implementation with its macro invocation to know which field serves
which purpose.
Furthermore we have both variants that are kind of redundant or either
overly broad to be useful for the user to match on or overly specific
with few uses.

So I set out to start fixing the lacking documentation and naming to
make it feasible to critically review the individual usages and fix
those.
Furthermore we can decide to join or split up variants that don't seem
to be fit for purpose.

# Call to action

**Everyone:** Feel free to add review comments if you spot inconsistent
use of `ShellError` variants.

# User-Facing Changes

(None now, end goal more explicit and consistent error messages)

# Tests + Formatting

(No additional tests needed so far)

# Commits (so far)

- Remove `ShellError::FeatureNotEnabled`
- Name fields on `SE::ExternalNotSupported`
- Name field on `SE::InvalidProbability`
- Name fields on `SE::NushellFailed` variants
- Remove unused `SE::NushellFailedSpannedHelp`
- Name field on `SE::VariableNotFoundAtRuntime`
- Name fields on `SE::EnvVarNotFoundAtRuntime`
- Name fields on `SE::ModuleNotFoundAtRuntime`
- Remove usused `ModuleOrOverlayNotFoundAtRuntime`
- Name fields on `SE::OverlayNotFoundAtRuntime`
- Name field on `SE::NotFound`
2023-03-06 18:33:09 +01:00

109 lines
3.1 KiB
Rust

mod evaluated_call;
mod plugin_custom_value;
mod plugin_data;
pub use evaluated_call::EvaluatedCall;
use nu_protocol::{PluginSignature, ShellError, Span, Value};
pub use plugin_custom_value::PluginCustomValue;
pub use plugin_data::PluginData;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct CallInfo {
pub name: String,
pub call: EvaluatedCall,
pub input: CallInput,
}
#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub enum CallInput {
Value(Value),
Data(PluginData),
}
// Information sent to the plugin
#[derive(Serialize, Deserialize, Debug)]
pub enum PluginCall {
Signature,
CallInfo(CallInfo),
CollapseCustomValue(PluginData),
}
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
pub struct LabeledError {
pub label: String,
pub msg: String,
pub span: Option<Span>,
}
impl From<LabeledError> for ShellError {
fn from(error: LabeledError) -> Self {
match error.span {
Some(span) => {
ShellError::GenericError(error.label, error.msg, Some(span), None, Vec::new())
}
None => ShellError::GenericError(
error.label,
"".to_string(),
None,
Some(error.msg),
Vec::new(),
),
}
}
}
impl From<ShellError> for LabeledError {
fn from(error: ShellError) -> Self {
match error {
ShellError::GenericError(label, msg, span, _help, _related) => {
LabeledError { label, msg, span }
}
ShellError::CantConvert {
to_type: expected,
from_type: input,
span,
help: _help,
} => LabeledError {
label: format!("Can't convert to {expected}"),
msg: format!("can't convert {expected} to {input}"),
span: Some(span),
},
ShellError::DidYouMean(suggestion, span) => LabeledError {
label: "Name not found".into(),
msg: format!("did you mean '{suggestion}'"),
span: Some(span),
},
ShellError::PluginFailedToLoad(msg) => LabeledError {
label: "Plugin failed to load".into(),
msg,
span: None,
},
ShellError::PluginFailedToEncode(msg) => LabeledError {
label: "Plugin failed to encode".into(),
msg,
span: None,
},
ShellError::PluginFailedToDecode(msg) => LabeledError {
label: "Plugin failed to decode".into(),
msg,
span: None,
},
err => LabeledError {
label: "Error - Add to LabeledError From<ShellError>".into(),
msg: err.to_string(),
span: None,
},
}
}
}
// Information received from the plugin
#[derive(Serialize, Deserialize)]
pub enum PluginResponse {
Error(LabeledError),
Signature(Vec<PluginSignature>),
Value(Box<Value>),
PluginData(String, PluginData),
}