rib-lang 0.0.0

Parser for Golem's Rib language
Documentation
// Copyright 2024-2025 Golem Cloud
//
// Licensed under the Golem Source License v1.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://license.golem.cloud/LICENSE
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::wit_type::WitType;
use crate::{ComponentDependency, FunctionName, InferredExpr, RibCompilationError};

// An easier data type that focus just on the side effecting function calls in Rib script.
// These will not include variant or enum calls, that were originally
// tagged as functions before compilation.
// This is why we need a fully inferred Rib (fully compiled rib),
// which has specific details, along with original type registry to construct this data.
// These function calls are indeed worker invoke calls and nothing else.
// If Rib has inbuilt function support, those will not be included here either.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WorkerFunctionsInRib {
    pub function_calls: Vec<WorkerFunctionType>,
}

impl WorkerFunctionsInRib {
    pub fn from_inferred_expr(
        inferred_expr: &InferredExpr,
        component_dependency: &ComponentDependency,
    ) -> Result<Option<WorkerFunctionsInRib>, RibCompilationError> {
        let worker_invoke_registry_keys = inferred_expr.worker_invoke_registry_keys();

        let mut function_calls = vec![];

        for key in worker_invoke_registry_keys {
            let (_, function_type) = component_dependency
                .get_function_type(&key)
                .map_err(|e| RibCompilationError::RibStaticAnalysisError(e.to_string()))?;

            let function_call_in_rib = WorkerFunctionType {
                function_name: key,
                parameter_types: function_type
                    .parameter_types
                    .iter()
                    .map(|param| WitType::try_from(param).unwrap())
                    .collect(),
                return_type: function_type
                    .return_type
                    .as_ref()
                    .map(|return_type| WitType::try_from(return_type).unwrap()),
            };

            function_calls.push(function_call_in_rib)
        }

        if function_calls.is_empty() {
            Ok(None)
        } else {
            Ok(Some(WorkerFunctionsInRib { function_calls }))
        }
    }
}

// The type of a function call with worker (ephmeral or durable) in Rib script
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WorkerFunctionType {
    pub function_name: FunctionName,
    pub parameter_types: Vec<WitType>,
    pub return_type: Option<WitType>,
}