forked from mirrors/jj
template_builder: support extending the core template build fn table
This commit is contained in:
parent
5c252bd8e4
commit
18670ff6e0
1 changed files with 46 additions and 6 deletions
|
@ -246,15 +246,26 @@ pub type TemplateBuildMethodFnMap<'a, L, T> =
|
||||||
|
|
||||||
/// Symbol table of methods available in the core template.
|
/// Symbol table of methods available in the core template.
|
||||||
pub struct CoreTemplateBuildFnTable<'a, L: TemplateLanguage<'a>> {
|
pub struct CoreTemplateBuildFnTable<'a, L: TemplateLanguage<'a>> {
|
||||||
string_methods: TemplateBuildMethodFnMap<'a, L, String>,
|
pub string_methods: TemplateBuildMethodFnMap<'a, L, String>,
|
||||||
boolean_methods: TemplateBuildMethodFnMap<'a, L, bool>,
|
pub boolean_methods: TemplateBuildMethodFnMap<'a, L, bool>,
|
||||||
integer_methods: TemplateBuildMethodFnMap<'a, L, i64>,
|
pub integer_methods: TemplateBuildMethodFnMap<'a, L, i64>,
|
||||||
signature_methods: TemplateBuildMethodFnMap<'a, L, Signature>,
|
pub signature_methods: TemplateBuildMethodFnMap<'a, L, Signature>,
|
||||||
timestamp_methods: TemplateBuildMethodFnMap<'a, L, Timestamp>,
|
pub timestamp_methods: TemplateBuildMethodFnMap<'a, L, Timestamp>,
|
||||||
timestamp_range_methods: TemplateBuildMethodFnMap<'a, L, TimestampRange>,
|
pub timestamp_range_methods: TemplateBuildMethodFnMap<'a, L, TimestampRange>,
|
||||||
// TODO: add global functions table?
|
// TODO: add global functions table?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn merge_fn_map<'a, L: TemplateLanguage<'a>, T>(
|
||||||
|
base: &mut TemplateBuildMethodFnMap<'a, L, T>,
|
||||||
|
extension: TemplateBuildMethodFnMap<'a, L, T>,
|
||||||
|
) {
|
||||||
|
for (name, function) in extension {
|
||||||
|
if base.insert(name, function).is_some() {
|
||||||
|
panic!("Conflicting template definitions for '{name}' function");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a, L: TemplateLanguage<'a>> CoreTemplateBuildFnTable<'a, L> {
|
impl<'a, L: TemplateLanguage<'a>> CoreTemplateBuildFnTable<'a, L> {
|
||||||
/// Creates new symbol table containing the builtin methods.
|
/// Creates new symbol table containing the builtin methods.
|
||||||
pub fn builtin() -> Self {
|
pub fn builtin() -> Self {
|
||||||
|
@ -268,6 +279,35 @@ impl<'a, L: TemplateLanguage<'a>> CoreTemplateBuildFnTable<'a, L> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn empty() -> Self {
|
||||||
|
CoreTemplateBuildFnTable {
|
||||||
|
string_methods: HashMap::new(),
|
||||||
|
boolean_methods: HashMap::new(),
|
||||||
|
integer_methods: HashMap::new(),
|
||||||
|
signature_methods: HashMap::new(),
|
||||||
|
timestamp_methods: HashMap::new(),
|
||||||
|
timestamp_range_methods: HashMap::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn merge(&mut self, extension: CoreTemplateBuildFnTable<'a, L>) {
|
||||||
|
let CoreTemplateBuildFnTable {
|
||||||
|
string_methods,
|
||||||
|
boolean_methods,
|
||||||
|
integer_methods,
|
||||||
|
signature_methods,
|
||||||
|
timestamp_methods,
|
||||||
|
timestamp_range_methods,
|
||||||
|
} = extension;
|
||||||
|
|
||||||
|
merge_fn_map(&mut self.string_methods, string_methods);
|
||||||
|
merge_fn_map(&mut self.boolean_methods, boolean_methods);
|
||||||
|
merge_fn_map(&mut self.integer_methods, integer_methods);
|
||||||
|
merge_fn_map(&mut self.signature_methods, signature_methods);
|
||||||
|
merge_fn_map(&mut self.timestamp_methods, timestamp_methods);
|
||||||
|
merge_fn_map(&mut self.timestamp_range_methods, timestamp_range_methods);
|
||||||
|
}
|
||||||
|
|
||||||
/// Applies the method call node `function` to the given `property` by using
|
/// Applies the method call node `function` to the given `property` by using
|
||||||
/// this symbol table.
|
/// this symbol table.
|
||||||
pub fn build_method(
|
pub fn build_method(
|
||||||
|
|
Loading…
Reference in a new issue