air_interpreter_interface/
call_request_parameters.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 air_interpreter_sede::define_simple_representation;
18use air_interpreter_sede::derive_serialized_type;
19use air_interpreter_sede::Format;
20use air_interpreter_sede::FromSerialized;
21use air_interpreter_sede::MsgPackFormat;
22use air_interpreter_sede::MsgPackMultiformat;
23use air_interpreter_sede::Representation;
24use air_interpreter_value::JValue;
25
26use marine_call_parameters::SecurityTetraplet;
27#[cfg(feature = "marine")]
28use marine_rs_sdk::marine;
29use serde::Deserialize;
30use serde::Serialize;
31
32use std::collections::HashMap;
33use std::rc::Rc;
34
35pub type CallRequests = HashMap<u32, CallRequestParams>;
36
37derive_serialized_type!(SerializedCallArguments);
38derive_serialized_type!(SerializedTetraplets);
39derive_serialized_type!(SerializedCallRequests);
40
41pub type CallArgumentsFormat = MsgPackFormat;
42pub type TetrapletsFormat = MsgPackFormat;
43pub type CallRequestsFormat = MsgPackMultiformat;
44
45define_simple_representation! {
46    CallArgumentsRepr,
47    Vec<JValue>,
48    CallArgumentsFormat,
49    SerializedCallArguments
50}
51
52pub type CallArgumentsDeserializeError = <CallArgumentsRepr as Representation>::DeserializeError;
53
54impl FromSerialized<Vec<serde_json::Value>> for CallArgumentsRepr {
55    fn deserialize(&self, repr: &[u8]) -> Result<Vec<serde_json::Value>, Self::DeserializeError> {
56        Self.get_format().from_slice(repr)
57    }
58}
59
60define_simple_representation! {
61    TetrapletsRepr,
62    // additional implementation for Vec<Vec<SecurityTetraplet>> is defined below
63    // TODO allow this macro to define implementations for multiple types
64    Vec<Vec<Rc<SecurityTetraplet>>>,
65    TetrapletsFormat,
66    SerializedTetraplets
67}
68
69pub type TetrapletDeserializeError = <TetrapletsRepr as Representation>::DeserializeError;
70
71define_simple_representation! {
72    CallRequestsRepr,
73    CallRequests,
74    CallRequestsFormat,
75    SerializedCallRequests
76}
77
78pub type CallRequestsDeserializeError = <CallRequestsRepr as Representation>::DeserializeError;
79
80impl FromSerialized<Vec<Vec<SecurityTetraplet>>> for TetrapletsRepr {
81    fn deserialize(
82        &self,
83        repr: &[u8],
84    ) -> Result<Vec<Vec<SecurityTetraplet>>, Self::DeserializeError> {
85        Self.get_format().from_slice(repr)
86    }
87}
88
89/// Contains arguments of a call instruction and all other necessary information
90/// required for calling a service.
91#[cfg_attr(feature = "marine", marine)]
92#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
93pub struct CallRequestParams {
94    /// Id of a service that should be called.
95    pub service_id: String,
96
97    /// Name of a function from service identified by service_id that should be called.
98    pub function_name: String,
99
100    /// Serialized to JSON string Vec<JValue> of arguments that should be passed to a service.
101    pub arguments: SerializedCallArguments,
102
103    /// Serialized to JSON string Vec<Vec<SecurityTetraplet>> that should be passed to a service.
104    pub tetraplets: SerializedTetraplets,
105}
106
107impl CallRequestParams {
108    pub fn new(
109        service_id: String,
110        function_name: String,
111        arguments: SerializedCallArguments,
112        tetraplets: SerializedTetraplets,
113    ) -> Self {
114        Self {
115            service_id,
116            function_name,
117            arguments,
118            tetraplets,
119        }
120    }
121}