marine_module_interface/interface/
interface_transformer.rs

1/*
2 * Copyright 2021 Fluence Labs Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use super::ModuleInterface;
18use super::InterfaceResult;
19use super::FunctionSignature;
20use super::records_transformer::RecordsTransformer;
21use crate::it_interface::IModuleInterface;
22use crate::it_interface::IFunctionSignature;
23use crate::it_interface::IRecordTypes;
24
25pub fn it_to_module_interface(mm_interface: IModuleInterface) -> InterfaceResult<ModuleInterface> {
26    let record_types = mm_interface.export_record_types;
27
28    let function_signatures = mm_interface
29        .function_signatures
30        .into_iter()
31        .map(|sign| serialize_function_signature(sign, &record_types))
32        .collect();
33
34    let record_types = RecordsTransformer::transform(&record_types)?;
35
36    let interface = ModuleInterface {
37        function_signatures,
38        record_types,
39    };
40
41    Ok(interface)
42}
43
44fn serialize_function_signature(
45    signature: IFunctionSignature,
46    record_types: &IRecordTypes,
47) -> FunctionSignature {
48    use super::itype_text_view;
49
50    let arguments = signature
51        .arguments
52        .iter()
53        .map(|arg| (arg.name.clone(), itype_text_view(&arg.ty, record_types)))
54        .collect();
55
56    let output_types = signature
57        .outputs
58        .iter()
59        .map(|itype| itype_text_view(itype, record_types))
60        .collect();
61
62    FunctionSignature {
63        name: signature.name.to_string(),
64        arguments,
65        output_types,
66    }
67}