diff --git a/cli/examples/custom-commit-templater/main.rs b/cli/examples/custom-commit-templater/main.rs index ce20d7a64..6bbfe9786 100644 --- a/cli/examples/custom-commit-templater/main.rs +++ b/cli/examples/custom-commit-templater/main.rs @@ -108,7 +108,7 @@ impl CommitTemplateLanguageExtension for HexCounter { let chars: Vec<_> = string.chars().collect(); match chars[..] { [ch] => Ok(ch), - _ => Err(TemplateParseError::unexpected_expression( + _ => Err(TemplateParseError::expression( "Expected singular character argument", span, )), diff --git a/cli/examples/custom-operation-templater/main.rs b/cli/examples/custom-operation-templater/main.rs index 562f1efb4..2c4a29b0b 100644 --- a/cli/examples/custom-operation-templater/main.rs +++ b/cli/examples/custom-operation-templater/main.rs @@ -68,7 +68,7 @@ impl OperationTemplateLanguageExtension for HexCounter { let chars: Vec<_> = string.chars().collect(); match chars[..] { [ch] => Ok(ch), - _ => Err(TemplateParseError::unexpected_expression( + _ => Err(TemplateParseError::expression( "Expected singular character argument", span, )), diff --git a/cli/src/commit_templater.rs b/cli/src/commit_templater.rs index 628ed8beb..ca65f19b1 100644 --- a/cli/src/commit_templater.rs +++ b/cli/src/commit_templater.rs @@ -636,10 +636,13 @@ fn evaluate_immutable_revset<'repo>( // It's usually smaller than the immutable set. The revset engine can also // optimize "::" query to use bitset-based implementation. let expression = revset_util::parse_immutable_expression(&language.revset_parse_context) - .map_err(|err| TemplateParseError::other("Failed to parse revset", err, span))?; + .map_err(|err| { + TemplateParseError::expression("Failed to parse revset", span).with_source(err) + })?; let symbol_resolver = revset_util::default_symbol_resolver(repo, language.id_prefix_context); - let revset = revset_util::evaluate(repo, &symbol_resolver, expression) - .map_err(|err| TemplateParseError::other("Failed to evaluate revset", err, span))?; + let revset = revset_util::evaluate(repo, &symbol_resolver, expression).map_err(|err| { + TemplateParseError::expression("Failed to evaluate revset", span).with_source(err) + })?; Ok(revset) } diff --git a/cli/src/template_builder.rs b/cli/src/template_builder.rs index a4043a9a4..1767cb1a9 100644 --- a/cli/src/template_builder.rs +++ b/cli/src/template_builder.rs @@ -682,9 +682,8 @@ fn builtin_timestamp_methods<'a, L: TemplateLanguage<'a> + ?Sized>( let [format_node] = template_parser::expect_exact_arguments(function)?; let format = template_parser::expect_string_literal_with(format_node, |format, span| { - time_util::FormattingItems::parse(format).ok_or_else(|| { - TemplateParseError::unexpected_expression("Invalid time format", span) - }) + time_util::FormattingItems::parse(format) + .ok_or_else(|| TemplateParseError::expression("Invalid time format", span)) })? .into_owned(); let out_property = self_property.and_then(move |timestamp| { @@ -847,7 +846,7 @@ where if let [name] = lambda.params.as_slice() { local_variables.insert(name, &item_fn); } else { - return Err(TemplateParseError::unexpected_expression( + return Err(TemplateParseError::expression( "Expected 1 lambda parameters", lambda.params_span, )); @@ -1028,7 +1027,7 @@ pub fn build_expression<'a, L: TemplateLanguage<'a> + ?Sized>( expression.labels.push(method.function.name.to_owned()); Ok(expression) } - ExpressionKind::Lambda(_) => Err(TemplateParseError::unexpected_expression( + ExpressionKind::Lambda(_) => Err(TemplateParseError::expression( "Lambda cannot be defined here", node.span, )), diff --git a/cli/src/template_parser.rs b/cli/src/template_parser.rs index 78bf23265..5bf45f06c 100644 --- a/cli/src/template_parser.rs +++ b/cli/src/template_parser.rs @@ -98,7 +98,7 @@ pub enum TemplateParseErrorKind { #[error("Redefinition of function parameter")] RedefinedFunctionParameter, #[error("{0}")] - UnexpectedExpression(String), + Expression(String), #[error(r#"Alias "{0}" cannot be expanded"#)] BadAliasExpansion(String), #[error(r#"Alias "{0}" expanded recursively"#)] @@ -151,27 +151,12 @@ impl TemplateParseError { pub fn expected_type(type_name: &str, span: pest::Span<'_>) -> Self { let message = format!(r#"Expected expression of type "{type_name}""#); - TemplateParseError::unexpected_expression(message, span) + TemplateParseError::expression(message, span) } - pub fn unexpected_expression(message: impl Into, span: pest::Span<'_>) -> Self { - TemplateParseError::with_span( - TemplateParseErrorKind::UnexpectedExpression(message.into()), - span, - ) - } - - /// Some other expression error with the underlying error `source`. - pub fn other( - message: impl Into, - source: impl Into>, - span: pest::Span<'_>, - ) -> Self { - TemplateParseError::with_span( - TemplateParseErrorKind::UnexpectedExpression(message.into()), - span, - ) - .with_source(source) + /// Some other expression error. + pub fn expression(message: impl Into, span: pest::Span<'_>) -> Self { + TemplateParseError::with_span(TemplateParseErrorKind::Expression(message.into()), span) } pub fn within_alias_expansion(self, id: TemplateAliasId<'_>, span: pest::Span<'_>) -> Self { @@ -328,10 +313,7 @@ fn parse_identifier_name(pair: Pair) -> TemplateParseResult<&str> { if let ExpressionKind::Identifier(name) = parse_identifier_or_literal(pair) { Ok(name) } else { - Err(TemplateParseError::unexpected_expression( - "Expected identifier", - span, - )) + Err(TemplateParseError::expression("Expected identifier", span)) } } @@ -853,7 +835,7 @@ pub fn expect_string_literal_with<'a, 'i, T>( | ExpressionKind::Concat(_) | ExpressionKind::FunctionCall(_) | ExpressionKind::MethodCall(_) - | ExpressionKind::Lambda(_) => Err(TemplateParseError::unexpected_expression( + | ExpressionKind::Lambda(_) => Err(TemplateParseError::expression( "Expected string literal", node.span, )), @@ -877,7 +859,7 @@ pub fn expect_lambda_with<'a, 'i, T>( | ExpressionKind::Binary(..) | ExpressionKind::Concat(_) | ExpressionKind::FunctionCall(_) - | ExpressionKind::MethodCall(_) => Err(TemplateParseError::unexpected_expression( + | ExpressionKind::MethodCall(_) => Err(TemplateParseError::expression( "Expected lambda expression", node.span, )),