From 6e5eff5423ce8d39553f81ae55751c6ce248f487 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Thu, 22 Feb 2024 18:25:19 +0900 Subject: [PATCH] templater: update test templater to be compatible with "self" methods Prepares for the removal of build_keyword(). --- cli/src/template_builder.rs | 56 ++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/cli/src/template_builder.rs b/cli/src/template_builder.rs index 13933158b6..075a5c21dd 100644 --- a/cli/src/template_builder.rs +++ b/cli/src/template_builder.rs @@ -956,9 +956,9 @@ mod tests { impl TemplateLanguage<'static> for TestTemplateLanguage { type Context = (); - type Property = CoreTemplatePropertyKind<'static, ()>; + type Property = TestTemplatePropertyKind; - impl_core_wrap_property_fns!('static); + impl_core_wrap_property_fns!('static, TestTemplatePropertyKind::Core); fn build_keyword( &self, @@ -977,11 +977,59 @@ mod tests { property: Self::Property, function: &FunctionCallNode, ) -> TemplateParseResult { - build_core_method(self, build_ctx, property, function) + match property { + TestTemplatePropertyKind::Core(property) => { + build_core_method(self, build_ctx, property, function) + } + TestTemplatePropertyKind::Unit => { + let build = self + .keywords + .get(function.name) + .ok_or_else(|| TemplateParseError::no_such_method("()", function))?; + template_parser::expect_no_arguments(function)?; + Ok(build(self)) + } + } + } + } + + enum TestTemplatePropertyKind { + Core(CoreTemplatePropertyKind<'static, ()>), + #[allow(unused)] // TODO + Unit, + } + + impl IntoTemplateProperty<'static, ()> for TestTemplatePropertyKind { + fn try_into_boolean(self) -> Option>> { + match self { + TestTemplatePropertyKind::Core(property) => property.try_into_boolean(), + TestTemplatePropertyKind::Unit => None, + } + } + + fn try_into_integer(self) -> Option>> { + match self { + TestTemplatePropertyKind::Core(property) => property.try_into_integer(), + TestTemplatePropertyKind::Unit => None, + } + } + + fn try_into_plain_text(self) -> Option>> { + match self { + TestTemplatePropertyKind::Core(property) => property.try_into_plain_text(), + TestTemplatePropertyKind::Unit => None, + } + } + + fn try_into_template(self) -> Option>> { + match self { + TestTemplatePropertyKind::Core(property) => property.try_into_template(), + TestTemplatePropertyKind::Unit => None, + } } } - type TestTemplateKeywordFn = fn(&TestTemplateLanguage) -> CoreTemplatePropertyKind<'static, ()>; + type TestTemplateKeywordFn = fn(&TestTemplateLanguage) -> TestTemplatePropertyKind; /// Helper to set up template evaluation environment. #[derive(Clone, Default)]