mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 13:36:07 +02:00
First step (#411)
This commit is contained in:
@ -37,7 +37,7 @@ pub fn get_signature(path: &Path) -> Result<Vec<Signature>, ShellError> {
|
||||
let mut plugin_cmd = create_command(path);
|
||||
|
||||
let mut child = plugin_cmd.spawn().map_err(|err| {
|
||||
ShellError::InternalError(format!("Error spawning child process: {}", err))
|
||||
ShellError::PluginFailedToLoad(format!("Error spawning child process: {}", err))
|
||||
})?;
|
||||
|
||||
// Create message to plugin to indicate that signature is required and
|
||||
@ -55,14 +55,16 @@ pub fn get_signature(path: &Path) -> Result<Vec<Signature>, ShellError> {
|
||||
|
||||
match response {
|
||||
PluginResponse::Signature(sign) => Ok(sign),
|
||||
PluginResponse::Error(msg) => Err(ShellError::InternalError(format!(
|
||||
PluginResponse::Error(msg) => Err(ShellError::PluginFailedToLoad(format!(
|
||||
"Plugin response error {}",
|
||||
msg,
|
||||
))),
|
||||
_ => Err(ShellError::InternalError("Plugin missing signature".into())),
|
||||
_ => Err(ShellError::PluginFailedToLoad(
|
||||
"Plugin missing signature".into(),
|
||||
)),
|
||||
}
|
||||
} else {
|
||||
Err(ShellError::InternalError(
|
||||
Err(ShellError::PluginFailedToLoad(
|
||||
"Plugin missing stdout reader".into(),
|
||||
))
|
||||
}?;
|
||||
@ -285,7 +287,7 @@ pub fn eval_plugin_signatures(working_set: &mut StateWorkingSet) -> Result<(), S
|
||||
plugin_decl
|
||||
})
|
||||
.collect::<Vec<Box<dyn Command>>>()),
|
||||
Err(err) => Err(ShellError::InternalError(format!("{}", err))),
|
||||
Err(err) => Err(ShellError::PluginFailedToLoad(format!("{}", err))),
|
||||
},
|
||||
})
|
||||
// Need to collect the vector in order to check the error from getting the signature
|
||||
|
@ -25,55 +25,56 @@ pub fn encode_call(
|
||||
let call_builder = call_info_builder
|
||||
.reborrow()
|
||||
.get_call()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
call::serialize_call(&call_info.call, call_builder)
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
// Serializing the input value from the call info
|
||||
let value_builder = call_info_builder
|
||||
.reborrow()
|
||||
.get_input()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
value::serialize_value(&call_info.input, value_builder);
|
||||
}
|
||||
};
|
||||
|
||||
serialize::write_message(writer, &message).map_err(|e| ShellError::InternalError(e.to_string()))
|
||||
serialize::write_message(writer, &message)
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))
|
||||
}
|
||||
|
||||
pub fn decode_call(reader: &mut impl std::io::BufRead) -> Result<PluginCall, ShellError> {
|
||||
let message_reader = serialize::read_message(reader, ::capnp::message::ReaderOptions::new())
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
let reader = message_reader
|
||||
.get_root::<plugin_call::Reader>()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
match reader.which() {
|
||||
Err(capnp::NotInSchema(_)) => Err(ShellError::InternalError("value not in schema".into())),
|
||||
Err(capnp::NotInSchema(_)) => {
|
||||
Err(ShellError::PluginFailedToLoad("value not in schema".into()))
|
||||
}
|
||||
Ok(plugin_call::Signature(())) => Ok(PluginCall::Signature),
|
||||
Ok(plugin_call::CallInfo(reader)) => {
|
||||
let reader = reader.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
let reader = reader.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
let name = reader
|
||||
.get_name()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
let call_reader = reader
|
||||
.get_call()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
let call = call::deserialize_call(call_reader)
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
let call = call::deserialize_call(call_reader)?;
|
||||
|
||||
let input_reader = reader
|
||||
.get_input()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
let input = value::deserialize_value(input_reader)
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
let input = value::deserialize_value(input_reader)?;
|
||||
|
||||
Ok(PluginCall::CallInfo(Box::new(CallInfo {
|
||||
name: name.to_string(),
|
||||
@ -109,26 +110,29 @@ pub fn encode_response(
|
||||
}
|
||||
};
|
||||
|
||||
serialize::write_message(writer, &message).map_err(|e| ShellError::InternalError(e.to_string()))
|
||||
serialize::write_message(writer, &message)
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))
|
||||
}
|
||||
|
||||
pub fn decode_response(reader: &mut impl std::io::BufRead) -> Result<PluginResponse, ShellError> {
|
||||
let message_reader = serialize::read_message(reader, ::capnp::message::ReaderOptions::new())
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
let reader = message_reader
|
||||
.get_root::<plugin_response::Reader>()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
match reader.which() {
|
||||
Err(capnp::NotInSchema(_)) => Err(ShellError::InternalError("value not in schema".into())),
|
||||
Err(capnp::NotInSchema(_)) => {
|
||||
Err(ShellError::PluginFailedToLoad("value not in schema".into()))
|
||||
}
|
||||
Ok(plugin_response::Error(reader)) => {
|
||||
let msg = reader.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
let msg = reader.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
Ok(PluginResponse::Error(msg.to_string()))
|
||||
}
|
||||
Ok(plugin_response::Signature(reader)) => {
|
||||
let reader = reader.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
let reader = reader.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
let signatures = reader
|
||||
.iter()
|
||||
@ -138,9 +142,9 @@ pub fn decode_response(reader: &mut impl std::io::BufRead) -> Result<PluginRespo
|
||||
Ok(PluginResponse::Signature(signatures))
|
||||
}
|
||||
Ok(plugin_response::Value(reader)) => {
|
||||
let reader = reader.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
let reader = reader.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
let val = value::deserialize_value(reader)
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
Ok(PluginResponse::Value(Box::new(val)))
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ fn serialize_named(
|
||||
entry_builder
|
||||
.reborrow()
|
||||
.set_key(key.item.as_str())
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
if let Some(value) = expression {
|
||||
let value_builder = entry_builder.init_value();
|
||||
@ -54,7 +54,7 @@ pub(crate) fn deserialize_call(
|
||||
) -> Result<EvaluatedCall, ShellError> {
|
||||
let head_reader = reader
|
||||
.get_head()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
let head = Span {
|
||||
start: head_reader.get_start() as usize,
|
||||
@ -77,7 +77,7 @@ fn deserialize_positionals(
|
||||
) -> Result<Vec<Value>, ShellError> {
|
||||
let positional_reader = reader
|
||||
.get_positional()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
positional_reader
|
||||
.iter()
|
||||
@ -90,11 +90,11 @@ type NamedList = Vec<(Spanned<String>, Option<Value>)>;
|
||||
fn deserialize_named(span: Span, reader: evaluated_call::Reader) -> Result<NamedList, ShellError> {
|
||||
let named_reader = reader
|
||||
.get_named()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
let entries_list = named_reader
|
||||
.get_entries()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
let mut entries: Vec<(Spanned<String>, Option<Value>)> =
|
||||
Vec::with_capacity(entries_list.len() as usize);
|
||||
@ -102,16 +102,16 @@ fn deserialize_named(span: Span, reader: evaluated_call::Reader) -> Result<Named
|
||||
for entry_reader in entries_list {
|
||||
let item = entry_reader
|
||||
.get_key()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?
|
||||
.to_string();
|
||||
|
||||
let value = if entry_reader.has_value() {
|
||||
let value_reader = entry_reader
|
||||
.get_value()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
let value = value::deserialize_value(value_reader)
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
Some(value)
|
||||
} else {
|
||||
@ -144,7 +144,7 @@ mod tests {
|
||||
serialize_call(call, builder)?;
|
||||
|
||||
serialize::write_message(writer, &message)
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))
|
||||
}
|
||||
|
||||
fn read_buffer(reader: &mut impl std::io::BufRead) -> Result<EvaluatedCall, ShellError> {
|
||||
@ -153,7 +153,7 @@ mod tests {
|
||||
|
||||
let reader = message_reader
|
||||
.get_root::<evaluated_call::Reader>()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
deserialize_call(reader)
|
||||
}
|
||||
|
@ -96,18 +96,18 @@ fn serialize_flag(arg: &Flag, mut builder: flag::Builder) {
|
||||
pub(crate) fn deserialize_signature(reader: signature::Reader) -> Result<Signature, ShellError> {
|
||||
let name = reader
|
||||
.get_name()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
let usage = reader
|
||||
.get_usage()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
let extra_usage = reader
|
||||
.get_extra_usage()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
let is_filter = reader.get_is_filter();
|
||||
|
||||
let category = match reader
|
||||
.get_category()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?
|
||||
{
|
||||
PluginCategory::Default => Category::Default,
|
||||
PluginCategory::Conversions => Category::Conversions,
|
||||
@ -127,7 +127,7 @@ pub(crate) fn deserialize_signature(reader: signature::Reader) -> Result<Signatu
|
||||
// Deserializing required arguments
|
||||
let required_list = reader
|
||||
.get_required_positional()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
let required_positional = required_list
|
||||
.iter()
|
||||
@ -137,7 +137,7 @@ pub(crate) fn deserialize_signature(reader: signature::Reader) -> Result<Signatu
|
||||
// Deserializing optional arguments
|
||||
let optional_list = reader
|
||||
.get_optional_positional()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
let optional_positional = optional_list
|
||||
.iter()
|
||||
@ -148,7 +148,7 @@ pub(crate) fn deserialize_signature(reader: signature::Reader) -> Result<Signatu
|
||||
let rest_positional = if reader.has_rest() {
|
||||
let argument_reader = reader
|
||||
.get_rest()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
Some(deserialize_argument(argument_reader)?)
|
||||
} else {
|
||||
@ -158,7 +158,7 @@ pub(crate) fn deserialize_signature(reader: signature::Reader) -> Result<Signatu
|
||||
// Deserializing named arguments
|
||||
let named_list = reader
|
||||
.get_named()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
let named = named_list
|
||||
.iter()
|
||||
@ -182,15 +182,15 @@ pub(crate) fn deserialize_signature(reader: signature::Reader) -> Result<Signatu
|
||||
fn deserialize_argument(reader: argument::Reader) -> Result<PositionalArg, ShellError> {
|
||||
let name = reader
|
||||
.get_name()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
let desc = reader
|
||||
.get_desc()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
let shape = reader
|
||||
.get_shape()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
let shape = match shape {
|
||||
Shape::String => SyntaxShape::String,
|
||||
@ -212,18 +212,18 @@ fn deserialize_argument(reader: argument::Reader) -> Result<PositionalArg, Shell
|
||||
fn deserialize_flag(reader: flag::Reader) -> Result<Flag, ShellError> {
|
||||
let long = reader
|
||||
.get_long()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
let desc = reader
|
||||
.get_desc()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
let required = reader.get_required();
|
||||
|
||||
let short = if reader.has_short() {
|
||||
let short_reader = reader
|
||||
.get_short()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
short_reader.chars().next()
|
||||
} else {
|
||||
@ -232,7 +232,7 @@ fn deserialize_flag(reader: flag::Reader) -> Result<Flag, ShellError> {
|
||||
|
||||
let arg = reader
|
||||
.get_arg()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
let arg = match arg {
|
||||
Shape::None => None,
|
||||
@ -270,7 +270,7 @@ mod tests {
|
||||
serialize_signature(signature, builder);
|
||||
|
||||
serialize::write_message(writer, &message)
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))
|
||||
}
|
||||
|
||||
pub fn read_buffer(reader: &mut impl std::io::BufRead) -> Result<Signature, ShellError> {
|
||||
@ -279,7 +279,7 @@ mod tests {
|
||||
|
||||
let reader = message_reader
|
||||
.get_root::<signature::Reader>()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
deserialize_signature(reader)
|
||||
}
|
||||
|
@ -63,7 +63,7 @@ pub(crate) fn serialize_value(value: &Value, mut builder: value::Builder) {
|
||||
pub(crate) fn deserialize_value(reader: value::Reader) -> Result<Value, ShellError> {
|
||||
let span_reader = reader
|
||||
.get_span()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
let span = Span {
|
||||
start: span_reader.get_start() as usize,
|
||||
@ -77,26 +77,26 @@ pub(crate) fn deserialize_value(reader: value::Reader) -> Result<Value, ShellErr
|
||||
Ok(value::Float(val)) => Ok(Value::Float { val, span }),
|
||||
Ok(value::String(val)) => {
|
||||
let string = val
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?
|
||||
.to_string();
|
||||
Ok(Value::String { val: string, span })
|
||||
}
|
||||
Ok(value::Record(record)) => {
|
||||
let record = record.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
let record = record.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
let cols = record
|
||||
.get_cols()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?
|
||||
.iter()
|
||||
.map(|col| {
|
||||
col.map_err(|e| ShellError::InternalError(e.to_string()))
|
||||
col.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))
|
||||
.map(|col| col.to_string())
|
||||
})
|
||||
.collect::<Result<Vec<String>, ShellError>>()?;
|
||||
|
||||
let vals = record
|
||||
.get_vals()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?
|
||||
.iter()
|
||||
.map(deserialize_value)
|
||||
.collect::<Result<Vec<Value>, ShellError>>()?;
|
||||
@ -104,7 +104,7 @@ pub(crate) fn deserialize_value(reader: value::Reader) -> Result<Value, ShellErr
|
||||
Ok(Value::Record { cols, vals, span })
|
||||
}
|
||||
Ok(value::List(vals)) => {
|
||||
let values = vals.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
let values = vals.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
let values_list = values
|
||||
.iter()
|
||||
@ -136,7 +136,7 @@ mod tests {
|
||||
serialize_value(value, builder.reborrow());
|
||||
|
||||
serialize::write_message(writer, &message)
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))
|
||||
}
|
||||
|
||||
pub fn read_buffer(reader: &mut impl std::io::BufRead) -> Result<Value, ShellError> {
|
||||
@ -145,7 +145,7 @@ mod tests {
|
||||
|
||||
let reader = message_reader
|
||||
.get_root::<value::Reader>()
|
||||
.map_err(|e| ShellError::InternalError(e.to_string()))?;
|
||||
.map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
|
||||
|
||||
deserialize_value(reader.reborrow())
|
||||
}
|
||||
|
Reference in New Issue
Block a user