Skip to main content

rib/compiler/
worker_functions_in_rib.rs

1// Copyright 2024-2025 Golem Cloud
2//
3// Licensed under the Golem Source License v1.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://license.golem.cloud/LICENSE
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::wit_type::WitType;
16use crate::{ComponentDependency, FunctionName, InferredExpr, RibCompilationError};
17
18// An easier data type that focus just on the side effecting function calls in Rib script.
19// These will not include variant or enum calls, that were originally
20// tagged as functions before compilation.
21// This is why we need a fully inferred Rib (fully compiled rib),
22// which has specific details, along with original type registry to construct this data.
23// These function calls are indeed worker invoke calls and nothing else.
24// If Rib has inbuilt function support, those will not be included here either.
25#[derive(Debug, Clone, PartialEq, Eq)]
26pub struct WorkerFunctionsInRib {
27    pub function_calls: Vec<WorkerFunctionType>,
28}
29
30impl WorkerFunctionsInRib {
31    pub fn from_inferred_expr(
32        inferred_expr: &InferredExpr,
33        component_dependency: &ComponentDependency,
34    ) -> Result<Option<WorkerFunctionsInRib>, RibCompilationError> {
35        let worker_invoke_registry_keys = inferred_expr.worker_invoke_registry_keys();
36
37        let mut function_calls = vec![];
38
39        for key in worker_invoke_registry_keys {
40            let (_, function_type) = component_dependency
41                .get_function_type(&key)
42                .map_err(|e| RibCompilationError::RibStaticAnalysisError(e.to_string()))?;
43
44            let function_call_in_rib = WorkerFunctionType {
45                function_name: key,
46                parameter_types: function_type
47                    .parameter_types
48                    .iter()
49                    .map(|param| WitType::try_from(param).unwrap())
50                    .collect(),
51                return_type: function_type
52                    .return_type
53                    .as_ref()
54                    .map(|return_type| WitType::try_from(return_type).unwrap()),
55            };
56
57            function_calls.push(function_call_in_rib)
58        }
59
60        if function_calls.is_empty() {
61            Ok(None)
62        } else {
63            Ok(Some(WorkerFunctionsInRib { function_calls }))
64        }
65    }
66}
67
68// The type of a function call with worker (ephmeral or durable) in Rib script
69#[derive(Debug, Clone, PartialEq, Eq)]
70pub struct WorkerFunctionType {
71    pub function_name: FunctionName,
72    pub parameter_types: Vec<WitType>,
73    pub return_type: Option<WitType>,
74}