templater: extract little helper that parses text and expands aliases

The caller doesn't have to care about alias expansion.
This commit is contained in:
Yuya Nishihara 2023-03-12 20:02:32 +09:00
parent b380ca0cf7
commit 91d309a93c
3 changed files with 14 additions and 6 deletions

View file

@ -458,8 +458,7 @@ pub fn parse<'repo>(
aliases_map: &TemplateAliasesMap,
) -> TemplateParseResult<Box<dyn Template<Commit> + 'repo>> {
let language = CommitTemplateLanguage { repo, workspace_id };
let node = template_parser::parse_template(template_text)?;
let node = template_parser::expand_aliases(node, aliases_map)?;
let node = template_parser::parse(template_text, aliases_map)?;
let expression = template_parser::build_expression(&language, &node)?;
Ok(expression.into_template())
}

View file

@ -187,8 +187,7 @@ pub fn parse(
) -> TemplateParseResult<Box<dyn Template<Operation>>> {
let head_op_id = repo.op_id();
let language = OperationTemplateLanguage { head_op_id };
let node = template_parser::parse_template(template_text)?;
let node = template_parser::expand_aliases(node, aliases_map)?;
let node = template_parser::parse(template_text, aliases_map)?;
let expression = template_parser::build_expression(&language, &node)?;
Ok(expression.into_template())
}

View file

@ -587,6 +587,17 @@ pub fn expand_aliases<'i>(
expand_node(node, state)
}
/// Parses text into AST nodes, and expands aliases.
///
/// No type/name checking is made at this stage.
pub fn parse<'i>(
template_text: &'i str,
aliases_map: &'i TemplateAliasesMap,
) -> TemplateParseResult<ExpressionNode<'i>> {
let node = parse_template(template_text)?;
expand_aliases(node, aliases_map)
}
/// Callbacks to build language-specific evaluation objects from AST nodes.
pub trait TemplateLanguage<'a> {
type Context: 'a;
@ -1195,8 +1206,7 @@ mod tests {
impl WithTemplateAliasesMap {
fn parse<'i>(&'i self, template_text: &'i str) -> TemplateParseResult<ExpressionNode<'i>> {
let node = parse_template(template_text)?;
expand_aliases(node, &self.0)
parse(template_text, &self.0)
}
fn parse_normalized<'i>(