forked from extern/nushell
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`
79 lines
2.8 KiB
Rust
79 lines
2.8 KiB
Rust
mod cool_custom_value;
|
|
mod second_custom_value;
|
|
|
|
use cool_custom_value::CoolCustomValue;
|
|
use nu_plugin::{serve_plugin, MsgPackSerializer, Plugin};
|
|
use nu_plugin::{EvaluatedCall, LabeledError};
|
|
use nu_protocol::{Category, PluginSignature, ShellError, Value};
|
|
use second_custom_value::SecondCustomValue;
|
|
|
|
struct CustomValuePlugin;
|
|
|
|
impl Plugin for CustomValuePlugin {
|
|
fn signature(&self) -> Vec<nu_protocol::PluginSignature> {
|
|
vec![
|
|
PluginSignature::build("custom-value generate")
|
|
.usage("PluginSignature for a plugin that generates a custom value")
|
|
.category(Category::Experimental),
|
|
PluginSignature::build("custom-value generate2")
|
|
.usage("PluginSignature for a plugin that generates a different custom value")
|
|
.category(Category::Experimental),
|
|
PluginSignature::build("custom-value update")
|
|
.usage("PluginSignature for a plugin that updates a custom value")
|
|
.category(Category::Experimental),
|
|
]
|
|
}
|
|
|
|
fn run(
|
|
&mut self,
|
|
name: &str,
|
|
call: &EvaluatedCall,
|
|
input: &Value,
|
|
) -> Result<Value, LabeledError> {
|
|
match name {
|
|
"custom-value generate" => self.generate(call, input),
|
|
"custom-value generate2" => self.generate2(call, input),
|
|
"custom-value update" => self.update(call, input),
|
|
_ => Err(LabeledError {
|
|
label: "Plugin call with wrong name signature".into(),
|
|
msg: "the signature used to call the plugin does not match any name in the plugin signature vector".into(),
|
|
span: Some(call.head),
|
|
}),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl CustomValuePlugin {
|
|
fn generate(&mut self, call: &EvaluatedCall, _input: &Value) -> Result<Value, LabeledError> {
|
|
Ok(CoolCustomValue::new("abc").into_value(call.head))
|
|
}
|
|
|
|
fn generate2(&mut self, call: &EvaluatedCall, _input: &Value) -> Result<Value, LabeledError> {
|
|
Ok(SecondCustomValue::new("xyz").into_value(call.head))
|
|
}
|
|
|
|
fn update(&mut self, call: &EvaluatedCall, input: &Value) -> Result<Value, LabeledError> {
|
|
if let Ok(mut value) = CoolCustomValue::try_from_value(input) {
|
|
value.cool += "xyz";
|
|
return Ok(value.into_value(call.head));
|
|
}
|
|
|
|
if let Ok(mut value) = SecondCustomValue::try_from_value(input) {
|
|
value.something += "abc";
|
|
return Ok(value.into_value(call.head));
|
|
}
|
|
|
|
Err(ShellError::CantConvert {
|
|
to_type: "cool or second".into(),
|
|
from_type: "non-cool and non-second".into(),
|
|
span: call.head,
|
|
help: None,
|
|
}
|
|
.into())
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
serve_plugin(&mut CustomValuePlugin, MsgPackSerializer {})
|
|
}
|