Document and critically review ShellError
variants - Ep. 2 (#8326)
Continuation of #8229 # 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. **Everyone:** Feel free to add review comments if you spot inconsistent use of `ShellError` variants. - Name fields of `SE::IncorrectValue` - Merge and name fields on `SE::TypeMismatch` - Name fields on `SE::UnsupportedOperator` - Name fields on `AssignmentRequires*` and fix doc - Name fields on `SE::UnknownOperator` - Name fields on `SE::MissingParameter` - Name fields on `SE::DelimiterError` - Name fields on `SE::IncompatibleParametersSingle` # User-Facing Changes (None now, end goal more explicit and consistent error messages) # Tests + Formatting (No additional tests needed so far)
This commit is contained in:
parent
4ae1b1cc26
commit
f7b8f97873
|
@ -846,10 +846,10 @@ pub fn eval_env_change_hook(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
x => {
|
x => {
|
||||||
return Err(ShellError::TypeMismatch(
|
return Err(ShellError::TypeMismatch {
|
||||||
"record for the 'env_change' hook".to_string(),
|
err_message: "record for the 'env_change' hook".to_string(),
|
||||||
x.span()?,
|
span: x.span()?,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1109,10 +1109,10 @@ fn run_hook_block(
|
||||||
if let Some(arg) = arguments.get(idx) {
|
if let Some(arg) = arguments.get(idx) {
|
||||||
callee_stack.add_var(*var_id, arg.1.clone())
|
callee_stack.add_var(*var_id, arg.1.clone())
|
||||||
} else {
|
} else {
|
||||||
return Err(ShellError::IncompatibleParametersSingle(
|
return Err(ShellError::IncompatibleParametersSingle {
|
||||||
"This hook block has too many parameters".into(),
|
msg: "This hook block has too many parameters".into(),
|
||||||
span,
|
span,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -277,10 +277,10 @@ fn at_impl(input: &[u8], arg: &Arguments, span: Span) -> Value {
|
||||||
match start.cmp(&end) {
|
match start.cmp(&end) {
|
||||||
Ordering::Equal => Value::Binary { val: vec![], span },
|
Ordering::Equal => Value::Binary { val: vec![], span },
|
||||||
Ordering::Greater => Value::Error {
|
Ordering::Greater => Value::Error {
|
||||||
error: ShellError::TypeMismatch(
|
error: ShellError::TypeMismatch {
|
||||||
"End must be greater than or equal to Start".to_string(),
|
err_message: "End must be greater than or equal to Start".to_string(),
|
||||||
arg.arg_span,
|
span: arg.arg_span,
|
||||||
),
|
},
|
||||||
},
|
},
|
||||||
Ordering::Less => Value::Binary {
|
Ordering::Less => Value::Binary {
|
||||||
val: {
|
val: {
|
||||||
|
|
|
@ -55,10 +55,10 @@ impl Command for BytesBuild {
|
||||||
// Explicitly propagate errors instead of dropping them.
|
// Explicitly propagate errors instead of dropping them.
|
||||||
Value::Error { error } => return Err(error),
|
Value::Error { error } => return Err(error),
|
||||||
other => {
|
other => {
|
||||||
return Err(ShellError::TypeMismatch(
|
return Err(ShellError::TypeMismatch {
|
||||||
"only binary data arguments are supported".to_string(),
|
err_message: "only binary data arguments are supported".to_string(),
|
||||||
other.expect_span(),
|
span: other.expect_span(),
|
||||||
))
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,10 +61,10 @@ impl Command for BytesRemove {
|
||||||
let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);
|
let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);
|
||||||
let pattern_to_remove = call.req::<Spanned<Vec<u8>>>(engine_state, stack, 0)?;
|
let pattern_to_remove = call.req::<Spanned<Vec<u8>>>(engine_state, stack, 0)?;
|
||||||
if pattern_to_remove.item.is_empty() {
|
if pattern_to_remove.item.is_empty() {
|
||||||
return Err(ShellError::TypeMismatch(
|
return Err(ShellError::TypeMismatch {
|
||||||
"the pattern to remove cannot be empty".to_string(),
|
err_message: "the pattern to remove cannot be empty".to_string(),
|
||||||
pattern_to_remove.span,
|
span: pattern_to_remove.span,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let pattern_to_remove: Vec<u8> = pattern_to_remove.item;
|
let pattern_to_remove: Vec<u8> = pattern_to_remove.item;
|
||||||
|
|
|
@ -61,10 +61,10 @@ impl Command for BytesReplace {
|
||||||
let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);
|
let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);
|
||||||
let find = call.req::<Spanned<Vec<u8>>>(engine_state, stack, 0)?;
|
let find = call.req::<Spanned<Vec<u8>>>(engine_state, stack, 0)?;
|
||||||
if find.item.is_empty() {
|
if find.item.is_empty() {
|
||||||
return Err(ShellError::TypeMismatch(
|
return Err(ShellError::TypeMismatch {
|
||||||
"the pattern to find cannot be empty".to_string(),
|
err_message: "the pattern to find cannot be empty".to_string(),
|
||||||
find.span,
|
span: find.span,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let arg = Arguments {
|
let arg = Arguments {
|
||||||
|
|
|
@ -99,11 +99,12 @@ impl Command for Histogram {
|
||||||
let frequency_column_name = match frequency_name_arg {
|
let frequency_column_name = match frequency_name_arg {
|
||||||
Some(inner) => {
|
Some(inner) => {
|
||||||
if ["value", "count", "quantile", "percentage"].contains(&inner.item.as_str()) {
|
if ["value", "count", "quantile", "percentage"].contains(&inner.item.as_str()) {
|
||||||
return Err(ShellError::TypeMismatch(
|
return Err(ShellError::TypeMismatch {
|
||||||
|
err_message:
|
||||||
"frequency-column-name can't be 'value', 'count' or 'percentage'"
|
"frequency-column-name can't be 'value', 'count' or 'percentage'"
|
||||||
.to_string(),
|
.to_string(),
|
||||||
inner.span,
|
span: inner.span,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
inner.item
|
inner.item
|
||||||
}
|
}
|
||||||
|
@ -118,10 +119,11 @@ impl Command for Histogram {
|
||||||
"normalize" => PercentageCalcMethod::Normalize,
|
"normalize" => PercentageCalcMethod::Normalize,
|
||||||
"relative" => PercentageCalcMethod::Relative,
|
"relative" => PercentageCalcMethod::Relative,
|
||||||
_ => {
|
_ => {
|
||||||
return Err(ShellError::TypeMismatch(
|
return Err(ShellError::TypeMismatch {
|
||||||
"calc method can only be 'normalize' or 'relative'".to_string(),
|
err_message: "calc method can only be 'normalize' or 'relative'"
|
||||||
inner.span,
|
.to_string(),
|
||||||
))
|
span: inner.span,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -290,10 +290,10 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
|
||||||
},
|
},
|
||||||
Zone::Error => Value::Error {
|
Zone::Error => Value::Error {
|
||||||
// This is an argument error, not an input error
|
// This is an argument error, not an input error
|
||||||
error: ShellError::TypeMismatch(
|
error: ShellError::TypeMismatch {
|
||||||
"Invalid timezone or offset".to_string(),
|
err_message: "Invalid timezone or offset".to_string(),
|
||||||
*span,
|
span: *span,
|
||||||
),
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -70,10 +70,10 @@ impl Command for SubCommand {
|
||||||
let radix: u32 = match radix {
|
let radix: u32 = match radix {
|
||||||
Some(Value::Int { val, span }) => {
|
Some(Value::Int { val, span }) => {
|
||||||
if !(2..=36).contains(&val) {
|
if !(2..=36).contains(&val) {
|
||||||
return Err(ShellError::TypeMismatch(
|
return Err(ShellError::TypeMismatch {
|
||||||
"Radix must lie in the range [2, 36]".to_string(),
|
err_message: "Radix must lie in the range [2, 36]".to_string(),
|
||||||
span,
|
span,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
val as u32
|
val as u32
|
||||||
}
|
}
|
||||||
|
|
|
@ -154,10 +154,10 @@ fn string_helper(
|
||||||
let decimals_value: Option<i64> = call.get_flag(engine_state, stack, "decimals")?;
|
let decimals_value: Option<i64> = call.get_flag(engine_state, stack, "decimals")?;
|
||||||
if let Some(decimal_val) = decimals_value {
|
if let Some(decimal_val) = decimals_value {
|
||||||
if decimals && decimal_val.is_negative() {
|
if decimals && decimal_val.is_negative() {
|
||||||
return Err(ShellError::TypeMismatch(
|
return Err(ShellError::TypeMismatch {
|
||||||
"Cannot accept negative integers for decimals arguments".to_string(),
|
err_message: "Cannot accept negative integers for decimals arguments".to_string(),
|
||||||
head,
|
span: head,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let cell_paths = call.rest(engine_state, stack, 0)?;
|
let cell_paths = call.rest(engine_state, stack, 0)?;
|
||||||
|
|
|
@ -156,10 +156,10 @@ fn command_lazy(
|
||||||
|
|
||||||
if columns.len() != new_names.len() {
|
if columns.len() != new_names.len() {
|
||||||
let value: Value = call.req(engine_state, stack, 1)?;
|
let value: Value = call.req(engine_state, stack, 1)?;
|
||||||
return Err(ShellError::IncompatibleParametersSingle(
|
return Err(ShellError::IncompatibleParametersSingle {
|
||||||
"New name list has different size to column list".into(),
|
msg: "New name list has different size to column list".into(),
|
||||||
value.span()?,
|
span: value.span()?,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let lazy = lazy.into_polars();
|
let lazy = lazy.into_polars();
|
||||||
|
|
|
@ -83,10 +83,10 @@ impl Command for ExprIsIn {
|
||||||
let list = values.as_series(call.head)?;
|
let list = values.as_series(call.head)?;
|
||||||
|
|
||||||
if matches!(list.dtype(), DataType::Object(..)) {
|
if matches!(list.dtype(), DataType::Object(..)) {
|
||||||
return Err(ShellError::IncompatibleParametersSingle(
|
return Err(ShellError::IncompatibleParametersSingle {
|
||||||
"Cannot use a mixed list as argument".into(),
|
msg: "Cannot use a mixed list as argument".into(),
|
||||||
call.head,
|
span: call.head,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let expr: NuExpression = expr.into_polars().is_in(lit(list)).into();
|
let expr: NuExpression = expr.into_polars().is_in(lit(list)).into();
|
||||||
|
|
|
@ -122,10 +122,10 @@ impl Command for ToLazyGroupBy {
|
||||||
.any(|expr| !matches!(expr, Expr::Column(..)))
|
.any(|expr| !matches!(expr, Expr::Column(..)))
|
||||||
{
|
{
|
||||||
let value: Value = call.req(engine_state, stack, 0)?;
|
let value: Value = call.req(engine_state, stack, 0)?;
|
||||||
return Err(ShellError::IncompatibleParametersSingle(
|
return Err(ShellError::IncompatibleParametersSingle {
|
||||||
"Expected only Col expressions".into(),
|
msg: "Expected only Col expressions".into(),
|
||||||
value.span()?,
|
span: value.span()?,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let lazy = NuLazyFrame::try_from_pipeline(input, call.head)?;
|
let lazy = NuLazyFrame::try_from_pipeline(input, call.head)?;
|
||||||
|
|
|
@ -195,20 +195,20 @@ impl Command for LazyJoin {
|
||||||
|
|
||||||
if left_on.len() != right_on.len() {
|
if left_on.len() != right_on.len() {
|
||||||
let right_on: Value = call.req(engine_state, stack, 2)?;
|
let right_on: Value = call.req(engine_state, stack, 2)?;
|
||||||
return Err(ShellError::IncompatibleParametersSingle(
|
return Err(ShellError::IncompatibleParametersSingle {
|
||||||
"The right column list has a different size to the left column list".into(),
|
msg: "The right column list has a different size to the left column list".into(),
|
||||||
right_on.span()?,
|
span: right_on.span()?,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checking that both list of expressions are made out of col expressions or strings
|
// Checking that both list of expressions are made out of col expressions or strings
|
||||||
for (index, list) in &[(1usize, &left_on), (2, &left_on)] {
|
for (index, list) in &[(1usize, &left_on), (2, &left_on)] {
|
||||||
if list.iter().any(|expr| !matches!(expr, Expr::Column(..))) {
|
if list.iter().any(|expr| !matches!(expr, Expr::Column(..))) {
|
||||||
let value: Value = call.req(engine_state, stack, *index)?;
|
let value: Value = call.req(engine_state, stack, *index)?;
|
||||||
return Err(ShellError::IncompatibleParametersSingle(
|
return Err(ShellError::IncompatibleParametersSingle {
|
||||||
"Expected only a string, col expressions or list of strings".into(),
|
msg: "Expected only a string, col expressions or list of strings".into(),
|
||||||
value.span()?,
|
span: value.span()?,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,9 +7,9 @@ pub fn extract_strings(value: Value) -> Result<Vec<String>, ShellError> {
|
||||||
) {
|
) {
|
||||||
(Ok(col), Err(_)) => Ok(vec![col]),
|
(Ok(col), Err(_)) => Ok(vec![col]),
|
||||||
(Err(_), Ok(cols)) => Ok(cols),
|
(Err(_), Ok(cols)) => Ok(cols),
|
||||||
_ => Err(ShellError::IncompatibleParametersSingle(
|
_ => Err(ShellError::IncompatibleParametersSingle {
|
||||||
"Expected a string or list of strings".into(),
|
msg: "Expected a string or list of strings".into(),
|
||||||
value.span()?,
|
span: value.span()?,
|
||||||
)),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -128,13 +128,13 @@ pub(super) fn compute_between_series(
|
||||||
)),
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => Err(ShellError::IncompatibleParametersSingle(
|
_ => Err(ShellError::IncompatibleParametersSingle {
|
||||||
format!(
|
msg: format!(
|
||||||
"Operation {} can only be done with boolean values",
|
"Operation {} can only be done with boolean values",
|
||||||
operator.item
|
operator.item
|
||||||
),
|
),
|
||||||
operation_span,
|
span: operation_span,
|
||||||
)),
|
}),
|
||||||
},
|
},
|
||||||
Operator::Boolean(Boolean::Or) => match lhs.dtype() {
|
Operator::Boolean(Boolean::Or) => match lhs.dtype() {
|
||||||
DataType::Boolean => {
|
DataType::Boolean => {
|
||||||
|
@ -157,13 +157,13 @@ pub(super) fn compute_between_series(
|
||||||
)),
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => Err(ShellError::IncompatibleParametersSingle(
|
_ => Err(ShellError::IncompatibleParametersSingle {
|
||||||
format!(
|
msg: format!(
|
||||||
"Operation {} can only be done with boolean values",
|
"Operation {} can only be done with boolean values",
|
||||||
operator.item
|
operator.item
|
||||||
),
|
),
|
||||||
operation_span,
|
span: operation_span,
|
||||||
)),
|
}),
|
||||||
},
|
},
|
||||||
_ => Err(ShellError::OperatorMismatch {
|
_ => Err(ShellError::OperatorMismatch {
|
||||||
op_span: operator.span,
|
op_span: operator.span,
|
||||||
|
|
|
@ -151,10 +151,10 @@ impl NuDataFrame {
|
||||||
}
|
}
|
||||||
Axis::Column => {
|
Axis::Column => {
|
||||||
if self.df.width() != other.df.width() {
|
if self.df.width() != other.df.width() {
|
||||||
return Err(ShellError::IncompatibleParametersSingle(
|
return Err(ShellError::IncompatibleParametersSingle {
|
||||||
"Dataframes with different number of columns".into(),
|
msg: "Dataframes with different number of columns".into(),
|
||||||
span,
|
span,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if !self
|
if !self
|
||||||
|
@ -163,10 +163,10 @@ impl NuDataFrame {
|
||||||
.iter()
|
.iter()
|
||||||
.all(|col| other.df.get_column_names().contains(col))
|
.all(|col| other.df.get_column_names().contains(col))
|
||||||
{
|
{
|
||||||
return Err(ShellError::IncompatibleParametersSingle(
|
return Err(ShellError::IncompatibleParametersSingle {
|
||||||
"Dataframes with different columns names".into(),
|
msg: "Dataframes with different columns names".into(),
|
||||||
span,
|
span,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let new_cols = self
|
let new_cols = self
|
||||||
|
|
|
@ -76,10 +76,10 @@ fn compute_with_value(
|
||||||
polars::prelude::Expr::Literal(..) => {
|
polars::prelude::Expr::Literal(..) => {
|
||||||
with_operator(operator, left, rhs, lhs_span, right.span()?, op)
|
with_operator(operator, left, rhs, lhs_span, right.span()?, op)
|
||||||
}
|
}
|
||||||
_ => Err(ShellError::TypeMismatch(
|
_ => Err(ShellError::TypeMismatch {
|
||||||
"Only literal expressions or number".into(),
|
err_message: "Only literal expressions or number".into(),
|
||||||
right.span()?,
|
span: right.span()?,
|
||||||
)),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
|
|
@ -132,7 +132,10 @@ where
|
||||||
span,
|
span,
|
||||||
},
|
},
|
||||||
Err(_) => Value::Error {
|
Err(_) => Value::Error {
|
||||||
error: ShellError::TypeMismatch("invalid format".to_string(), span),
|
error: ShellError::TypeMismatch {
|
||||||
|
err_message: "invalid format".to_string(),
|
||||||
|
span,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -137,7 +137,10 @@ fn _to_timezone(dt: DateTime<FixedOffset>, timezone: &Spanned<String>, span: Spa
|
||||||
match datetime_in_timezone(&dt, timezone.item.as_str()) {
|
match datetime_in_timezone(&dt, timezone.item.as_str()) {
|
||||||
Ok(dt) => Value::Date { val: dt, span },
|
Ok(dt) => Value::Date { val: dt, span },
|
||||||
Err(_) => Value::Error {
|
Err(_) => Value::Error {
|
||||||
error: ShellError::TypeMismatch(String::from("invalid time zone"), timezone.span),
|
error: ShellError::TypeMismatch {
|
||||||
|
err_message: String::from("invalid time zone"),
|
||||||
|
span: timezone.span,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,10 +55,10 @@ impl Command for Mkdir {
|
||||||
let mut stream: VecDeque<Value> = VecDeque::new();
|
let mut stream: VecDeque<Value> = VecDeque::new();
|
||||||
|
|
||||||
if directories.peek().is_none() {
|
if directories.peek().is_none() {
|
||||||
return Err(ShellError::MissingParameter(
|
return Err(ShellError::MissingParameter {
|
||||||
"requires directory paths".to_string(),
|
param_name: "requires directory paths".to_string(),
|
||||||
call.head,
|
span: call.head,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i, dir) in directories.enumerate() {
|
for (i, dir) in directories.enumerate() {
|
||||||
|
|
|
@ -71,17 +71,17 @@ impl Command for Open {
|
||||||
// Collect a filename from the input
|
// Collect a filename from the input
|
||||||
match input {
|
match input {
|
||||||
PipelineData::Value(Value::Nothing { .. }, ..) => {
|
PipelineData::Value(Value::Nothing { .. }, ..) => {
|
||||||
return Err(ShellError::MissingParameter(
|
return Err(ShellError::MissingParameter {
|
||||||
"needs filename".to_string(),
|
param_name: "needs filename".to_string(),
|
||||||
call.head,
|
span: call.head,
|
||||||
))
|
})
|
||||||
}
|
}
|
||||||
PipelineData::Value(val, ..) => val.as_spanned_string()?,
|
PipelineData::Value(val, ..) => val.as_spanned_string()?,
|
||||||
_ => {
|
_ => {
|
||||||
return Err(ShellError::MissingParameter(
|
return Err(ShellError::MissingParameter {
|
||||||
"needs filename".to_string(),
|
param_name: "needs filename".to_string(),
|
||||||
call.head,
|
span: call.head,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -94,10 +94,10 @@ impl Command for Touch {
|
||||||
Some(reference) => {
|
Some(reference) => {
|
||||||
let reference_path = Path::new(&reference.item);
|
let reference_path = Path::new(&reference.item);
|
||||||
if !reference_path.exists() {
|
if !reference_path.exists() {
|
||||||
return Err(ShellError::TypeMismatch(
|
return Err(ShellError::TypeMismatch {
|
||||||
"path provided is invalid".to_string(),
|
err_message: "path provided is invalid".to_string(),
|
||||||
reference.span,
|
span: reference.span,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
date = Some(
|
date = Some(
|
||||||
|
@ -119,10 +119,10 @@ impl Command for Touch {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
return Err(ShellError::MissingParameter(
|
return Err(ShellError::MissingParameter {
|
||||||
"reference".to_string(),
|
param_name: "reference".to_string(),
|
||||||
call.head,
|
span: call.head,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -95,10 +95,10 @@ impl Command for Watch {
|
||||||
match nu_glob::Pattern::new(&absolute_path.to_string_lossy()) {
|
match nu_glob::Pattern::new(&absolute_path.to_string_lossy()) {
|
||||||
Ok(pattern) => Some(pattern),
|
Ok(pattern) => Some(pattern),
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
return Err(ShellError::TypeMismatch(
|
return Err(ShellError::TypeMismatch {
|
||||||
"Glob pattern is invalid".to_string(),
|
err_message: "Glob pattern is invalid".to_string(),
|
||||||
glob.span,
|
span: glob.span,
|
||||||
))
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -124,19 +124,20 @@ impl Command for DropNth {
|
||||||
// check for negative range inputs, e.g., (2..-5)
|
// check for negative range inputs, e.g., (2..-5)
|
||||||
if from.is_negative() || to.is_negative() {
|
if from.is_negative() || to.is_negative() {
|
||||||
let span: Spanned<Range> = call.req(engine_state, stack, 0)?;
|
let span: Spanned<Range> = call.req(engine_state, stack, 0)?;
|
||||||
return Err(ShellError::TypeMismatch(
|
return Err(ShellError::TypeMismatch {
|
||||||
"drop nth accepts only positive integers".to_string(),
|
err_message: "drop nth accepts only positive integers".to_string(),
|
||||||
span.span,
|
span: span.span,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
// check if the upper bound is smaller than the lower bound, e.g., do not accept 4..2
|
// check if the upper bound is smaller than the lower bound, e.g., do not accept 4..2
|
||||||
if to < from {
|
if to < from {
|
||||||
let span: Spanned<Range> = call.req(engine_state, stack, 0)?;
|
let span: Spanned<Range> = call.req(engine_state, stack, 0)?;
|
||||||
return Err(ShellError::TypeMismatch(
|
return Err(ShellError::TypeMismatch {
|
||||||
|
err_message:
|
||||||
"The upper bound needs to be equal or larger to the lower bound"
|
"The upper bound needs to be equal or larger to the lower bound"
|
||||||
.to_string(),
|
.to_string(),
|
||||||
span.span,
|
span: span.span,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// check for equality to isize::MAX because for some reason,
|
// check for equality to isize::MAX because for some reason,
|
||||||
|
@ -191,11 +192,11 @@ fn extract_int_or_range(
|
||||||
|
|
||||||
let range_opt = range_opt.map(Either::Right).ok();
|
let range_opt = range_opt.map(Either::Right).ok();
|
||||||
|
|
||||||
int_opt.or(range_opt).ok_or_else(|| {
|
int_opt
|
||||||
ShellError::TypeMismatch(
|
.or(range_opt)
|
||||||
"int or range".into(),
|
.ok_or_else(|| ShellError::TypeMismatch {
|
||||||
value.span().unwrap_or_else(|_| Span::new(0, 0)),
|
err_message: "int or range".into(),
|
||||||
)
|
span: value.span().unwrap_or_else(|_| Span::new(0, 0)),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -185,8 +185,10 @@ fn find_with_regex(
|
||||||
|
|
||||||
let regex = flags.to_string() + regex.as_str();
|
let regex = flags.to_string() + regex.as_str();
|
||||||
|
|
||||||
let re = Regex::new(regex.as_str())
|
let re = Regex::new(regex.as_str()).map_err(|e| ShellError::TypeMismatch {
|
||||||
.map_err(|e| ShellError::TypeMismatch(format!("invalid regex: {e}"), span))?;
|
err_message: format!("invalid regex: {e}"),
|
||||||
|
span,
|
||||||
|
})?;
|
||||||
|
|
||||||
input.filter(
|
input.filter(
|
||||||
move |value| match value {
|
move |value| match value {
|
||||||
|
|
|
@ -115,10 +115,10 @@ fn replace_headers(value: Value, headers: &[String]) -> Result<Value, ShellError
|
||||||
|
|
||||||
Ok(Value::List { vals, span })
|
Ok(Value::List { vals, span })
|
||||||
}
|
}
|
||||||
_ => Err(ShellError::TypeMismatch(
|
_ => Err(ShellError::TypeMismatch {
|
||||||
"record".to_string(),
|
err_message: "record".to_string(),
|
||||||
value.span()?,
|
span: value.span()?,
|
||||||
)),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,10 +138,11 @@ fn extract_headers(value: &Value, config: &Config) -> Result<Vec<String>, ShellE
|
||||||
Value::Record { vals, .. } => {
|
Value::Record { vals, .. } => {
|
||||||
for v in vals {
|
for v in vals {
|
||||||
if !is_valid_header(v) {
|
if !is_valid_header(v) {
|
||||||
return Err(ShellError::TypeMismatch(
|
return Err(ShellError::TypeMismatch {
|
||||||
"needs compatible type: Null, String, Bool, Float, Int".to_string(),
|
err_message: "needs compatible type: Null, String, Bool, Float, Int"
|
||||||
v.span()?,
|
.to_string(),
|
||||||
));
|
span: v.span()?,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -171,10 +172,10 @@ fn extract_headers(value: &Value, config: &Config) -> Result<Vec<String>, ShellE
|
||||||
Vec::new(),
|
Vec::new(),
|
||||||
)
|
)
|
||||||
})?,
|
})?,
|
||||||
_ => Err(ShellError::TypeMismatch(
|
_ => Err(ShellError::TypeMismatch {
|
||||||
"record".to_string(),
|
err_message: "record".to_string(),
|
||||||
value.span()?,
|
span: value.span()?,
|
||||||
)),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -108,11 +108,8 @@ fn rename(
|
||||||
}) = call.get_flag(engine_state, stack, "column")?
|
}) = call.get_flag(engine_state, stack, "column")?
|
||||||
{
|
{
|
||||||
if columns.is_empty() {
|
if columns.is_empty() {
|
||||||
return Err(ShellError::TypeMismatch(
|
return Err(ShellError::TypeMismatch { err_message: "The column list cannot be empty and must contain only two values: the column's name and its replacement value"
|
||||||
"The column list cannot be empty and must contain only two values: the column's name and its replacement value"
|
.to_string(), span: column_span });
|
||||||
.to_string(),
|
|
||||||
column_span,
|
|
||||||
));
|
|
||||||
} else {
|
} else {
|
||||||
(Some(columns[0].span()?), column_span)
|
(Some(columns[0].span()?), column_span)
|
||||||
}
|
}
|
||||||
|
@ -122,11 +119,8 @@ fn rename(
|
||||||
|
|
||||||
if let Some(ref cols) = specified_column {
|
if let Some(ref cols) = specified_column {
|
||||||
if cols.len() != 2 {
|
if cols.len() != 2 {
|
||||||
return Err(ShellError::TypeMismatch(
|
return Err(ShellError::TypeMismatch { err_message: "The column list must contain only two values: the column's name and its replacement value"
|
||||||
"The column list must contain only two values: the column's name and its replacement value"
|
.to_string(), span: list_span });
|
||||||
.to_string(),
|
|
||||||
list_span,
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -36,7 +36,10 @@ fn vertical_rotate_value(
|
||||||
span,
|
span,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
_ => Err(ShellError::TypeMismatch("list".to_string(), value.span()?)),
|
_ => Err(ShellError::TypeMismatch {
|
||||||
|
err_message: "list".to_string(),
|
||||||
|
span: value.span()?,
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,9 +96,9 @@ fn horizontal_rotate_value(
|
||||||
|
|
||||||
Ok(Value::List { vals: values, span })
|
Ok(Value::List { vals: values, span })
|
||||||
}
|
}
|
||||||
_ => Err(ShellError::TypeMismatch(
|
_ => Err(ShellError::TypeMismatch {
|
||||||
"record".to_string(),
|
err_message: "record".to_string(),
|
||||||
value.span()?,
|
span: value.span()?,
|
||||||
)),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,13 +78,18 @@ impl Command for Skip {
|
||||||
let metadata = input.metadata();
|
let metadata = input.metadata();
|
||||||
|
|
||||||
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 }) => {
|
||||||
ShellError::TypeMismatch(
|
val.try_into().map_err(|err| ShellError::TypeMismatch {
|
||||||
format!("Could not convert {val} to unsigned integer: {err}"),
|
err_message: format!("Could not convert {val} to unsigned integer: {err}"),
|
||||||
span,
|
span,
|
||||||
)
|
})?
|
||||||
})?,
|
}
|
||||||
Some(_) => return Err(ShellError::TypeMismatch("expected integer".into(), span)),
|
Some(_) => {
|
||||||
|
return Err(ShellError::TypeMismatch {
|
||||||
|
err_message: "expected integer".into(),
|
||||||
|
span,
|
||||||
|
})
|
||||||
|
}
|
||||||
None => 1,
|
None => 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -87,7 +87,10 @@ impl Command for SortBy {
|
||||||
let mut vec: Vec<_> = input.into_iter_strict(call.head)?.collect();
|
let mut vec: Vec<_> = input.into_iter_strict(call.head)?.collect();
|
||||||
|
|
||||||
if columns.is_empty() {
|
if columns.is_empty() {
|
||||||
return Err(ShellError::MissingParameter("columns".into(), call.head));
|
return Err(ShellError::MissingParameter {
|
||||||
|
param_name: "columns".into(),
|
||||||
|
span: call.head,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
crate::sort(&mut vec, columns, call.head, insensitive, natural)?;
|
crate::sort(&mut vec, columns, call.head, insensitive, natural)?;
|
||||||
|
|
|
@ -60,7 +60,10 @@ impl Command for UniqBy {
|
||||||
let columns: Vec<String> = call.rest(engine_state, stack, 0)?;
|
let columns: Vec<String> = call.rest(engine_state, stack, 0)?;
|
||||||
|
|
||||||
if columns.is_empty() {
|
if columns.is_empty() {
|
||||||
return Err(ShellError::MissingParameter("columns".into(), call.head));
|
return Err(ShellError::MissingParameter {
|
||||||
|
param_name: "columns".into(),
|
||||||
|
span: call.head,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let metadata = input.metadata();
|
let metadata = input.metadata();
|
||||||
|
|
|
@ -124,10 +124,10 @@ fn from_csv(
|
||||||
} else {
|
} else {
|
||||||
let vec_s: Vec<char> = s.chars().collect();
|
let vec_s: Vec<char> = s.chars().collect();
|
||||||
if vec_s.len() != 1 {
|
if vec_s.len() != 1 {
|
||||||
return Err(ShellError::MissingParameter(
|
return Err(ShellError::MissingParameter {
|
||||||
"single character separator".into(),
|
param_name: "single character separator".into(),
|
||||||
span,
|
span,
|
||||||
));
|
});
|
||||||
};
|
};
|
||||||
vec_s[0]
|
vec_s[0]
|
||||||
}
|
}
|
||||||
|
|
|
@ -68,7 +68,10 @@ pub fn from_delimited_data(
|
||||||
|
|
||||||
Ok(
|
Ok(
|
||||||
from_delimited_string_to_value(concat_string, noheaders, no_infer, sep, trim, name)
|
from_delimited_string_to_value(concat_string, noheaders, no_infer, sep, trim, name)
|
||||||
.map_err(|x| ShellError::DelimiterError(x.to_string(), name))?
|
.map_err(|x| ShellError::DelimiterError {
|
||||||
|
msg: x.to_string(),
|
||||||
|
span: name,
|
||||||
|
})?
|
||||||
.into_pipeline_data_with_metadata(metadata),
|
.into_pipeline_data_with_metadata(metadata),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -80,11 +83,12 @@ pub fn trim_from_str(trim: Option<Value>) -> Result<Trim, ShellError> {
|
||||||
"headers" => Ok(Trim::Headers),
|
"headers" => Ok(Trim::Headers),
|
||||||
"fields" => Ok(Trim::Fields),
|
"fields" => Ok(Trim::Fields),
|
||||||
"none" => Ok(Trim::None),
|
"none" => Ok(Trim::None),
|
||||||
_ => Err(ShellError::TypeMismatch(
|
_ => Err(ShellError::TypeMismatch {
|
||||||
|
err_message:
|
||||||
"the only possible values for trim are 'all', 'headers', 'fields' and 'none'"
|
"the only possible values for trim are 'all', 'headers', 'fields' and 'none'"
|
||||||
.into(),
|
.into(),
|
||||||
span,
|
span,
|
||||||
)),
|
}),
|
||||||
},
|
},
|
||||||
_ => Ok(Trim::None),
|
_ => Ok(Trim::None),
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,10 +74,10 @@ fn convert_columns(columns: &[Value], span: Span) -> Result<Vec<String>, ShellEr
|
||||||
.iter()
|
.iter()
|
||||||
.map(|value| match &value {
|
.map(|value| match &value {
|
||||||
Value::String { val: s, .. } => Ok(s.clone()),
|
Value::String { val: s, .. } => Ok(s.clone()),
|
||||||
_ => Err(ShellError::IncompatibleParametersSingle(
|
_ => Err(ShellError::IncompatibleParametersSingle {
|
||||||
"Incorrect column format, Only string as column name".to_string(),
|
msg: "Incorrect column format, Only string as column name".to_string(),
|
||||||
value.span().unwrap_or(span),
|
span: value.span().unwrap_or(span),
|
||||||
)),
|
}),
|
||||||
})
|
})
|
||||||
.collect::<Result<Vec<String>, _>>()?;
|
.collect::<Result<Vec<String>, _>>()?;
|
||||||
|
|
||||||
|
|
|
@ -74,10 +74,10 @@ fn convert_columns(columns: &[Value], span: Span) -> Result<Vec<String>, ShellEr
|
||||||
.iter()
|
.iter()
|
||||||
.map(|value| match &value {
|
.map(|value| match &value {
|
||||||
Value::String { val: s, .. } => Ok(s.clone()),
|
Value::String { val: s, .. } => Ok(s.clone()),
|
||||||
_ => Err(ShellError::IncompatibleParametersSingle(
|
_ => Err(ShellError::IncompatibleParametersSingle {
|
||||||
"Incorrect column format, Only string as column name".to_string(),
|
msg: "Incorrect column format, Only string as column name".to_string(),
|
||||||
value.span().unwrap_or(span),
|
span: value.span().unwrap_or(span),
|
||||||
)),
|
}),
|
||||||
})
|
})
|
||||||
.collect::<Result<Vec<String>, _>>()?;
|
.collect::<Result<Vec<String>, _>>()?;
|
||||||
|
|
||||||
|
|
|
@ -88,10 +88,11 @@ fn to_csv(
|
||||||
} else {
|
} else {
|
||||||
let vec_s: Vec<char> = s.chars().collect();
|
let vec_s: Vec<char> = s.chars().collect();
|
||||||
if vec_s.len() != 1 {
|
if vec_s.len() != 1 {
|
||||||
return Err(ShellError::TypeMismatch(
|
return Err(ShellError::TypeMismatch {
|
||||||
"Expected a single separator char from --separator".to_string(),
|
err_message: "Expected a single separator char from --separator"
|
||||||
|
.to_string(),
|
||||||
span,
|
span,
|
||||||
));
|
});
|
||||||
};
|
};
|
||||||
vec_s[0]
|
vec_s[0]
|
||||||
}
|
}
|
||||||
|
|
|
@ -142,7 +142,10 @@ pub fn cal(
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_invalid_year_shell_error(head: Span) -> ShellError {
|
fn get_invalid_year_shell_error(head: Span) -> ShellError {
|
||||||
ShellError::TypeMismatch("The year is invalid".to_string(), head)
|
ShellError::TypeMismatch {
|
||||||
|
err_message: "The year is invalid".to_string(),
|
||||||
|
span: head,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct MonthHelper {
|
struct MonthHelper {
|
||||||
|
@ -251,10 +254,10 @@ fn add_month_to_table(
|
||||||
Err(()) => match full_year_value {
|
Err(()) => match full_year_value {
|
||||||
Some(x) => return Err(get_invalid_year_shell_error(x.span)),
|
Some(x) => return Err(get_invalid_year_shell_error(x.span)),
|
||||||
None => {
|
None => {
|
||||||
return Err(ShellError::UnknownOperator(
|
return Err(ShellError::UnknownOperator {
|
||||||
"Issue parsing command, invalid command".to_string(),
|
op_token: "Issue parsing command, invalid command".to_string(),
|
||||||
tag,
|
span: tag,
|
||||||
))
|
})
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -275,10 +278,10 @@ fn add_month_to_table(
|
||||||
if days_of_the_week.contains(&s.as_str()) {
|
if days_of_the_week.contains(&s.as_str()) {
|
||||||
week_start_day = s.to_string();
|
week_start_day = s.to_string();
|
||||||
} else {
|
} else {
|
||||||
return Err(ShellError::TypeMismatch(
|
return Err(ShellError::TypeMismatch {
|
||||||
"The specified week start day is invalid".to_string(),
|
err_message: "The specified week start day is invalid".to_string(),
|
||||||
day.span,
|
span: day.span,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -210,11 +210,10 @@ pub fn request_set_timeout(
|
||||||
if let Some(timeout) = timeout {
|
if let Some(timeout) = timeout {
|
||||||
let val = timeout.as_i64()?;
|
let val = timeout.as_i64()?;
|
||||||
if val.is_negative() || val < 1 {
|
if val.is_negative() || val < 1 {
|
||||||
return Err(ShellError::TypeMismatch(
|
return Err(ShellError::TypeMismatch {
|
||||||
"Timeout value must be an integer and larger than 0".to_string(),
|
err_message: "Timeout value must be an integer and larger than 0".to_string(),
|
||||||
// timeout is already guaranteed to not be an error
|
span: timeout.expect_span(),
|
||||||
timeout.expect_span(),
|
});
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
request = request.timeout(Duration::from_secs(val as u64));
|
request = request.timeout(Duration::from_secs(val as u64));
|
||||||
|
|
|
@ -151,10 +151,12 @@ impl UrlComponents {
|
||||||
port: Some(p),
|
port: Some(p),
|
||||||
..self
|
..self
|
||||||
}),
|
}),
|
||||||
Err(_) => Err(ShellError::IncompatibleParametersSingle(
|
Err(_) => Err(ShellError::IncompatibleParametersSingle {
|
||||||
String::from("Port parameter should represent an unsigned integer"),
|
msg: String::from(
|
||||||
|
"Port parameter should represent an unsigned integer",
|
||||||
|
),
|
||||||
span,
|
span,
|
||||||
)),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -163,12 +165,12 @@ impl UrlComponents {
|
||||||
..self
|
..self
|
||||||
}),
|
}),
|
||||||
Value::Error { error } => Err(error),
|
Value::Error { error } => Err(error),
|
||||||
other => Err(ShellError::IncompatibleParametersSingle(
|
other => Err(ShellError::IncompatibleParametersSingle {
|
||||||
String::from(
|
msg: String::from(
|
||||||
"Port parameter should be an unsigned integer or a string representing it",
|
"Port parameter should be an unsigned integer or a string representing it",
|
||||||
),
|
),
|
||||||
other.expect_span(),
|
span: other.expect_span(),
|
||||||
)),
|
}),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -210,10 +212,10 @@ impl UrlComponents {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
Value::Error { error } => Err(error),
|
Value::Error { error } => Err(error),
|
||||||
other => Err(ShellError::IncompatibleParametersSingle(
|
other => Err(ShellError::IncompatibleParametersSingle {
|
||||||
String::from("Key params has to be a record"),
|
msg: String::from("Key params has to be a record"),
|
||||||
other.expect_span(),
|
span: other.expect_span(),
|
||||||
)),
|
}),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -324,7 +326,10 @@ impl UrlComponents {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_shell_error_for_missing_parameter(pname: String, span: Span) -> ShellError {
|
fn generate_shell_error_for_missing_parameter(pname: String, span: Span) -> ShellError {
|
||||||
ShellError::MissingParameter(pname, span)
|
ShellError::MissingParameter {
|
||||||
|
param_name: pname,
|
||||||
|
span,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -641,7 +641,12 @@ Format: #
|
||||||
// this record is defined in nu-color-config crate
|
// this record is defined in nu-color-config crate
|
||||||
let code: Value = match call.opt(engine_state, stack, 0)? {
|
let code: Value = match call.opt(engine_state, stack, 0)? {
|
||||||
Some(c) => c,
|
Some(c) => c,
|
||||||
None => return Err(ShellError::MissingParameter("code".into(), call.head)),
|
None => {
|
||||||
|
return Err(ShellError::MissingParameter {
|
||||||
|
param_name: "code".into(),
|
||||||
|
span: call.head,
|
||||||
|
})
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let param_is_string = matches!(code, Value::String { val: _, span: _ });
|
let param_is_string = matches!(code, Value::String { val: _, span: _ });
|
||||||
|
@ -672,12 +677,13 @@ Format: #
|
||||||
if (escape || osc) && (param_is_valid_string) {
|
if (escape || osc) && (param_is_valid_string) {
|
||||||
let code_vec: Vec<char> = code_string.chars().collect();
|
let code_vec: Vec<char> = code_string.chars().collect();
|
||||||
if code_vec[0] == '\\' {
|
if code_vec[0] == '\\' {
|
||||||
return Err(ShellError::TypeMismatch(
|
return Err(ShellError::TypeMismatch {
|
||||||
"no need for escape characters".into(),
|
err_message: "no need for escape characters".into(),
|
||||||
call.get_flag_expr("escape")
|
span: call
|
||||||
|
.get_flag_expr("escape")
|
||||||
.expect("Unexpected missing argument")
|
.expect("Unexpected missing argument")
|
||||||
.span,
|
.span,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -711,12 +717,13 @@ Format: #
|
||||||
match str_to_ansi(&code_string) {
|
match str_to_ansi(&code_string) {
|
||||||
Some(c) => c,
|
Some(c) => c,
|
||||||
None => {
|
None => {
|
||||||
return Err(ShellError::TypeMismatch(
|
return Err(ShellError::TypeMismatch {
|
||||||
String::from("Unknown ansi code"),
|
err_message: String::from("Unknown ansi code"),
|
||||||
call.positional_nth(0)
|
span: call
|
||||||
|
.positional_nth(0)
|
||||||
.expect("Unexpected missing argument")
|
.expect("Unexpected missing argument")
|
||||||
.span,
|
.span,
|
||||||
))
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -737,10 +744,10 @@ Format: #
|
||||||
"bg" => nu_style.bg = Some(v.as_string()?),
|
"bg" => nu_style.bg = Some(v.as_string()?),
|
||||||
"attr" => nu_style.attr = Some(v.as_string()?),
|
"attr" => nu_style.attr = Some(v.as_string()?),
|
||||||
_ => {
|
_ => {
|
||||||
return Err(ShellError::IncompatibleParametersSingle(
|
return Err(ShellError::IncompatibleParametersSingle {
|
||||||
format!("problem with key: {k}"),
|
msg: format!("problem with key: {k}"),
|
||||||
code.expect_span(),
|
span: code.expect_span(),
|
||||||
))
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -158,10 +158,11 @@ fn action(
|
||||||
(None, None, None, None) => {
|
(None, None, None, None) => {
|
||||||
// Error - no colors
|
// Error - no colors
|
||||||
Value::Error {
|
Value::Error {
|
||||||
error: ShellError::MissingParameter(
|
error: ShellError::MissingParameter {
|
||||||
|
param_name:
|
||||||
"please supply foreground and/or background color parameters".into(),
|
"please supply foreground and/or background color parameters".into(),
|
||||||
*command_span,
|
span: *command_span,
|
||||||
),
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(None, None, None, Some(bg_end)) => {
|
(None, None, None, Some(bg_end)) => {
|
||||||
|
@ -285,7 +286,10 @@ fn action(
|
||||||
let got = format!("value is {}, not string", other.get_type());
|
let got = format!("value is {}, not string", other.get_type());
|
||||||
|
|
||||||
Value::Error {
|
Value::Error {
|
||||||
error: ShellError::TypeMismatch(got, other.span().unwrap_or(*command_span)),
|
error: ShellError::TypeMismatch {
|
||||||
|
err_message: got,
|
||||||
|
span: other.span().unwrap_or(*command_span),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -139,7 +139,10 @@ fn process_value(value: &Value, text: &Option<String>, command_span: &Span) -> V
|
||||||
let got = format!("value is {}, not string", other.get_type());
|
let got = format!("value is {}, not string", other.get_type());
|
||||||
|
|
||||||
Value::Error {
|
Value::Error {
|
||||||
error: ShellError::TypeMismatch(got, other.span().unwrap_or(*command_span)),
|
error: ShellError::TypeMismatch {
|
||||||
|
err_message: got,
|
||||||
|
span: other.span().unwrap_or(*command_span),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,7 +58,10 @@ fn action(input: &Value, _args: &CellPathOnlyArgs, command_span: Span) -> Value
|
||||||
let got = format!("value is {}, not string", other.get_type());
|
let got = format!("value is {}, not string", other.get_type());
|
||||||
|
|
||||||
Value::Error {
|
Value::Error {
|
||||||
error: ShellError::TypeMismatch(got, other.span().unwrap_or(command_span)),
|
error: ShellError::TypeMismatch {
|
||||||
|
err_message: got,
|
||||||
|
span: other.span().unwrap_or(command_span),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -246,10 +246,10 @@ impl Command for Char {
|
||||||
if call.has_flag("integer") {
|
if call.has_flag("integer") {
|
||||||
let args: Vec<i64> = call.rest(engine_state, stack, 0)?;
|
let args: Vec<i64> = call.rest(engine_state, stack, 0)?;
|
||||||
if args.is_empty() {
|
if args.is_empty() {
|
||||||
return Err(ShellError::MissingParameter(
|
return Err(ShellError::MissingParameter {
|
||||||
"missing at least one unicode character".into(),
|
param_name: "missing at least one unicode character".into(),
|
||||||
call_span,
|
span: call_span,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
let mut multi_byte = String::new();
|
let mut multi_byte = String::new();
|
||||||
for (i, &arg) in args.iter().enumerate() {
|
for (i, &arg) in args.iter().enumerate() {
|
||||||
|
@ -263,10 +263,10 @@ impl Command for Char {
|
||||||
} else if call.has_flag("unicode") {
|
} else if call.has_flag("unicode") {
|
||||||
let args: Vec<String> = call.rest(engine_state, stack, 0)?;
|
let args: Vec<String> = call.rest(engine_state, stack, 0)?;
|
||||||
if args.is_empty() {
|
if args.is_empty() {
|
||||||
return Err(ShellError::MissingParameter(
|
return Err(ShellError::MissingParameter {
|
||||||
"missing at least one unicode character".into(),
|
param_name: "missing at least one unicode character".into(),
|
||||||
call_span,
|
span: call_span,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
let mut multi_byte = String::new();
|
let mut multi_byte = String::new();
|
||||||
for (i, arg) in args.iter().enumerate() {
|
for (i, arg) in args.iter().enumerate() {
|
||||||
|
@ -280,21 +280,22 @@ impl Command for Char {
|
||||||
} else {
|
} else {
|
||||||
let args: Vec<String> = call.rest(engine_state, stack, 0)?;
|
let args: Vec<String> = call.rest(engine_state, stack, 0)?;
|
||||||
if args.is_empty() {
|
if args.is_empty() {
|
||||||
return Err(ShellError::MissingParameter(
|
return Err(ShellError::MissingParameter {
|
||||||
"missing name of the character".into(),
|
param_name: "missing name of the character".into(),
|
||||||
call_span,
|
span: call_span,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
let special_character = str_to_character(&args[0]);
|
let special_character = str_to_character(&args[0]);
|
||||||
if let Some(output) = special_character {
|
if let Some(output) = special_character {
|
||||||
Ok(Value::string(output, call_span).into_pipeline_data())
|
Ok(Value::string(output, call_span).into_pipeline_data())
|
||||||
} else {
|
} else {
|
||||||
Err(ShellError::TypeMismatch(
|
Err(ShellError::TypeMismatch {
|
||||||
"error finding named character".into(),
|
err_message: "error finding named character".into(),
|
||||||
call.positional_nth(0)
|
span: call
|
||||||
|
.positional_nth(0)
|
||||||
.expect("Unexpected missing argument")
|
.expect("Unexpected missing argument")
|
||||||
.span,
|
.span,
|
||||||
))
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -306,10 +307,10 @@ fn integer_to_unicode_char(value: i64, t: &Span) -> Result<char, ShellError> {
|
||||||
if let Some(ch) = decoded_char {
|
if let Some(ch) = decoded_char {
|
||||||
Ok(ch)
|
Ok(ch)
|
||||||
} else {
|
} else {
|
||||||
Err(ShellError::TypeMismatch(
|
Err(ShellError::TypeMismatch {
|
||||||
"not a valid Unicode codepoint".into(),
|
err_message: "not a valid Unicode codepoint".into(),
|
||||||
*t,
|
span: *t,
|
||||||
))
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -321,10 +322,10 @@ fn string_to_unicode_char(s: &str, t: &Span) -> Result<char, ShellError> {
|
||||||
if let Some(ch) = decoded_char {
|
if let Some(ch) = decoded_char {
|
||||||
Ok(ch)
|
Ok(ch)
|
||||||
} else {
|
} else {
|
||||||
Err(ShellError::TypeMismatch(
|
Err(ShellError::TypeMismatch {
|
||||||
"error decoding Unicode character".into(),
|
err_message: "error decoding Unicode character".into(),
|
||||||
*t,
|
span: *t,
|
||||||
))
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -187,10 +187,10 @@ fn action(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
other => Value::Error {
|
other => Value::Error {
|
||||||
error: ShellError::TypeMismatch(
|
error: ShellError::TypeMismatch {
|
||||||
format!("string or binary, not {}", other.get_type()),
|
err_message: format!("string or binary, not {}", other.get_type()),
|
||||||
other.span().unwrap_or(command_span),
|
span: other.span().unwrap_or(command_span),
|
||||||
),
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -157,10 +157,10 @@ fn extract_formatting_operations(
|
||||||
}
|
}
|
||||||
|
|
||||||
if column_span_end < column_span_start {
|
if column_span_end < column_span_start {
|
||||||
return Err(ShellError::DelimiterError(
|
return Err(ShellError::DelimiterError {
|
||||||
"there are unmatched curly braces".to_string(),
|
msg: "there are unmatched curly braces".to_string(),
|
||||||
error_span,
|
span: error_span,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if !column_name.is_empty() {
|
if !column_name.is_empty() {
|
||||||
|
@ -301,10 +301,10 @@ fn format_record(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(err) => {
|
Some(err) => {
|
||||||
return Err(ShellError::TypeMismatch(
|
return Err(ShellError::TypeMismatch {
|
||||||
format!("expression is invalid, detail message: {err:?}"),
|
err_message: format!("expression is invalid, detail message: {err:?}"),
|
||||||
*span,
|
span: *span,
|
||||||
))
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,16 +26,16 @@ pub fn grapheme_flags(call: &Call) -> Result<bool, ShellError> {
|
||||||
// Note that Nushell already prevents nonexistent flags from being used with commands,
|
// Note that Nushell already prevents nonexistent flags from being used with commands,
|
||||||
// so this function can be reused for both the --utf-8-bytes commands and the --code-points commands.
|
// so this function can be reused for both the --utf-8-bytes commands and the --code-points commands.
|
||||||
if g_flag && call.has_flag("utf-8-bytes") {
|
if g_flag && call.has_flag("utf-8-bytes") {
|
||||||
Err(ShellError::IncompatibleParametersSingle(
|
Err(ShellError::IncompatibleParametersSingle {
|
||||||
"Incompatible flags: --grapheme-clusters (-g) and --utf-8-bytes (-b)".to_string(),
|
msg: "Incompatible flags: --grapheme-clusters (-g) and --utf-8-bytes (-b)".to_string(),
|
||||||
call.head,
|
span: call.head,
|
||||||
))?
|
})?
|
||||||
}
|
}
|
||||||
if g_flag && call.has_flag("code-points") {
|
if g_flag && call.has_flag("code-points") {
|
||||||
Err(ShellError::IncompatibleParametersSingle(
|
Err(ShellError::IncompatibleParametersSingle {
|
||||||
"Incompatible flags: --grapheme-clusters (-g) and --utf-8-bytes (-b)".to_string(),
|
msg: "Incompatible flags: --grapheme-clusters (-g) and --utf-8-bytes (-b)".to_string(),
|
||||||
call.head,
|
span: call.head,
|
||||||
))?
|
})?
|
||||||
}
|
}
|
||||||
// Grapheme cluster usage is decided by the non-default -g flag
|
// Grapheme cluster usage is decided by the non-default -g flag
|
||||||
Ok(g_flag)
|
Ok(g_flag)
|
||||||
|
|
|
@ -285,10 +285,10 @@ fn build_regex(input: &str, span: Span) -> Result<String, ShellError> {
|
||||||
column.push(c);
|
column.push(c);
|
||||||
|
|
||||||
if loop_input.peek().is_none() {
|
if loop_input.peek().is_none() {
|
||||||
return Err(ShellError::DelimiterError(
|
return Err(ShellError::DelimiterError {
|
||||||
"Found opening `{` without an associated closing `}`".to_owned(),
|
msg: "Found opening `{` without an associated closing `}`".to_owned(),
|
||||||
span,
|
span,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -119,16 +119,16 @@ fn split_words(
|
||||||
|
|
||||||
if matches!(word_length, None) {
|
if matches!(word_length, None) {
|
||||||
if call.has_flag("grapheme-clusters") {
|
if call.has_flag("grapheme-clusters") {
|
||||||
return Err(ShellError::IncompatibleParametersSingle(
|
return Err(ShellError::IncompatibleParametersSingle {
|
||||||
"--grapheme-clusters (-g) requires --min-word-length (-l)".to_string(),
|
msg: "--grapheme-clusters (-g) requires --min-word-length (-l)".to_string(),
|
||||||
span,
|
span,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
if call.has_flag("utf-8-bytes") {
|
if call.has_flag("utf-8-bytes") {
|
||||||
return Err(ShellError::IncompatibleParametersSingle(
|
return Err(ShellError::IncompatibleParametersSingle {
|
||||||
"--utf-8-bytes (-b) requires --min-word-length (-l)".to_string(),
|
msg: "--utf-8-bytes (-b) requires --min-word-length (-l)".to_string(),
|
||||||
span,
|
span,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let graphemes = grapheme_flags(call)?;
|
let graphemes = grapheme_flags(call)?;
|
||||||
|
|
|
@ -219,10 +219,10 @@ fn process_range(
|
||||||
}
|
}
|
||||||
Value::List { vals, .. } => {
|
Value::List { vals, .. } => {
|
||||||
if vals.len() > 2 {
|
if vals.len() > 2 {
|
||||||
Err(ShellError::TypeMismatch(
|
Err(ShellError::TypeMismatch {
|
||||||
String::from("there shouldn't be more than two indexes"),
|
err_message: String::from("there shouldn't be more than two indexes"),
|
||||||
head,
|
span: head,
|
||||||
))
|
})
|
||||||
} else {
|
} else {
|
||||||
let idx: Vec<String> = vals
|
let idx: Vec<String> = vals
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -248,18 +248,15 @@ fn process_range(
|
||||||
let end_index = r.1.parse::<i32>().unwrap_or(input_len as i32);
|
let end_index = r.1.parse::<i32>().unwrap_or(input_len as i32);
|
||||||
|
|
||||||
if start_index < 0 || start_index > end_index {
|
if start_index < 0 || start_index > end_index {
|
||||||
return Err(ShellError::TypeMismatch(
|
return Err(ShellError::TypeMismatch {
|
||||||
String::from("start index can't be negative or greater than end index"),
|
err_message: String::from("start index can't be negative or greater than end index"),
|
||||||
head,
|
span: head,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if end_index < 0 || end_index < start_index || end_index > input_len as i32 {
|
if end_index < 0 || end_index < start_index || end_index > input_len as i32 {
|
||||||
return Err(ShellError::TypeMismatch(
|
return Err(ShellError::TypeMismatch { err_message: String::from(
|
||||||
String::from(
|
"end index can't be negative, smaller than start index or greater than input length"), span: head });
|
||||||
"end index can't be negative, smaller than start index or greater than input length"),
|
|
||||||
head,
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
Ok(IndexOfOptionalBounds(start_index, end_index))
|
Ok(IndexOfOptionalBounds(start_index, end_index))
|
||||||
}
|
}
|
||||||
|
|
|
@ -209,7 +209,10 @@ fn action(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => Value::Error {
|
Err(e) => Value::Error {
|
||||||
error: ShellError::IncorrectValue(format!("Regex error: {e}"), find.span),
|
error: ShellError::IncorrectValue {
|
||||||
|
msg: format!("Regex error: {e}"),
|
||||||
|
span: find.span,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -155,10 +155,10 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
|
||||||
match start.cmp(&end) {
|
match start.cmp(&end) {
|
||||||
Ordering::Equal => Value::string("", head),
|
Ordering::Equal => Value::string("", head),
|
||||||
Ordering::Greater => Value::Error {
|
Ordering::Greater => Value::Error {
|
||||||
error: ShellError::TypeMismatch(
|
error: ShellError::TypeMismatch {
|
||||||
"End must be greater than or equal to Start".to_string(),
|
err_message: "End must be greater than or equal to Start".to_string(),
|
||||||
head,
|
span: head,
|
||||||
),
|
},
|
||||||
},
|
},
|
||||||
Ordering::Less => Value::String {
|
Ordering::Less => Value::String {
|
||||||
val: {
|
val: {
|
||||||
|
@ -220,10 +220,10 @@ fn process_arguments(range: &Value, head: Span) -> Result<(isize, isize), ShellE
|
||||||
}
|
}
|
||||||
Value::List { vals, .. } => {
|
Value::List { vals, .. } => {
|
||||||
if vals.len() > 2 {
|
if vals.len() > 2 {
|
||||||
Err(ShellError::TypeMismatch(
|
Err(ShellError::TypeMismatch {
|
||||||
"More than two indices given".to_string(),
|
err_message: "More than two indices given".to_string(),
|
||||||
head,
|
span: head,
|
||||||
))
|
})
|
||||||
} else {
|
} else {
|
||||||
let idx: Vec<String> = vals
|
let idx: Vec<String> = vals
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -231,11 +231,12 @@ fn process_arguments(range: &Value, head: Span) -> Result<(isize, isize), ShellE
|
||||||
match v {
|
match v {
|
||||||
Value::Int { val, .. } => Ok(val.to_string()),
|
Value::Int { val, .. } => Ok(val.to_string()),
|
||||||
Value::String { val, .. } => Ok(val.to_string()),
|
Value::String { val, .. } => Ok(val.to_string()),
|
||||||
_ => Err(ShellError::TypeMismatch(
|
_ => Err(ShellError::TypeMismatch {
|
||||||
|
err_message:
|
||||||
"could not perform substring. Expecting a string or int"
|
"could not perform substring. Expecting a string or int"
|
||||||
.to_string(),
|
.to_string(),
|
||||||
head,
|
span: head,
|
||||||
)),
|
}),
|
||||||
}
|
}
|
||||||
.unwrap_or_else(|_| String::from(""))
|
.unwrap_or_else(|_| String::from(""))
|
||||||
})
|
})
|
||||||
|
@ -243,14 +244,16 @@ fn process_arguments(range: &Value, head: Span) -> Result<(isize, isize), ShellE
|
||||||
|
|
||||||
let start = idx
|
let start = idx
|
||||||
.get(0)
|
.get(0)
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| ShellError::TypeMismatch {
|
||||||
ShellError::TypeMismatch("could not perform substring".to_string(), head)
|
err_message: "could not perform substring".to_string(),
|
||||||
|
span: head,
|
||||||
})?
|
})?
|
||||||
.to_string();
|
.to_string();
|
||||||
let end = idx
|
let end = idx
|
||||||
.get(1)
|
.get(1)
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| ShellError::TypeMismatch {
|
||||||
ShellError::TypeMismatch("could not perform substring".to_string(), head)
|
err_message: "could not perform substring".to_string(),
|
||||||
|
span: head,
|
||||||
})?
|
})?
|
||||||
.to_string();
|
.to_string();
|
||||||
Ok(SubstringText(start, end))
|
Ok(SubstringText(start, end))
|
||||||
|
@ -261,35 +264,39 @@ fn process_arguments(range: &Value, head: Span) -> Result<(isize, isize), ShellE
|
||||||
|
|
||||||
let start = idx
|
let start = idx
|
||||||
.first()
|
.first()
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| ShellError::TypeMismatch {
|
||||||
ShellError::TypeMismatch("could not perform substring".to_string(), head)
|
err_message: "could not perform substring".to_string(),
|
||||||
|
span: head,
|
||||||
})?
|
})?
|
||||||
.to_string();
|
.to_string();
|
||||||
let end = idx
|
let end = idx
|
||||||
.get(1)
|
.get(1)
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| ShellError::TypeMismatch {
|
||||||
ShellError::TypeMismatch("could not perform substring".to_string(), head)
|
err_message: "could not perform substring".to_string(),
|
||||||
|
span: head,
|
||||||
})?
|
})?
|
||||||
.to_string();
|
.to_string();
|
||||||
|
|
||||||
Ok(SubstringText(start, end))
|
Ok(SubstringText(start, end))
|
||||||
}
|
}
|
||||||
_ => Err(ShellError::TypeMismatch(
|
_ => Err(ShellError::TypeMismatch {
|
||||||
"could not perform substring".to_string(),
|
err_message: "could not perform substring".to_string(),
|
||||||
head,
|
span: head,
|
||||||
)),
|
}),
|
||||||
}?;
|
}?;
|
||||||
let start = match &search {
|
let start = match &search {
|
||||||
SubstringText(start, _) if start.is_empty() || start == "_" => 0,
|
SubstringText(start, _) if start.is_empty() || start == "_" => 0,
|
||||||
SubstringText(start, _) => start.trim().parse().map_err(|_| {
|
SubstringText(start, _) => start.trim().parse().map_err(|_| ShellError::TypeMismatch {
|
||||||
ShellError::TypeMismatch("could not perform substring".to_string(), head)
|
err_message: "could not perform substring".to_string(),
|
||||||
|
span: head,
|
||||||
})?,
|
})?,
|
||||||
};
|
};
|
||||||
|
|
||||||
let end = match &search {
|
let end = match &search {
|
||||||
SubstringText(_, end) if end.is_empty() || end == "_" => isize::max_value(),
|
SubstringText(_, end) if end.is_empty() || end == "_" => isize::max_value(),
|
||||||
SubstringText(_, end) => end.trim().parse().map_err(|_| {
|
SubstringText(_, end) => end.trim().parse().map_err(|_| ShellError::TypeMismatch {
|
||||||
ShellError::TypeMismatch("could not perform substring".to_string(), head)
|
err_message: "could not perform substring".to_string(),
|
||||||
|
span: head,
|
||||||
})?,
|
})?,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -254,10 +254,10 @@ fn which(
|
||||||
let ctrlc = engine_state.ctrlc.clone();
|
let ctrlc = engine_state.ctrlc.clone();
|
||||||
|
|
||||||
if which_args.applications.is_empty() {
|
if which_args.applications.is_empty() {
|
||||||
return Err(ShellError::MissingParameter(
|
return Err(ShellError::MissingParameter {
|
||||||
"application".into(),
|
param_name: "application".into(),
|
||||||
call.head,
|
span: call.head,
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut output = vec![];
|
let mut output = vec![];
|
||||||
|
|
|
@ -360,10 +360,10 @@ fn get_converted_value(
|
||||||
Err(e) => ConversionResult::ConversionError(e),
|
Err(e) => ConversionResult::ConversionError(e),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ConversionResult::ConversionError(ShellError::MissingParameter(
|
ConversionResult::ConversionError(ShellError::MissingParameter {
|
||||||
"block input".into(),
|
param_name: "block input".into(),
|
||||||
from_span,
|
span: from_span,
|
||||||
))
|
})
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ConversionResult::CellPathError
|
ConversionResult::CellPathError
|
||||||
|
|
|
@ -19,9 +19,10 @@ pub fn eval_operator(op: &Expression) -> Result<Operator, ShellError> {
|
||||||
expr: Expr::Operator(operator),
|
expr: Expr::Operator(operator),
|
||||||
..
|
..
|
||||||
} => Ok(operator.clone()),
|
} => Ok(operator.clone()),
|
||||||
Expression { span, expr, .. } => {
|
Expression { span, expr, .. } => Err(ShellError::UnknownOperator {
|
||||||
Err(ShellError::UnknownOperator(format!("{expr:?}"), *span))
|
op_token: format!("{expr:?}"),
|
||||||
}
|
span: *span,
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -339,7 +340,10 @@ pub fn eval_expression(
|
||||||
let lhs = eval_expression(engine_state, stack, expr)?;
|
let lhs = eval_expression(engine_state, stack, expr)?;
|
||||||
match lhs {
|
match lhs {
|
||||||
Value::Bool { val, .. } => Ok(Value::boolean(!val, expr.span)),
|
Value::Bool { val, .. } => Ok(Value::boolean(!val, expr.span)),
|
||||||
_ => Err(ShellError::TypeMismatch("bool".to_string(), expr.span)),
|
_ => Err(ShellError::TypeMismatch {
|
||||||
|
err_message: "bool".to_string(),
|
||||||
|
span: expr.span,
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Expr::BinaryOp(lhs, op, rhs) => {
|
Expr::BinaryOp(lhs, op, rhs) => {
|
||||||
|
@ -454,7 +458,7 @@ pub fn eval_expression(
|
||||||
stack.vars.insert(*var_id, rhs);
|
stack.vars.insert(*var_id, rhs);
|
||||||
Ok(Value::nothing(lhs.span))
|
Ok(Value::nothing(lhs.span))
|
||||||
} else {
|
} else {
|
||||||
Err(ShellError::AssignmentRequiresMutableVar(lhs.span))
|
Err(ShellError::AssignmentRequiresMutableVar { lhs_span: lhs.span })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Expr::FullCellPath(cell_path) => match &cell_path.head.expr {
|
Expr::FullCellPath(cell_path) => match &cell_path.head.expr {
|
||||||
|
@ -496,12 +500,14 @@ pub fn eval_expression(
|
||||||
}
|
}
|
||||||
Ok(Value::nothing(cell_path.head.span))
|
Ok(Value::nothing(cell_path.head.span))
|
||||||
} else {
|
} else {
|
||||||
Err(ShellError::AssignmentRequiresMutableVar(lhs.span))
|
Err(ShellError::AssignmentRequiresMutableVar {
|
||||||
|
lhs_span: lhs.span,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => Err(ShellError::AssignmentRequiresVar(lhs.span)),
|
_ => Err(ShellError::AssignmentRequiresVar { lhs_span: lhs.span }),
|
||||||
},
|
},
|
||||||
_ => Err(ShellError::AssignmentRequiresVar(lhs.span)),
|
_ => Err(ShellError::AssignmentRequiresVar { lhs_span: lhs.span }),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -290,10 +290,14 @@ impl PipelineData {
|
||||||
match self {
|
match self {
|
||||||
PipelineData::Empty => Ok((String::new(), span, None)),
|
PipelineData::Empty => Ok((String::new(), span, None)),
|
||||||
PipelineData::Value(Value::String { val, span }, metadata) => Ok((val, span, metadata)),
|
PipelineData::Value(Value::String { val, span }, metadata) => Ok((val, span, metadata)),
|
||||||
PipelineData::Value(val, _) => {
|
PipelineData::Value(val, _) => Err(ShellError::TypeMismatch {
|
||||||
Err(ShellError::TypeMismatch("string".into(), val.span()?))
|
err_message: "string".into(),
|
||||||
}
|
span: val.span()?,
|
||||||
PipelineData::ListStream(_, _) => Err(ShellError::TypeMismatch("string".into(), span)),
|
}),
|
||||||
|
PipelineData::ListStream(_, _) => Err(ShellError::TypeMismatch {
|
||||||
|
err_message: "string".into(),
|
||||||
|
span,
|
||||||
|
}),
|
||||||
PipelineData::ExternalStream {
|
PipelineData::ExternalStream {
|
||||||
stdout: None,
|
stdout: None,
|
||||||
metadata,
|
metadata,
|
||||||
|
|
|
@ -98,17 +98,7 @@ pub enum ShellError {
|
||||||
/// Convert the argument type before passing it in, or change the command to accept the type.
|
/// Convert the argument type before passing it in, or change the command to accept the type.
|
||||||
#[error("Type mismatch.")]
|
#[error("Type mismatch.")]
|
||||||
#[diagnostic(code(nu::shell::type_mismatch))]
|
#[diagnostic(code(nu::shell::type_mismatch))]
|
||||||
TypeMismatch(String, #[label = "{0}"] Span),
|
TypeMismatch {
|
||||||
|
|
||||||
// TODO: merge with `TypeMismatch` as they are currently identical in capability
|
|
||||||
/// A command received an argument of the wrong type.
|
|
||||||
///
|
|
||||||
/// ## Resolution
|
|
||||||
///
|
|
||||||
/// Convert the argument type before passing it in, or change the command to accept the type.
|
|
||||||
#[error("Type mismatch.")]
|
|
||||||
#[diagnostic(code(nu::shell::type_mismatch))]
|
|
||||||
TypeMismatchGenericMessage {
|
|
||||||
err_message: String,
|
err_message: String,
|
||||||
#[label = "{err_message}"]
|
#[label = "{err_message}"]
|
||||||
span: Span,
|
span: Span,
|
||||||
|
@ -121,7 +111,11 @@ pub enum ShellError {
|
||||||
/// Correct the argument value before passing it in or change the command.
|
/// Correct the argument value before passing it in or change the command.
|
||||||
#[error("Incorrect value.")]
|
#[error("Incorrect value.")]
|
||||||
#[diagnostic(code(nu::shell::incorrect_value))]
|
#[diagnostic(code(nu::shell::incorrect_value))]
|
||||||
IncorrectValue(String, #[label = "{0}"] Span),
|
IncorrectValue {
|
||||||
|
msg: String,
|
||||||
|
#[label = "{msg}"]
|
||||||
|
span: Span,
|
||||||
|
},
|
||||||
|
|
||||||
/// This value cannot be used with this operator.
|
/// This value cannot be used with this operator.
|
||||||
///
|
///
|
||||||
|
@ -129,45 +123,63 @@ pub enum ShellError {
|
||||||
///
|
///
|
||||||
/// Not all values, for example custom values, can be used with all operators. Either
|
/// Not all values, for example custom values, can be used with all operators. Either
|
||||||
/// implement support for the operator on this type, or convert the type to a supported one.
|
/// implement support for the operator on this type, or convert the type to a supported one.
|
||||||
#[error("Unsupported operator: {0}.")]
|
#[error("Unsupported operator: {operator}.")]
|
||||||
#[diagnostic(code(nu::shell::unsupported_operator))]
|
#[diagnostic(code(nu::shell::unsupported_operator))]
|
||||||
UnsupportedOperator(Operator, #[label = "unsupported operator"] Span),
|
UnsupportedOperator {
|
||||||
|
operator: Operator,
|
||||||
|
#[label = "unsupported operator"]
|
||||||
|
span: Span,
|
||||||
|
},
|
||||||
|
|
||||||
/// This value cannot be used with this operator.
|
/// Invalid assignment left-hand side
|
||||||
///
|
///
|
||||||
/// ## Resolution
|
/// ## Resolution
|
||||||
///
|
///
|
||||||
/// Assignment requires that you assign to a variable or variable cell path.
|
/// Assignment requires that you assign to a variable or variable cell path.
|
||||||
#[error("Assignment operations require a variable.")]
|
#[error("Assignment operations require a variable.")]
|
||||||
#[diagnostic(code(nu::shell::assignment_requires_variable))]
|
#[diagnostic(code(nu::shell::assignment_requires_variable))]
|
||||||
AssignmentRequiresVar(#[label = "needs to be a variable"] Span),
|
AssignmentRequiresVar {
|
||||||
|
#[label = "needs to be a variable"]
|
||||||
|
lhs_span: Span,
|
||||||
|
},
|
||||||
|
|
||||||
/// This value cannot be used with this operator.
|
/// Invalid assignment left-hand side
|
||||||
///
|
///
|
||||||
/// ## Resolution
|
/// ## Resolution
|
||||||
///
|
///
|
||||||
/// Assignment requires that you assign to a mutable variable or cell path.
|
/// Assignment requires that you assign to a mutable variable or cell path.
|
||||||
#[error("Assignment to an immutable variable.")]
|
#[error("Assignment to an immutable variable.")]
|
||||||
#[diagnostic(code(nu::shell::assignment_requires_mutable_variable))]
|
#[diagnostic(code(nu::shell::assignment_requires_mutable_variable))]
|
||||||
AssignmentRequiresMutableVar(#[label = "needs to be a mutable variable"] Span),
|
AssignmentRequiresMutableVar {
|
||||||
|
#[label = "needs to be a mutable variable"]
|
||||||
|
lhs_span: Span,
|
||||||
|
},
|
||||||
|
|
||||||
/// An operator was not recognized during evaluation.
|
/// An operator was not recognized during evaluation.
|
||||||
///
|
///
|
||||||
/// ## Resolution
|
/// ## Resolution
|
||||||
///
|
///
|
||||||
/// Did you write the correct operator?
|
/// Did you write the correct operator?
|
||||||
#[error("Unknown operator: {0}.")]
|
#[error("Unknown operator: {op_token}.")]
|
||||||
#[diagnostic(code(nu::shell::unknown_operator))]
|
#[diagnostic(code(nu::shell::unknown_operator))]
|
||||||
UnknownOperator(String, #[label = "unknown operator"] Span),
|
UnknownOperator {
|
||||||
|
op_token: String,
|
||||||
|
#[label = "unknown operator"]
|
||||||
|
span: Span,
|
||||||
|
},
|
||||||
|
|
||||||
/// An expected command parameter is missing.
|
/// An expected command parameter is missing.
|
||||||
///
|
///
|
||||||
/// ## Resolution
|
/// ## Resolution
|
||||||
///
|
///
|
||||||
/// Add the expected parameter and try again.
|
/// Add the expected parameter and try again.
|
||||||
#[error("Missing parameter: {0}.")]
|
#[error("Missing parameter: {param_name}.")]
|
||||||
#[diagnostic(code(nu::shell::missing_parameter))]
|
#[diagnostic(code(nu::shell::missing_parameter))]
|
||||||
MissingParameter(String, #[label = "missing parameter: {0}"] Span),
|
MissingParameter {
|
||||||
|
param_name: String,
|
||||||
|
#[label = "missing parameter: {param_name}"]
|
||||||
|
span: Span,
|
||||||
|
},
|
||||||
|
|
||||||
/// Two parameters conflict with each other or are otherwise mutually exclusive.
|
/// Two parameters conflict with each other or are otherwise mutually exclusive.
|
||||||
///
|
///
|
||||||
|
@ -193,7 +205,11 @@ pub enum ShellError {
|
||||||
/// Check your syntax for mismatched braces, RegExp syntax errors, etc, based on the specific error message.
|
/// Check your syntax for mismatched braces, RegExp syntax errors, etc, based on the specific error message.
|
||||||
#[error("Delimiter error")]
|
#[error("Delimiter error")]
|
||||||
#[diagnostic(code(nu::shell::delimiter_error))]
|
#[diagnostic(code(nu::shell::delimiter_error))]
|
||||||
DelimiterError(String, #[label("{0}")] Span),
|
DelimiterError {
|
||||||
|
msg: String,
|
||||||
|
#[label("{msg}")]
|
||||||
|
span: Span,
|
||||||
|
},
|
||||||
|
|
||||||
/// An operation received parameters with some sort of incompatibility
|
/// An operation received parameters with some sort of incompatibility
|
||||||
/// (for example, different number of rows in a table, incompatible column names, etc).
|
/// (for example, different number of rows in a table, incompatible column names, etc).
|
||||||
|
@ -204,7 +220,11 @@ pub enum ShellError {
|
||||||
/// inputs to make sure they match that way.
|
/// inputs to make sure they match that way.
|
||||||
#[error("Incompatible parameters.")]
|
#[error("Incompatible parameters.")]
|
||||||
#[diagnostic(code(nu::shell::incompatible_parameters))]
|
#[diagnostic(code(nu::shell::incompatible_parameters))]
|
||||||
IncompatibleParametersSingle(String, #[label = "{0}"] Span),
|
IncompatibleParametersSingle {
|
||||||
|
msg: String,
|
||||||
|
#[label = "{msg}"]
|
||||||
|
span: Span,
|
||||||
|
},
|
||||||
|
|
||||||
/// This build of nushell implements this feature, but it has not been enabled.
|
/// This build of nushell implements this feature, but it has not been enabled.
|
||||||
///
|
///
|
||||||
|
|
|
@ -55,6 +55,6 @@ pub trait CustomValue: fmt::Debug + Send + Sync {
|
||||||
op: Span,
|
op: Span,
|
||||||
_right: &Value,
|
_right: &Value,
|
||||||
) -> Result<Value, ShellError> {
|
) -> Result<Value, ShellError> {
|
||||||
Err(ShellError::UnsupportedOperator(operator, op))
|
Err(ShellError::UnsupportedOperator { operator, span: op })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -763,7 +763,7 @@ impl Value {
|
||||||
// Records (and tables) are the only built-in which support column names,
|
// Records (and tables) are the only built-in which support column names,
|
||||||
// so only use this message for them.
|
// so only use this message for them.
|
||||||
Value::Record { .. } => {
|
Value::Record { .. } => {
|
||||||
err_or_null!(ShellError::TypeMismatchGenericMessage {
|
err_or_null!(ShellError::TypeMismatch {
|
||||||
err_message: "Can't access record values with a row index. Try specifying a column name instead".into(),
|
err_message: "Can't access record values with a row index. Try specifying a column name instead".into(),
|
||||||
span: *origin_span, }, *origin_span)
|
span: *origin_span, }, *origin_span)
|
||||||
}
|
}
|
||||||
|
@ -2573,7 +2573,10 @@ impl Value {
|
||||||
&& (self.get_type() != Type::Any)
|
&& (self.get_type() != Type::Any)
|
||||||
&& (rhs.get_type() != Type::Any)
|
&& (rhs.get_type() != Type::Any)
|
||||||
{
|
{
|
||||||
return Err(ShellError::TypeMismatch("compatible type".to_string(), op));
|
return Err(ShellError::TypeMismatch {
|
||||||
|
err_message: "compatible type".to_string(),
|
||||||
|
span: op,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(ordering) = self.partial_cmp(rhs) {
|
if let Some(ordering) = self.partial_cmp(rhs) {
|
||||||
|
@ -2606,7 +2609,10 @@ impl Value {
|
||||||
&& (self.get_type() != Type::Any)
|
&& (self.get_type() != Type::Any)
|
||||||
&& (rhs.get_type() != Type::Any)
|
&& (rhs.get_type() != Type::Any)
|
||||||
{
|
{
|
||||||
return Err(ShellError::TypeMismatch("compatible type".to_string(), op));
|
return Err(ShellError::TypeMismatch {
|
||||||
|
err_message: "compatible type".to_string(),
|
||||||
|
span: op,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
self.partial_cmp(rhs)
|
self.partial_cmp(rhs)
|
||||||
|
@ -2637,7 +2643,10 @@ impl Value {
|
||||||
&& (self.get_type() != Type::Any)
|
&& (self.get_type() != Type::Any)
|
||||||
&& (rhs.get_type() != Type::Any)
|
&& (rhs.get_type() != Type::Any)
|
||||||
{
|
{
|
||||||
return Err(ShellError::TypeMismatch("compatible type".to_string(), op));
|
return Err(ShellError::TypeMismatch {
|
||||||
|
err_message: "compatible type".to_string(),
|
||||||
|
span: op,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
self.partial_cmp(rhs)
|
self.partial_cmp(rhs)
|
||||||
|
@ -2668,7 +2677,10 @@ impl Value {
|
||||||
&& (self.get_type() != Type::Any)
|
&& (self.get_type() != Type::Any)
|
||||||
&& (rhs.get_type() != Type::Any)
|
&& (rhs.get_type() != Type::Any)
|
||||||
{
|
{
|
||||||
return Err(ShellError::TypeMismatch("compatible type".to_string(), op));
|
return Err(ShellError::TypeMismatch {
|
||||||
|
err_message: "compatible type".to_string(),
|
||||||
|
span: op,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
match self.partial_cmp(rhs) {
|
match self.partial_cmp(rhs) {
|
||||||
|
|
|
@ -121,7 +121,10 @@ pub(crate) fn parse_commandline_args(
|
||||||
span: expr.span,
|
span: expr.span,
|
||||||
}))
|
}))
|
||||||
} else {
|
} else {
|
||||||
Err(ShellError::TypeMismatch("string".into(), expr.span))
|
Err(ShellError::TypeMismatch {
|
||||||
|
err_message: "string".into(),
|
||||||
|
span: expr.span,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user