rib/compiler/
compiler_output.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::compiler::worker_functions_in_rib::WorkerFunctionsInRib;
16use crate::{RibByteCode, RibInputTypeInfo, RibOutputTypeInfo};
17
18#[derive(Debug, Clone)]
19pub struct CompilerOutput {
20    pub worker_invoke_calls: Option<WorkerFunctionsInRib>,
21    pub byte_code: RibByteCode,
22    pub rib_input_type_info: RibInputTypeInfo,
23    // Optional to keep backward compatible as compiler output information
24    // for some existing Rib in persistence store doesn't have this info.
25    // This is optional mainly to support the proto conversions.
26    // At the API level, if we have access to expr, whenever this field is optional
27    // we can compile the expression again and get the output type info
28    pub rib_output_type_info: Option<RibOutputTypeInfo>,
29}
30
31#[cfg(feature = "protobuf")]
32mod protobuf {
33    use crate::{
34        CompilerOutput, RibByteCode, RibInputTypeInfo, RibOutputTypeInfo, WorkerFunctionsInRib,
35    };
36    use golem_api_grpc::proto::golem::rib::CompilerOutput as ProtoCompilerOutput;
37
38    impl TryFrom<ProtoCompilerOutput> for CompilerOutput {
39        type Error = String;
40
41        fn try_from(value: ProtoCompilerOutput) -> Result<Self, Self::Error> {
42            let proto_rib_input = value.rib_input.ok_or("Missing rib_input")?;
43            let proto_byte_code = value.byte_code.ok_or("Missing byte_code")?;
44            let rib_input = RibInputTypeInfo::try_from(proto_rib_input)?;
45            let byte_code = RibByteCode::try_from(proto_byte_code)?;
46            let worker_invoke_calls = if let Some(value) = value.worker_invoke_calls {
47                Some(WorkerFunctionsInRib::try_from(value)?)
48            } else {
49                None
50            };
51
52            let rib_output_type_info = value
53                .rib_output
54                .map(RibOutputTypeInfo::try_from)
55                .transpose()?;
56
57            Ok(CompilerOutput {
58                worker_invoke_calls,
59                byte_code,
60                rib_input_type_info: rib_input,
61                rib_output_type_info,
62            })
63        }
64    }
65
66    impl TryFrom<CompilerOutput> for ProtoCompilerOutput {
67        type Error = String;
68
69        fn try_from(value: CompilerOutput) -> Result<Self, Self::Error> {
70            Ok(ProtoCompilerOutput {
71                byte_code: Some(golem_api_grpc::proto::golem::rib::RibByteCode::try_from(
72                    value.byte_code,
73                )?),
74                rib_input: Some(golem_api_grpc::proto::golem::rib::RibInputType::from(
75                    value.rib_input_type_info,
76                )),
77                worker_invoke_calls: value
78                    .worker_invoke_calls
79                    .map(golem_api_grpc::proto::golem::rib::WorkerFunctionsInRib::from),
80
81                rib_output: value
82                    .rib_output_type_info
83                    .map(golem_api_grpc::proto::golem::rib::RibOutputType::from),
84            })
85        }
86    }
87}