Skip to main content

rib/compiler/
worker_functions_in_rib.rs

1use crate::wit_type::WitType;
2use crate::{ComponentDependency, FunctionName, InferredExpr, RibCompilationError};
3
4// An easier data type that focus just on the side effecting function calls in Rib script.
5// These will not include variant or enum calls, that were originally
6// tagged as functions before compilation.
7// This is why we need a fully inferred Rib (fully compiled rib),
8// which has specific details, along with original type registry to construct this data.
9// These function calls are indeed worker invoke calls and nothing else.
10// If Rib has inbuilt function support, those will not be included here either.
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct WorkerFunctionsInRib {
13    pub function_calls: Vec<WorkerFunctionType>,
14}
15
16impl WorkerFunctionsInRib {
17    pub fn from_inferred_expr(
18        inferred_expr: &InferredExpr,
19        component_dependency: &ComponentDependency,
20    ) -> Result<Option<WorkerFunctionsInRib>, RibCompilationError> {
21        let worker_invoke_registry_keys = inferred_expr.worker_invoke_registry_keys();
22
23        let mut function_calls = vec![];
24
25        for key in worker_invoke_registry_keys {
26            let (_, function_type) = component_dependency
27                .get_function_type(&key)
28                .map_err(|e| RibCompilationError::RibStaticAnalysisError(e.to_string()))?;
29
30            let function_call_in_rib = WorkerFunctionType {
31                function_name: key,
32                parameter_types: function_type
33                    .parameter_types
34                    .iter()
35                    .map(|param| WitType::try_from(param).unwrap())
36                    .collect(),
37                return_type: function_type
38                    .return_type
39                    .as_ref()
40                    .map(|return_type| WitType::try_from(return_type).unwrap()),
41            };
42
43            function_calls.push(function_call_in_rib)
44        }
45
46        if function_calls.is_empty() {
47            Ok(None)
48        } else {
49            Ok(Some(WorkerFunctionsInRib { function_calls }))
50        }
51    }
52}
53
54// The type of a function call with worker (ephmeral or durable) in Rib script
55#[derive(Debug, Clone, PartialEq, Eq)]
56pub struct WorkerFunctionType {
57    pub function_name: FunctionName,
58    pub parameter_types: Vec<WitType>,
59    pub return_type: Option<WitType>,
60}