templater: dispatch global functions through TemplateLanguage trait

Prepares for migrating to table-based lookup. It's unlikely that the
implementor handles global function calls differently, but the core doesn't
have an access to the customized symbol table.
This commit is contained in:
Yuya Nishihara 2024-03-14 20:00:29 +09:00
parent 1e8bf8426c
commit fa9cb72445
4 changed files with 33 additions and 2 deletions

View file

@ -107,6 +107,14 @@ impl<'repo> TemplateLanguage<'repo> for CommitTemplateLanguage<'repo> {
self.wrap_commit(TemplatePropertyFn(|commit: &Commit| Ok(commit.clone())))
}
fn build_function(
&self,
build_ctx: &BuildContext<Self::Property>,
function: &FunctionCallNode,
) -> TemplateParseResult<Self::Property> {
template_builder::build_global_function(self, build_ctx, function)
}
fn build_method(
&self,
build_ctx: &BuildContext<Self::Property>,

View file

@ -83,6 +83,14 @@ impl<'a, C: 'a> TemplateLanguage<'a> for GenericTemplateLanguage<'a, C> {
GenericTemplatePropertyKind::Self_
}
fn build_function(
&self,
build_ctx: &BuildContext<Self::Property>,
function: &FunctionCallNode,
) -> TemplateParseResult<Self::Property> {
template_builder::build_global_function(self, build_ctx, function)
}
fn build_method(
&self,
build_ctx: &BuildContext<Self::Property>,

View file

@ -59,6 +59,14 @@ impl TemplateLanguage<'static> for OperationTemplateLanguage {
self.wrap_operation(TemplatePropertyFn(|op: &Operation| Ok(op.clone())))
}
fn build_function(
&self,
build_ctx: &BuildContext<Self::Property>,
function: &FunctionCallNode,
) -> TemplateParseResult<Self::Property> {
template_builder::build_global_function(self, build_ctx, function)
}
fn build_method(
&self,
build_ctx: &BuildContext<Self::Property>,

View file

@ -73,6 +73,13 @@ pub trait TemplateLanguage<'a> {
/// clones the `Context` object.
fn build_self(&self) -> Self::Property;
/// Translates the given global `function` call to a property.
fn build_function(
&self,
build_ctx: &BuildContext<Self::Property>,
function: &FunctionCallNode,
) -> TemplateParseResult<Self::Property>;
fn build_method(
&self,
build_ctx: &BuildContext<Self::Property>,
@ -891,7 +898,7 @@ where
Ok(language.wrap_list_template(Box::new(list_template)))
}
fn build_global_function<'a, L: TemplateLanguage<'a> + ?Sized>(
pub fn build_global_function<'a, L: TemplateLanguage<'a> + ?Sized>(
language: &L,
build_ctx: &BuildContext<L::Property>,
function: &FunctionCallNode,
@ -1033,7 +1040,7 @@ pub fn build_expression<'a, L: TemplateLanguage<'a> + ?Sized>(
Ok(Expression::unlabeled(property))
}
ExpressionKind::FunctionCall(function) => {
let property = build_global_function(language, build_ctx, function)?;
let property = language.build_function(build_ctx, function)?;
Ok(Expression::unlabeled(property))
}
ExpressionKind::MethodCall(method) => {