fluence_app_service/
service_interface.rs

1/*
2 * Copyright 2020 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 marine::MarineModuleInterface;
18use marine::MarineFunctionSignature;
19use marine::IRecordType;
20use marine::MRecordTypes;
21use marine::itype_text_view;
22
23use serde::Serialize;
24
25use std::sync::Arc;
26
27#[derive(Serialize)]
28pub struct FunctionSignature {
29    pub name: String,
30    pub arguments: Vec<(String, String)>,
31    pub output_types: Vec<String>,
32}
33
34#[derive(Serialize)]
35pub struct RecordType {
36    pub name: String,
37    pub id: u64,
38    pub fields: Vec<(String, String)>,
39}
40
41#[derive(Serialize)]
42pub struct ServiceInterface {
43    pub function_signatures: Vec<FunctionSignature>,
44    pub record_types: Vec<RecordType>,
45}
46
47pub(crate) fn into_service_interface(
48    marine_interface: MarineModuleInterface<'_>,
49) -> ServiceInterface {
50    let record_types = marine_interface.record_types;
51
52    let function_signatures = marine_interface
53        .function_signatures
54        .into_iter()
55        .map(|sign| serialize_function_signature(sign, record_types))
56        .collect();
57
58    let record_types = record_types
59        .iter()
60        .map(|(id, record)| serialize_record_type(*id, record.clone(), record_types))
61        .collect::<Vec<_>>();
62
63    ServiceInterface {
64        function_signatures,
65        record_types,
66    }
67}
68
69fn serialize_function_signature(
70    signature: MarineFunctionSignature,
71    record_types: &MRecordTypes,
72) -> FunctionSignature {
73    let arguments = signature
74        .arguments
75        .iter()
76        .map(|arg| (arg.name.clone(), itype_text_view(&arg.ty, record_types)))
77        .collect();
78
79    let output_types = signature
80        .outputs
81        .iter()
82        .map(|itype| itype_text_view(itype, record_types))
83        .collect();
84
85    FunctionSignature {
86        name: signature.name.to_string(),
87        arguments,
88        output_types,
89    }
90}
91
92fn serialize_record_type(
93    id: u64,
94    record: Arc<IRecordType>,
95    record_types: &MRecordTypes,
96) -> RecordType {
97    let fields = record
98        .fields
99        .iter()
100        .map(|field| (field.name.clone(), itype_text_view(&field.ty, record_types)))
101        .collect::<Vec<_>>();
102
103    RecordType {
104        name: record.name.clone(),
105        id,
106        fields,
107    }
108}