Skip to content

Commit

Permalink
templater: add Operation type and methods
Browse files Browse the repository at this point in the history
There are no keywords or methods that return Operation yet, but we might add
"self" keyword that returns the context object.
  • Loading branch information
yuja committed Feb 23, 2024
1 parent 4e0fa66 commit e9b420f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
28 changes: 28 additions & 0 deletions cli/src/operation_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ impl TemplateLanguage<'static> for OperationTemplateLanguage<'_> {
OperationTemplatePropertyKind::Core(property) => {
template_builder::build_core_method(self, build_ctx, property, function)
}
OperationTemplatePropertyKind::Operation(property) => {
build_operation_method(self, build_ctx, property, function)
}
OperationTemplatePropertyKind::OperationId(property) => {
build_operation_id_method(self, build_ctx, property, function)
}
Expand All @@ -64,6 +67,14 @@ impl TemplateLanguage<'static> for OperationTemplateLanguage<'_> {
}

impl OperationTemplateLanguage<'_> {
#[allow(unused)] // TODO
fn wrap_operation(
&self,
property: impl TemplateProperty<Operation, Output = Operation> + 'static,
) -> OperationTemplatePropertyKind {
OperationTemplatePropertyKind::Operation(Box::new(property))
}

fn wrap_operation_id(
&self,
property: impl TemplateProperty<Operation, Output = OperationId> + 'static,
Expand All @@ -74,13 +85,15 @@ impl OperationTemplateLanguage<'_> {

enum OperationTemplatePropertyKind {
Core(CoreTemplatePropertyKind<'static, Operation>),
Operation(Box<dyn TemplateProperty<Operation, Output = Operation>>),
OperationId(Box<dyn TemplateProperty<Operation, Output = OperationId>>),
}

impl IntoTemplateProperty<'static, Operation> for OperationTemplatePropertyKind {
fn try_into_boolean(self) -> Option<Box<dyn TemplateProperty<Operation, Output = bool>>> {
match self {
OperationTemplatePropertyKind::Core(property) => property.try_into_boolean(),
OperationTemplatePropertyKind::Operation(_) => None,
OperationTemplatePropertyKind::OperationId(_) => None,
}
}
Expand All @@ -105,6 +118,7 @@ impl IntoTemplateProperty<'static, Operation> for OperationTemplatePropertyKind
fn try_into_template(self) -> Option<Box<dyn Template<Operation>>> {
match self {
OperationTemplatePropertyKind::Core(property) => property.try_into_template(),
OperationTemplatePropertyKind::Operation(_) => None,
OperationTemplatePropertyKind::OperationId(property) => Some(property.into_template()),
}
}
Expand All @@ -122,6 +136,20 @@ fn build_operation_keyword(
.ok_or_else(|| TemplateParseError::no_such_keyword(name, span))
}

fn build_operation_method(
language: &OperationTemplateLanguage,
_build_ctx: &BuildContext<OperationTemplatePropertyKind>,
self_property: impl TemplateProperty<Operation, Output = Operation> + 'static,
function: &FunctionCallNode,
) -> TemplateParseResult<OperationTemplatePropertyKind> {
if let Some(property) = build_operation_keyword_opt(language, self_property, function.name) {
template_parser::expect_no_arguments(function)?;
Ok(property)
} else {
Err(TemplateParseError::no_such_method("Operation", function))
}
}

fn build_operation_keyword_opt(
language: &OperationTemplateLanguage,
property: impl TemplateProperty<Operation, Output = Operation> + 'static,
Expand Down
5 changes: 5 additions & 0 deletions docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ The following methods are defined. See also the `List` type.

* `.join(separator: Template) -> Template`

### Operation type

This type cannot be printed. All operation keywords are accessible as 0-argument
methods.

### OperationId type

The following methods are defined.
Expand Down

0 comments on commit e9b420f

Please sign in to comment.