diff --git a/src/template_builder.rs b/src/template_builder.rs index 86544ccf8..0abcf4589 100644 --- a/src/template_builder.rs +++ b/src/template_builder.rs @@ -17,7 +17,7 @@ use jujutsu_lib::backend::{Signature, Timestamp}; use crate::template_parser::{ self, ExpressionKind, ExpressionNode, FunctionCallNode, MethodCallNode, TemplateParseError, - TemplateParseErrorKind, TemplateParseResult, + TemplateParseResult, }; use crate::templater::{ ConcatTemplate, ConditionalTemplate, FormattablePropertyListTemplate, IntoTemplate, @@ -388,8 +388,7 @@ fn build_timestamp_method<'a, L: TemplateLanguage<'a>>( let format = template_parser::expect_string_literal_with(format_node, |format, span| { time_util::FormattingItems::parse(format).ok_or_else(|| { - let kind = TemplateParseErrorKind::InvalidTimeFormat; - TemplateParseError::with_span(kind, span) + TemplateParseError::unexpected_expression("Invalid time format", span) }) })? .into_owned(); @@ -570,7 +569,7 @@ pub fn expect_boolean_expression<'a, L: TemplateLanguage<'a>>( ) -> TemplateParseResult + 'a>> { build_expression(language, node)? .try_into_boolean() - .ok_or_else(|| TemplateParseError::invalid_argument_type("Boolean", node.span)) + .ok_or_else(|| TemplateParseError::expected_type("Boolean", node.span)) } pub fn expect_integer_expression<'a, L: TemplateLanguage<'a>>( @@ -579,5 +578,5 @@ pub fn expect_integer_expression<'a, L: TemplateLanguage<'a>>( ) -> TemplateParseResult + 'a>> { build_expression(language, node)? .try_into_integer() - .ok_or_else(|| TemplateParseError::invalid_argument_type("Integer", node.span)) + .ok_or_else(|| TemplateParseError::expected_type("Integer", node.span)) } diff --git a/src/template_parser.rs b/src/template_parser.rs index bd3936786..2067329ba 100644 --- a/src/template_parser.rs +++ b/src/template_parser.rs @@ -49,12 +49,10 @@ pub enum TemplateParseErrorKind { NoSuchMethod { type_name: String, name: String }, #[error(r#"Function "{name}": {message}"#)] InvalidArguments { name: String, message: String }, - #[error(r#"Expected argument of type "{0}""#)] - InvalidArgumentType(String), - #[error("Invalid time format")] - InvalidTimeFormat, #[error("Redefinition of function parameter")] RedefinedFunctionParameter, + #[error("{0}")] + UnexpectedExpression(String), #[error(r#"Alias "{0}" cannot be expanded"#)] BadAliasExpansion(String), #[error(r#"Alias "{0}" expanded recursively"#)] @@ -125,12 +123,14 @@ impl TemplateParseError { ) } - pub fn invalid_argument_type( - expected_type_name: impl Into, - span: pest::Span<'_>, - ) -> Self { + 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) + } + + pub fn unexpected_expression(message: impl Into, span: pest::Span<'_>) -> Self { TemplateParseError::with_span( - TemplateParseErrorKind::InvalidArgumentType(expected_type_name.into()), + TemplateParseErrorKind::UnexpectedExpression(message.into()), span, ) } @@ -636,8 +636,8 @@ pub fn expect_string_literal_with<'a, 'i, T>( | ExpressionKind::Integer(_) | ExpressionKind::Concat(_) | ExpressionKind::FunctionCall(_) - | ExpressionKind::MethodCall(_) => Err(TemplateParseError::invalid_argument_type( - "String literal", + | ExpressionKind::MethodCall(_) => Err(TemplateParseError::unexpected_expression( + "Expected string literal", node.span, )), ExpressionKind::AliasExpanded(id, subst) => expect_string_literal_with(subst, f) diff --git a/tests/test_templater.rs b/tests/test_templater.rs index 47d13e2e0..f912964f5 100644 --- a/tests/test_templater.rs +++ b/tests/test_templater.rs @@ -232,7 +232,7 @@ fn test_templater_parse_error() { 1 | if(label("foo", "bar"), "baz") | ^-----------------^ | - = Expected argument of type "Boolean" + = Expected expression of type "Boolean" "###); } @@ -368,7 +368,7 @@ fn test_templater_timestamp_method() { 1 | author.timestamp().format(0) | ^ | - = Expected argument of type "String literal" + = Expected string literal "###); // Dynamic string isn't supported yet @@ -378,7 +378,7 @@ fn test_templater_timestamp_method() { 1 | author.timestamp().format("%Y" ++ "%m") | ^----------^ | - = Expected argument of type "String literal" + = Expected string literal "###); // Literal alias expansion @@ -657,7 +657,7 @@ fn test_templater_alias() { 1 | identity(identity(commit_id.short(""))) | ^^ | - = Expected argument of type "Integer" + = Expected expression of type "Integer" "###); insta::assert_snapshot!(render_err("commit_id ++ recurse"), @r###" @@ -716,7 +716,7 @@ fn test_templater_alias() { 1 | coalesce(label("x", "not boolean"), "") | ^-----------------------^ | - = Expected argument of type "Boolean" + = Expected expression of type "Boolean" "###); }