First step (#411)

This commit is contained in:
JT
2021-12-03 12:11:25 +13:00
committed by GitHub
parent d9bedaae2f
commit c5297d2b64
24 changed files with 178 additions and 151 deletions

View File

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

View File

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

View File

@ -52,11 +52,11 @@ impl Command for All {
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let predicate = &call.positional[0];
let span = call.head;
let block_id = predicate
.as_row_condition_block()
.ok_or_else(|| ShellError::InternalError("Expected row condition".to_owned()))?;
let span = call.head;
.ok_or_else(|| ShellError::TypeMismatch("expected row condition".to_owned(), span))?;
let block = engine_state.get_block(block_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,
) -> Result<PipelineData, ShellError> {
let predicate = &call.positional[0];
let span = call.head;
let block_id = predicate
.as_row_condition_block()
.ok_or_else(|| ShellError::InternalError("Expected row condition".to_owned()))?;
let span = call.head;
.ok_or_else(|| ShellError::TypeMismatch("expected row condition".to_owned(), span))?;
let block = engine_state.get_block(block_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,
) -> Result<PipelineData, ShellError> {
let n: Option<Value> = call.opt(engine_state, stack, 0)?;
let span = call.head;
let n: usize = match n {
Some(Value::Int { val, span }) => val.try_into().map_err(|err| {
@ -67,7 +68,7 @@ impl Command for Skip {
span,
)
})?,
Some(_) => return Err(ShellError::InternalError("Expected integer".into())),
Some(_) => return Err(ShellError::TypeMismatch("expected integer".into(), span)),
None => 1,
};

View File

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

View File

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

View File

@ -4,7 +4,7 @@ use unicode_segmentation::UnicodeSegmentation;
use nu_protocol::ast::Call;
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)]
pub struct Size;
@ -110,11 +110,7 @@ fn size(
move |v| match v.as_string() {
Ok(s) => count(&s, span),
Err(_) => Value::Error {
error: ShellError::PipelineMismatch {
expected: Type::String,
expected_span: span,
origin: span,
},
error: ShellError::PipelineMismatch("string".into(), span, span),
},
},
engine_state.ctrlc.clone(),

View File

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

View File

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

View File

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