templater: consolidate "unexpected expression" error constructors

I also renamed "UnexpectedExpression" to just "Expression" because "unexpected"
doesn't apply to "immutable" revset parse/evaluation errors.
This commit is contained in:
Yuya Nishihara 2024-03-29 20:35:36 +09:00
parent b09732f4f8
commit 76f3b80e8a
5 changed files with 20 additions and 36 deletions

View file

@ -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,
)),

View file

@ -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,
)),

View file

@ -636,10 +636,13 @@ fn evaluate_immutable_revset<'repo>(
// It's usually smaller than the immutable set. The revset engine can also
// optimize "::<recent_heads>" 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)
}

View file

@ -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,
)),

View file

@ -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<String>, 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<String>,
source: impl Into<Box<dyn error::Error + Send + Sync>>,
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<String>, 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<Rule>) -> 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,
)),