Improve def parse errors (#3681)

This commit is contained in:
JT
2021-06-25 17:18:38 +12:00
committed by GitHub
parent a3f119e0bd
commit cac2875c96
3 changed files with 109 additions and 68 deletions

View File

@ -32,7 +32,10 @@ pub enum ParseErrorReason {
Unclosed { delimiter: String, span: Span },
/// An unexpected internal error has occurred
InternalError { message: Spanned<String> },
GeneralError {
message: String,
label: Spanned<String>,
},
/// The parser tried to parse an argument for a command, but it failed for
/// some reason
@ -83,11 +86,15 @@ impl ParseError {
}
}
/// Construct a [ParseErrorReason::InternalError](ParseErrorReason::InternalError)
pub fn internal_error(message: Spanned<impl Into<String>>) -> ParseError {
/// Construct a [ParseErrorReason::GeneralError](ParseErrorReason::GeneralError)
pub fn general_error(
message: impl Into<String>,
label: Spanned<impl Into<String>>,
) -> ParseError {
ParseError {
reason: ParseErrorReason::InternalError {
message: message.item.into().spanned(message.span),
reason: ParseErrorReason::GeneralError {
message: message.into(),
label: label.item.into().spanned(label.span),
},
}
}
@ -119,11 +126,9 @@ impl From<ParseError> for ShellError {
ParseErrorReason::Mismatch { actual, expected } => {
ShellError::type_error(expected, actual)
}
ParseErrorReason::InternalError { message } => ShellError::labeled_error(
format!("Internal error: {}", message.item),
&message.item,
&message.span,
),
ParseErrorReason::GeneralError { message, label } => {
ShellError::labeled_error(&message, &label.item, &label.span)
}
ParseErrorReason::ArgumentError { command, error } => {
ShellError::argument_error(command, error)
}