First step (#411)

This commit is contained in:
JT 2021-12-03 12:11:25 +13:00 committed by GitHub
parent d9bedaae2f
commit c5297d2b64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 178 additions and 151 deletions

View File

@ -38,8 +38,10 @@ impl Command for Hide {
{ {
pat pat
} else { } else {
return Err(ShellError::InternalError( return Err(ShellError::LabeledError(
"Got something else than import pattern".into(), "Unexpected import".into(),
"import pattern not supported".into(),
call.head,
)); ));
}; };

View File

@ -35,8 +35,10 @@ impl Command for Use {
{ {
pat pat
} else { } else {
return Err(ShellError::InternalError( return Err(ShellError::LabeledError(
"Got something else than import pattern".into(), "Unexpected import".into(),
"import pattern not supported".into(),
call.head,
)); ));
}; };

View File

@ -52,11 +52,11 @@ impl Command for All {
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let predicate = &call.positional[0]; let predicate = &call.positional[0];
let span = call.head;
let block_id = predicate let block_id = predicate
.as_row_condition_block() .as_row_condition_block()
.ok_or_else(|| ShellError::InternalError("Expected row condition".to_owned()))?; .ok_or_else(|| ShellError::TypeMismatch("expected row condition".to_owned(), span))?;
let span = call.head;
let block = engine_state.get_block(block_id); let block = engine_state.get_block(block_id);
let var_id = block.signature.get_positional(0).and_then(|arg| arg.var_id); let var_id = block.signature.get_positional(0).and_then(|arg| arg.var_id);

View File

@ -52,11 +52,11 @@ impl Command for Any {
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let predicate = &call.positional[0]; let predicate = &call.positional[0];
let span = call.head;
let block_id = predicate let block_id = predicate
.as_row_condition_block() .as_row_condition_block()
.ok_or_else(|| ShellError::InternalError("Expected row condition".to_owned()))?; .ok_or_else(|| ShellError::TypeMismatch("expected row condition".to_owned(), span))?;
let span = call.head;
let block = engine_state.get_block(block_id); let block = engine_state.get_block(block_id);
let var_id = block.signature.get_positional(0).and_then(|arg| arg.var_id); let var_id = block.signature.get_positional(0).and_then(|arg| arg.var_id);

View File

@ -59,6 +59,7 @@ impl Command for Skip {
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let n: Option<Value> = call.opt(engine_state, stack, 0)?; let n: Option<Value> = call.opt(engine_state, stack, 0)?;
let span = call.head;
let n: usize = match n { let n: usize = match n {
Some(Value::Int { val, span }) => val.try_into().map_err(|err| { Some(Value::Int { val, span }) => val.try_into().map_err(|err| {
@ -67,7 +68,7 @@ impl Command for Skip {
span, span,
) )
})?, })?,
Some(_) => return Err(ShellError::InternalError("Expected integer".into())), Some(_) => return Err(ShellError::TypeMismatch("expected integer".into(), span)),
None => 1, None => 1,
}; };

View File

@ -47,11 +47,11 @@ impl Command for SkipUntil {
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let predicate = &call.positional[0]; let predicate = &call.positional[0];
let span = call.head;
let block_id = predicate let block_id = predicate
.as_row_condition_block() .as_row_condition_block()
.ok_or_else(|| ShellError::InternalError("Expected row condition".to_owned()))?; .ok_or_else(|| ShellError::TypeMismatch("expected row condition".to_owned(), span))?;
let span = call.head;
let block = engine_state.get_block(block_id).clone(); let block = engine_state.get_block(block_id).clone();
let var_id = block.signature.get_positional(0).and_then(|arg| arg.var_id); let var_id = block.signature.get_positional(0).and_then(|arg| arg.var_id);

View File

@ -47,11 +47,11 @@ impl Command for SkipWhile {
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let predicate = &call.positional[0]; let predicate = &call.positional[0];
let span = call.head;
let block_id = predicate let block_id = predicate
.as_row_condition_block() .as_row_condition_block()
.ok_or_else(|| ShellError::InternalError("Expected row condition".to_owned()))?; .ok_or_else(|| ShellError::TypeMismatch("expected row condition".to_owned(), span))?;
let span = call.head;
let block = engine_state.get_block(block_id).clone(); let block = engine_state.get_block(block_id).clone();
let var_id = block.signature.get_positional(0).and_then(|arg| arg.var_id); let var_id = block.signature.get_positional(0).and_then(|arg| arg.var_id);

View File

@ -28,11 +28,12 @@ impl Command for Where {
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> { ) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let span = call.head;
let cond = &call.positional[0]; let cond = &call.positional[0];
let span = call.head;
let block_id = cond let block_id = cond
.as_row_condition_block() .as_row_condition_block()
.ok_or_else(|| ShellError::InternalError("Expected row condition".to_owned()))?; .ok_or_else(|| ShellError::TypeMismatch("expected row condition".to_owned(), span))?;
let ctrlc = engine_state.ctrlc.clone(); let ctrlc = engine_state.ctrlc.clone();
let engine_state = engine_state.clone(); let engine_state = engine_state.clone();

View File

@ -2,8 +2,8 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Value,
Value, ValueStream, ValueStream,
}; };
use regex::Regex; use regex::Regex;
@ -117,11 +117,11 @@ fn operate(
} }
} }
Err(_) => { Err(_) => {
return Err(ShellError::PipelineMismatch { return Err(ShellError::PipelineMismatch(
expected: Type::String, "string".into(),
expected_span: head, head,
origin: v.span()?, v.span()?,
}) ))
} }
} }
} }

View File

@ -4,7 +4,7 @@ use unicode_segmentation::UnicodeSegmentation;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, Span, Type, Value}; use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, Span, Value};
#[derive(Clone)] #[derive(Clone)]
pub struct Size; pub struct Size;
@ -110,11 +110,7 @@ fn size(
move |v| match v.as_string() { move |v| match v.as_string() {
Ok(s) => count(&s, span), Ok(s) => count(&s, span),
Err(_) => Value::Error { Err(_) => Value::Error {
error: ShellError::PipelineMismatch { error: ShellError::PipelineMismatch("string".into(), span, span),
expected: Type::String,
expected_span: span,
origin: span,
},
}, },
}, },
engine_state.ctrlc.clone(), engine_state.ctrlc.clone(),

View File

@ -1,7 +1,7 @@
use nu_protocol::{ use nu_protocol::{
ast::Call, ast::Call,
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, Type, Value, Category, Example, PipelineData, ShellError, Signature, Span, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -72,11 +72,7 @@ fn split_chars_helper(v: &Value, name: Span) -> Vec<Value> {
.collect() .collect()
} else { } else {
vec![Value::Error { vec![Value::Error {
error: ShellError::PipelineMismatch { error: ShellError::PipelineMismatch("string".into(), name, v_span),
expected: Type::String,
expected_span: name,
origin: v_span,
},
}] }]
} }
} }

View File

@ -2,7 +2,7 @@ use nu_engine::CallExt;
use nu_protocol::{ use nu_protocol::{
ast::Call, ast::Call,
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack},
Category, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value, Category, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -107,11 +107,7 @@ fn split_column_helper(
} else { } else {
match v.span() { match v.span() {
Ok(span) => vec![Value::Error { Ok(span) => vec![Value::Error {
error: ShellError::PipelineMismatch { error: ShellError::PipelineMismatch("string".into(), head, span),
expected: Type::String,
expected_span: head,
origin: span,
},
}], }],
Err(error) => vec![Value::Error { error }], Err(error) => vec![Value::Error { error }],
} }

View File

@ -2,7 +2,7 @@ use nu_engine::CallExt;
use nu_protocol::{ use nu_protocol::{
ast::Call, ast::Call,
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack},
Category, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value, Category, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -69,11 +69,7 @@ fn split_row_helper(v: &Value, separator: &Spanned<String>, name: Span) -> Vec<V
.collect() .collect()
} else { } else {
vec![Value::Error { vec![Value::Error {
error: ShellError::PipelineMismatch { error: ShellError::PipelineMismatch("string".into(), name, v_span),
expected: Type::String,
expected_span: name,
origin: v_span,
},
}] }]
} }
} }

View File

@ -37,7 +37,7 @@ pub fn get_signature(path: &Path) -> Result<Vec<Signature>, ShellError> {
let mut plugin_cmd = create_command(path); let mut plugin_cmd = create_command(path);
let mut child = plugin_cmd.spawn().map_err(|err| { 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 // 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 { match response {
PluginResponse::Signature(sign) => Ok(sign), PluginResponse::Signature(sign) => Ok(sign),
PluginResponse::Error(msg) => Err(ShellError::InternalError(format!( PluginResponse::Error(msg) => Err(ShellError::PluginFailedToLoad(format!(
"Plugin response error {}", "Plugin response error {}",
msg, msg,
))), ))),
_ => Err(ShellError::InternalError("Plugin missing signature".into())), _ => Err(ShellError::PluginFailedToLoad(
"Plugin missing signature".into(),
)),
} }
} else { } else {
Err(ShellError::InternalError( Err(ShellError::PluginFailedToLoad(
"Plugin missing stdout reader".into(), "Plugin missing stdout reader".into(),
)) ))
}?; }?;
@ -285,7 +287,7 @@ pub fn eval_plugin_signatures(working_set: &mut StateWorkingSet) -> Result<(), S
plugin_decl plugin_decl
}) })
.collect::<Vec<Box<dyn Command>>>()), .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 // Need to collect the vector in order to check the error from getting the signature

View File

@ -25,55 +25,56 @@ pub fn encode_call(
let call_builder = call_info_builder let call_builder = call_info_builder
.reborrow() .reborrow()
.get_call() .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) 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 // Serializing the input value from the call info
let value_builder = call_info_builder let value_builder = call_info_builder
.reborrow() .reborrow()
.get_input() .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); 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> { pub fn decode_call(reader: &mut impl std::io::BufRead) -> Result<PluginCall, ShellError> {
let message_reader = serialize::read_message(reader, ::capnp::message::ReaderOptions::new()) 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 let reader = message_reader
.get_root::<plugin_call::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() { 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::Signature(())) => Ok(PluginCall::Signature),
Ok(plugin_call::CallInfo(reader)) => { 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 let name = reader
.get_name() .get_name()
.map_err(|e| ShellError::InternalError(e.to_string()))?; .map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
let call_reader = reader let call_reader = reader
.get_call() .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) let call = call::deserialize_call(call_reader)?;
.map_err(|e| ShellError::InternalError(e.to_string()))?;
let input_reader = reader let input_reader = reader
.get_input() .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) let input = value::deserialize_value(input_reader)?;
.map_err(|e| ShellError::InternalError(e.to_string()))?;
Ok(PluginCall::CallInfo(Box::new(CallInfo { Ok(PluginCall::CallInfo(Box::new(CallInfo {
name: name.to_string(), 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> { pub fn decode_response(reader: &mut impl std::io::BufRead) -> Result<PluginResponse, ShellError> {
let message_reader = serialize::read_message(reader, ::capnp::message::ReaderOptions::new()) 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 let reader = message_reader
.get_root::<plugin_response::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() { 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)) => { 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(PluginResponse::Error(msg.to_string()))
} }
Ok(plugin_response::Signature(reader)) => { 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 let signatures = reader
.iter() .iter()
@ -138,9 +142,9 @@ pub fn decode_response(reader: &mut impl std::io::BufRead) -> Result<PluginRespo
Ok(PluginResponse::Signature(signatures)) Ok(PluginResponse::Signature(signatures))
} }
Ok(plugin_response::Value(reader)) => { 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) 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))) Ok(PluginResponse::Value(Box::new(val)))
} }

View File

@ -38,7 +38,7 @@ fn serialize_named(
entry_builder entry_builder
.reborrow() .reborrow()
.set_key(key.item.as_str()) .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 { if let Some(value) = expression {
let value_builder = entry_builder.init_value(); let value_builder = entry_builder.init_value();
@ -54,7 +54,7 @@ pub(crate) fn deserialize_call(
) -> Result<EvaluatedCall, ShellError> { ) -> Result<EvaluatedCall, ShellError> {
let head_reader = reader let head_reader = reader
.get_head() .get_head()
.map_err(|e| ShellError::InternalError(e.to_string()))?; .map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
let head = Span { let head = Span {
start: head_reader.get_start() as usize, start: head_reader.get_start() as usize,
@ -77,7 +77,7 @@ fn deserialize_positionals(
) -> Result<Vec<Value>, ShellError> { ) -> Result<Vec<Value>, ShellError> {
let positional_reader = reader let positional_reader = reader
.get_positional() .get_positional()
.map_err(|e| ShellError::InternalError(e.to_string()))?; .map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
positional_reader positional_reader
.iter() .iter()
@ -90,11 +90,11 @@ type NamedList = Vec<(Spanned<String>, Option<Value>)>;
fn deserialize_named(span: Span, reader: evaluated_call::Reader) -> Result<NamedList, ShellError> { fn deserialize_named(span: Span, reader: evaluated_call::Reader) -> Result<NamedList, ShellError> {
let named_reader = reader let named_reader = reader
.get_named() .get_named()
.map_err(|e| ShellError::InternalError(e.to_string()))?; .map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
let entries_list = named_reader let entries_list = named_reader
.get_entries() .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>)> = let mut entries: Vec<(Spanned<String>, Option<Value>)> =
Vec::with_capacity(entries_list.len() as usize); 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 { for entry_reader in entries_list {
let item = entry_reader let item = entry_reader
.get_key() .get_key()
.map_err(|e| ShellError::InternalError(e.to_string()))? .map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?
.to_string(); .to_string();
let value = if entry_reader.has_value() { let value = if entry_reader.has_value() {
let value_reader = entry_reader let value_reader = entry_reader
.get_value() .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) 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) Some(value)
} else { } else {
@ -144,7 +144,7 @@ mod tests {
serialize_call(call, builder)?; serialize_call(call, builder)?;
serialize::write_message(writer, &message) 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> { fn read_buffer(reader: &mut impl std::io::BufRead) -> Result<EvaluatedCall, ShellError> {
@ -153,7 +153,7 @@ mod tests {
let reader = message_reader let reader = message_reader
.get_root::<evaluated_call::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) deserialize_call(reader)
} }

View File

@ -96,18 +96,18 @@ fn serialize_flag(arg: &Flag, mut builder: flag::Builder) {
pub(crate) fn deserialize_signature(reader: signature::Reader) -> Result<Signature, ShellError> { pub(crate) fn deserialize_signature(reader: signature::Reader) -> Result<Signature, ShellError> {
let name = reader let name = reader
.get_name() .get_name()
.map_err(|e| ShellError::InternalError(e.to_string()))?; .map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
let usage = reader let usage = reader
.get_usage() .get_usage()
.map_err(|e| ShellError::InternalError(e.to_string()))?; .map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
let extra_usage = reader let extra_usage = reader
.get_extra_usage() .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 is_filter = reader.get_is_filter();
let category = match reader let category = match reader
.get_category() .get_category()
.map_err(|e| ShellError::InternalError(e.to_string()))? .map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?
{ {
PluginCategory::Default => Category::Default, PluginCategory::Default => Category::Default,
PluginCategory::Conversions => Category::Conversions, PluginCategory::Conversions => Category::Conversions,
@ -127,7 +127,7 @@ pub(crate) fn deserialize_signature(reader: signature::Reader) -> Result<Signatu
// Deserializing required arguments // Deserializing required arguments
let required_list = reader let required_list = reader
.get_required_positional() .get_required_positional()
.map_err(|e| ShellError::InternalError(e.to_string()))?; .map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
let required_positional = required_list let required_positional = required_list
.iter() .iter()
@ -137,7 +137,7 @@ pub(crate) fn deserialize_signature(reader: signature::Reader) -> Result<Signatu
// Deserializing optional arguments // Deserializing optional arguments
let optional_list = reader let optional_list = reader
.get_optional_positional() .get_optional_positional()
.map_err(|e| ShellError::InternalError(e.to_string()))?; .map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
let optional_positional = optional_list let optional_positional = optional_list
.iter() .iter()
@ -148,7 +148,7 @@ pub(crate) fn deserialize_signature(reader: signature::Reader) -> Result<Signatu
let rest_positional = if reader.has_rest() { let rest_positional = if reader.has_rest() {
let argument_reader = reader let argument_reader = reader
.get_rest() .get_rest()
.map_err(|e| ShellError::InternalError(e.to_string()))?; .map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
Some(deserialize_argument(argument_reader)?) Some(deserialize_argument(argument_reader)?)
} else { } else {
@ -158,7 +158,7 @@ pub(crate) fn deserialize_signature(reader: signature::Reader) -> Result<Signatu
// Deserializing named arguments // Deserializing named arguments
let named_list = reader let named_list = reader
.get_named() .get_named()
.map_err(|e| ShellError::InternalError(e.to_string()))?; .map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
let named = named_list let named = named_list
.iter() .iter()
@ -182,15 +182,15 @@ pub(crate) fn deserialize_signature(reader: signature::Reader) -> Result<Signatu
fn deserialize_argument(reader: argument::Reader) -> Result<PositionalArg, ShellError> { fn deserialize_argument(reader: argument::Reader) -> Result<PositionalArg, ShellError> {
let name = reader let name = reader
.get_name() .get_name()
.map_err(|e| ShellError::InternalError(e.to_string()))?; .map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
let desc = reader let desc = reader
.get_desc() .get_desc()
.map_err(|e| ShellError::InternalError(e.to_string()))?; .map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
let shape = reader let shape = reader
.get_shape() .get_shape()
.map_err(|e| ShellError::InternalError(e.to_string()))?; .map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
let shape = match shape { let shape = match shape {
Shape::String => SyntaxShape::String, 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> { fn deserialize_flag(reader: flag::Reader) -> Result<Flag, ShellError> {
let long = reader let long = reader
.get_long() .get_long()
.map_err(|e| ShellError::InternalError(e.to_string()))?; .map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
let desc = reader let desc = reader
.get_desc() .get_desc()
.map_err(|e| ShellError::InternalError(e.to_string()))?; .map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
let required = reader.get_required(); let required = reader.get_required();
let short = if reader.has_short() { let short = if reader.has_short() {
let short_reader = reader let short_reader = reader
.get_short() .get_short()
.map_err(|e| ShellError::InternalError(e.to_string()))?; .map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
short_reader.chars().next() short_reader.chars().next()
} else { } else {
@ -232,7 +232,7 @@ fn deserialize_flag(reader: flag::Reader) -> Result<Flag, ShellError> {
let arg = reader let arg = reader
.get_arg() .get_arg()
.map_err(|e| ShellError::InternalError(e.to_string()))?; .map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
let arg = match arg { let arg = match arg {
Shape::None => None, Shape::None => None,
@ -270,7 +270,7 @@ mod tests {
serialize_signature(signature, builder); serialize_signature(signature, builder);
serialize::write_message(writer, &message) 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> { pub fn read_buffer(reader: &mut impl std::io::BufRead) -> Result<Signature, ShellError> {
@ -279,7 +279,7 @@ mod tests {
let reader = message_reader let reader = message_reader
.get_root::<signature::Reader>() .get_root::<signature::Reader>()
.map_err(|e| ShellError::InternalError(e.to_string()))?; .map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
deserialize_signature(reader) deserialize_signature(reader)
} }

View File

@ -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> { pub(crate) fn deserialize_value(reader: value::Reader) -> Result<Value, ShellError> {
let span_reader = reader let span_reader = reader
.get_span() .get_span()
.map_err(|e| ShellError::InternalError(e.to_string()))?; .map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?;
let span = Span { let span = Span {
start: span_reader.get_start() as usize, 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::Float(val)) => Ok(Value::Float { val, span }),
Ok(value::String(val)) => { Ok(value::String(val)) => {
let string = val let string = val
.map_err(|e| ShellError::InternalError(e.to_string()))? .map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?
.to_string(); .to_string();
Ok(Value::String { val: string, span }) Ok(Value::String { val: string, span })
} }
Ok(value::Record(record)) => { 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 let cols = record
.get_cols() .get_cols()
.map_err(|e| ShellError::InternalError(e.to_string()))? .map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?
.iter() .iter()
.map(|col| { .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()) .map(|col| col.to_string())
}) })
.collect::<Result<Vec<String>, ShellError>>()?; .collect::<Result<Vec<String>, ShellError>>()?;
let vals = record let vals = record
.get_vals() .get_vals()
.map_err(|e| ShellError::InternalError(e.to_string()))? .map_err(|e| ShellError::PluginFailedToLoad(e.to_string()))?
.iter() .iter()
.map(deserialize_value) .map(deserialize_value)
.collect::<Result<Vec<Value>, ShellError>>()?; .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::Record { cols, vals, span })
} }
Ok(value::List(vals)) => { 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 let values_list = values
.iter() .iter()
@ -136,7 +136,7 @@ mod tests {
serialize_value(value, builder.reborrow()); serialize_value(value, builder.reborrow());
serialize::write_message(writer, &message) 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> { pub fn read_buffer(reader: &mut impl std::io::BufRead) -> Result<Value, ShellError> {
@ -145,7 +145,7 @@ mod tests {
let reader = message_reader let reader = message_reader
.get_root::<value::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()) deserialize_value(reader.reborrow())
} }

View File

@ -220,10 +220,8 @@ impl EngineState {
// Updating the signatures plugin file with the added signatures // Updating the signatures plugin file with the added signatures
if let Some(plugin_path) = &self.plugin_signatures { if let Some(plugin_path) = &self.plugin_signatures {
// Always creating the file which will erase previous signatures // Always create the file, which will erase previous signatures
let mut plugin_file = std::fs::File::create(plugin_path.as_path()) if let Ok(mut plugin_file) = std::fs::File::create(plugin_path.as_path()) {
.map_err(|err| ShellError::InternalError(err.to_string()))?;
// Plugin definitions with parsed signature // Plugin definitions with parsed signature
for decl in self.plugin_decls() { for decl in self.plugin_decls() {
// A successful plugin registration already includes the plugin filename // A successful plugin registration already includes the plugin filename
@ -233,16 +231,22 @@ impl EngineState {
let line = serde_json::to_string_pretty(&decl.signature()) let line = serde_json::to_string_pretty(&decl.signature())
.map(|signature| format!("register {} {}\n", file_name, signature)) .map(|signature| format!("register {} {}\n", file_name, signature))
.map_err(|err| ShellError::InternalError(err.to_string()))?; .map_err(|err| ShellError::PluginFailedToLoad(err.to_string()))?;
plugin_file plugin_file
.write_all(line.as_bytes()) .write_all(line.as_bytes())
.map_err(|err| ShellError::InternalError(err.to_string()))?; .map_err(|err| ShellError::PluginFailedToLoad(err.to_string()))?;
} }
Ok(()) Ok(())
} else { } else {
Err(ShellError::InternalError("Plugin file not found".into())) Err(ShellError::PluginFailedToLoad(
"Plugin file not found".into(),
))
}
} else {
Err(ShellError::PluginFailedToLoad(
"Plugin file not found".into(),
))
} }
} }

View File

@ -46,7 +46,7 @@ impl Stack {
return Ok(v.clone()); return Ok(v.clone());
} }
Err(ShellError::InternalError("variable not found".into())) Err(ShellError::NushellFailed("variable not found".into()))
} }
pub fn add_var(&mut self, var_id: VarId, value: Value) { pub fn add_var(&mut self, var_id: VarId, value: Value) {

View File

@ -28,13 +28,15 @@ pub enum ShellError {
#[error("Pipeline mismatch.")] #[error("Pipeline mismatch.")]
#[diagnostic(code(nu::shell::pipeline_mismatch), url(docsrs))] #[diagnostic(code(nu::shell::pipeline_mismatch), url(docsrs))]
PipelineMismatch { PipelineMismatch(
expected: Type, String,
#[label("expected: {expected}")] #[label("expected: {0}")] Span,
expected_span: Span, #[label("value originates from here")] Span,
#[label("value originates from here")] ),
origin: Span,
}, #[error("Type mismatch")]
#[diagnostic(code(nu::shell::type_mismatch), url(docsrs))]
TypeMismatch(String, #[label = "{0}"] Span),
#[error("Unsupported operator: {0}.")] #[error("Unsupported operator: {0}.")]
#[diagnostic(code(nu::shell::unsupported_operator), url(docsrs))] #[diagnostic(code(nu::shell::unsupported_operator), url(docsrs))]
@ -84,9 +86,10 @@ pub enum ShellError {
#[diagnostic(code(nu::shell::invalid_range), url(docsrs))] #[diagnostic(code(nu::shell::invalid_range), url(docsrs))]
InvalidRange(String, String, #[label = "expected a valid range"] Span), InvalidRange(String, String, #[label = "expected a valid range"] Span),
#[error("Internal error: {0}.")] // Only use this one if we Nushell completely falls over and hits a state that isn't possible or isn't recoverable
#[diagnostic(code(nu::shell::internal_error), url(docsrs))] #[error("Nushell failed: {0}.")]
InternalError(String), #[diagnostic(code(nu::shell::nushell_failed), url(docsrs))]
NushellFailed(String),
#[error("Variable not found!!!")] #[error("Variable not found!!!")]
#[diagnostic(code(nu::shell::variable_not_found), url(docsrs))] #[diagnostic(code(nu::shell::variable_not_found), url(docsrs))]
@ -166,6 +169,14 @@ pub enum ShellError {
#[diagnostic(code(nu::shell::file_not_found), url(docsrs))] #[diagnostic(code(nu::shell::file_not_found), url(docsrs))]
FileNotFoundCustom(String, #[label("{0}")] Span), FileNotFoundCustom(String, #[label("{0}")] Span),
#[error("Plugin failed to load")]
#[diagnostic(code(nu::shell::plugin_fialed_to_load), url(docsrs))]
PluginFailedToLoad(String),
#[error("I/O error")]
#[diagnostic(code(nu::shell::io_error), url(docsrs))]
IOError(String),
#[error("Directory not found")] #[error("Directory not found")]
#[diagnostic(code(nu::shell::directory_not_found), url(docsrs))] #[diagnostic(code(nu::shell::directory_not_found), url(docsrs))]
DirectoryNotFound(#[label("directory not found")] Span), DirectoryNotFound(#[label("directory not found")] Span),
@ -223,19 +234,19 @@ pub enum ShellError {
impl From<std::io::Error> for ShellError { impl From<std::io::Error> for ShellError {
fn from(input: std::io::Error) -> ShellError { fn from(input: std::io::Error) -> ShellError {
ShellError::InternalError(format!("{:?}", input)) ShellError::IOError(format!("{:?}", input))
} }
} }
impl std::convert::From<Box<dyn std::error::Error>> for ShellError { impl std::convert::From<Box<dyn std::error::Error>> for ShellError {
fn from(input: Box<dyn std::error::Error>) -> ShellError { fn from(input: Box<dyn std::error::Error>) -> ShellError {
ShellError::InternalError(input.to_string()) ShellError::IOError(input.to_string())
} }
} }
impl From<Box<dyn std::error::Error + Send + Sync>> for ShellError { impl From<Box<dyn std::error::Error + Send + Sync>> for ShellError {
fn from(input: Box<dyn std::error::Error + Send + Sync>) -> ShellError { fn from(input: Box<dyn std::error::Error + Send + Sync>) -> ShellError {
ShellError::InternalError(format!("{:?}", input)) ShellError::IOError(format!("{:?}", input))
} }
} }

View File

@ -44,8 +44,10 @@ impl Plugin for Example {
match name { match name {
"test-1" => test1(call, input), "test-1" => test1(call, input),
"test-2" => test2(call, input), "test-2" => test2(call, input),
_ => Err(ShellError::InternalError( _ => Err(ShellError::LabeledError(
"Plugin call with wrong name signature".into(), "Plugin call with wrong name signature".into(),
"using the wrong signature".into(),
call.head,
)), )),
} }
} }

View File

@ -81,14 +81,28 @@ impl Inc {
"Usage: inc field [--major|--minor|--patch]" "Usage: inc field [--major|--minor|--patch]"
} }
pub fn inc(&self, value: &Value) -> Result<Value, ShellError> { pub fn inc(&self, head: Span, value: &Value) -> Result<Value, ShellError> {
match value { match value {
Value::Int { val, span } => Ok(Value::Int { Value::Int { val, span } => Ok(Value::Int {
val: val + 1, val: val + 1,
span: *span, span: *span,
}), }),
Value::String { val, .. } => Ok(self.apply(val)), Value::String { val, .. } => Ok(self.apply(val)),
_ => Err(ShellError::InternalError("incrementable value".to_string())), x => {
if let Ok(span) = x.span() {
Err(ShellError::PipelineMismatch(
"incrementable value".into(),
head,
span,
))
} else {
Err(ShellError::LabeledError(
"Expected incrementable value".into(),
"incrementable value".into(),
head,
))
}
}
} }
} }
} }

View File

@ -46,6 +46,6 @@ impl Plugin for Inc {
self.for_semver(SemVerAction::Patch); self.for_semver(SemVerAction::Patch);
} }
self.inc(input) self.inc(call.head, input)
} }
} }