interpreter_interface/
lib.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 fluence::fce;
18
19use fluence_it_types::IValue;
20use serde::Deserialize;
21use serde::Serialize;
22
23pub const AQUA_INTERPRETER_SUCCESS: i32 = 0;
24
25/// Describes a result returned at the end of the interpreter execution.
26#[fce]
27#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
28pub struct InterpreterOutcome {
29    /// A return code, where AQUA_INTERPRETER_SUCCESS means success.
30    pub ret_code: i32,
31
32    /// Contains error message if ret_code != AQUA_INTERPRETER_SUCCESS.
33    pub error_message: String,
34
35    /// Contains script data that should be preserved in an executor of this interpreter
36    /// regardless of ret_code value.
37    pub data: Vec<u8>,
38
39    /// Public keys of peers that should receive data.
40    pub next_peer_pks: Vec<String>,
41}
42
43impl InterpreterOutcome {
44    pub fn from_ivalues(mut ivalues: Vec<IValue>) -> Result<Self, String> {
45        const OUTCOME_FIELDS_COUNT: usize = 4;
46
47        let record_values = match ivalues.remove(0) {
48            IValue::Record(record_values) => record_values,
49            v => {
50                return Err(format!(
51                    "expected record for InterpreterOutcome, got {:?}",
52                    v
53                ))
54            }
55        };
56
57        let mut record_values = record_values.into_vec();
58        if record_values.len() != OUTCOME_FIELDS_COUNT {
59            return Err(format!(
60                "expected InterpreterOutcome struct with {} fields, got {:?}",
61                OUTCOME_FIELDS_COUNT, record_values
62            ));
63        }
64
65        let ret_code = match record_values.remove(0) {
66            IValue::S32(ret_code) => ret_code,
67            v => return Err(format!("expected i32 for ret_code, got {:?}", v)),
68        };
69
70        let error_message = match record_values.remove(0) {
71            IValue::String(str) => str,
72            v => return Err(format!("expected string for data, got {:?}", v)),
73        };
74
75        let data = match record_values.remove(0) {
76            IValue::Array(array) => {
77                let array: Result<Vec<_>, _> = array
78                    .into_iter()
79                    .map(|v| match v {
80                        IValue::U8(byte) => Ok(byte),
81                        v => Err(format!("expected a byte, got {:?}", v)),
82                    })
83                    .collect();
84                array?
85            }
86            v => return Err(format!("expected Vec<u8> for data, got {:?}", v)),
87        };
88
89        let next_peer_pks = match record_values.remove(0) {
90            IValue::Array(ar_values) => {
91                let array = ar_values
92                    .into_iter()
93                    .map(|v| match v {
94                        IValue::String(str) => Ok(str),
95                        v => Err(format!("expected string for next_peer_pks, got {:?}", v)),
96                    })
97                    .collect::<Result<Vec<String>, _>>()?;
98
99                Ok(array)
100            }
101            v => Err(format!("expected array for next_peer_pks, got {:?}", v)),
102        }?;
103
104        let outcome = Self {
105            ret_code,
106            error_message,
107            data,
108            next_peer_pks,
109        };
110
111        Ok(outcome)
112    }
113}