templater: extract stub function that resolves method name

We'll probably add "Did you mean?" hint there.
This commit is contained in:
Yuya Nishihara 2024-02-25 10:46:23 +09:00
parent 6e29abb180
commit 1b4c339203
2 changed files with 17 additions and 6 deletions

View file

@ -71,12 +71,9 @@ impl<'repo> TemplateLanguage<'repo> for CommitTemplateLanguage<'repo> {
template_builder::build_core_method(self, build_ctx, property, function)
}
CommitTemplatePropertyKind::Commit(property) => {
// TODO: add name resolution helper that provides typo hint
if let Some(build) = self.build_fn_table.commit_methods.get(function.name) {
build(self, build_ctx, property, function)
} else {
Err(TemplateParseError::no_such_method("Commit", function))
}
let table = &self.build_fn_table.commit_methods;
let build = template_parser::lookup_method("Commit", table, function)?;
build(self, build_ctx, property, function)
}
CommitTemplatePropertyKind::CommitList(property) => {
template_builder::build_unformattable_list_method(

View file

@ -855,6 +855,20 @@ pub fn expect_lambda_with<'a, 'i, T>(
}
}
/// Looks up `table` by the given method name.
pub fn lookup_method<'a, V>(
type_name: impl Into<String>,
table: &'a HashMap<&str, V>,
function: &FunctionCallNode,
) -> TemplateParseResult<&'a V> {
if let Some(value) = table.get(function.name) {
Ok(value)
} else {
// TODO: provide typo hint
Err(TemplateParseError::no_such_method(type_name, function))
}
}
#[cfg(test)]
mod tests {
use assert_matches::assert_matches;