2021-12-12 12:50:35 +01:00
|
|
|
mod evaluated_call;
|
2022-07-25 18:32:56 +02:00
|
|
|
mod plugin_custom_value;
|
|
|
|
mod plugin_data;
|
2021-12-12 12:50:35 +01:00
|
|
|
|
|
|
|
pub use evaluated_call::EvaluatedCall;
|
|
|
|
use nu_protocol::{ShellError, Signature, Span, Value};
|
2022-07-25 18:32:56 +02:00
|
|
|
pub use plugin_custom_value::PluginCustomValue;
|
|
|
|
pub use plugin_data::PluginData;
|
2021-12-12 12:50:35 +01:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
pub struct CallInfo {
|
|
|
|
pub name: String,
|
|
|
|
pub call: EvaluatedCall,
|
2022-07-25 18:32:56 +02:00
|
|
|
pub input: CallInput,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
|
|
|
pub enum CallInput {
|
|
|
|
Value(Value),
|
|
|
|
Data(PluginData),
|
2021-12-12 12:50:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Information sent to the plugin
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
pub enum PluginCall {
|
|
|
|
Signature,
|
2022-07-25 18:32:56 +02:00
|
|
|
CallInfo(CallInfo),
|
|
|
|
CollapseCustomValue(PluginData),
|
2021-12-12 12:50:35 +01:00
|
|
|
}
|
|
|
|
|
2022-06-04 08:47:36 +02:00
|
|
|
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
|
2021-12-12 12:50:35 +01:00
|
|
|
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 {
|
2022-04-18 14:34:10 +02:00
|
|
|
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(),
|
|
|
|
),
|
2021-12-12 12:50:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ShellError> for LabeledError {
|
|
|
|
fn from(error: ShellError) -> Self {
|
|
|
|
match error {
|
2022-04-18 14:34:10 +02:00
|
|
|
ShellError::GenericError(label, msg, span, _help, _related) => {
|
|
|
|
LabeledError { label, msg, span }
|
|
|
|
}
|
|
|
|
ShellError::CantConvert(expected, input, span, _help) => LabeledError {
|
2023-01-30 02:37:54 +01:00
|
|
|
label: format!("Can't convert to {expected}"),
|
|
|
|
msg: format!("can't convert {expected} to {input}"),
|
2021-12-12 12:50:35 +01:00
|
|
|
span: Some(span),
|
|
|
|
},
|
|
|
|
ShellError::DidYouMean(suggestion, span) => LabeledError {
|
|
|
|
label: "Name not found".into(),
|
2023-01-30 02:37:54 +01:00
|
|
|
msg: format!("did you mean '{suggestion}'"),
|
2021-12-12 12:50:35 +01:00
|
|
|
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<Signature>),
|
|
|
|
Value(Box<Value>),
|
2022-07-25 18:32:56 +02:00
|
|
|
PluginData(String, PluginData),
|
2021-12-12 12:50:35 +01:00
|
|
|
}
|