2021-05-16 22:52:22 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2020 Fluence Labs Limited
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-08-24 16:14:15 +03:00
|
|
|
use super::ApResult;
|
|
|
|
use super::CallResult;
|
2022-08-26 00:43:43 +03:00
|
|
|
use super::CanonResult;
|
2021-08-24 16:14:15 +03:00
|
|
|
use super::ExecutedState;
|
2021-05-16 22:52:22 +03:00
|
|
|
use super::JValue;
|
2021-08-24 16:14:15 +03:00
|
|
|
use super::ParResult;
|
2021-10-04 10:58:00 +03:00
|
|
|
use super::Sender;
|
2022-06-10 08:29:56 +03:00
|
|
|
use super::TracePos;
|
2022-12-26 15:45:14 +07:00
|
|
|
use super::ValueRef;
|
2021-08-24 16:14:15 +03:00
|
|
|
use crate::FoldLore;
|
|
|
|
use crate::FoldResult;
|
|
|
|
use crate::FoldSubTraceLore;
|
|
|
|
use crate::SubTraceDesc;
|
2021-05-16 22:52:22 +03:00
|
|
|
|
2022-12-26 15:45:14 +07:00
|
|
|
use air_interpreter_cid::value_to_json_cid;
|
|
|
|
use air_interpreter_data::CidTracker;
|
|
|
|
|
2021-05-16 22:52:22 +03:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
2021-08-24 16:14:15 +03:00
|
|
|
pub fn scalar(result: JValue) -> ExecutedState {
|
2022-12-26 15:45:14 +07:00
|
|
|
let cid = value_to_json_cid(&result)
|
|
|
|
.unwrap_or_else(|e| panic!("{:?}: failed to compute CID of {:?}", e, result));
|
|
|
|
let value = ValueRef::Scalar(Rc::new(cid));
|
|
|
|
ExecutedState::Call(CallResult::Executed(value))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn scalar_tracked(result: impl Into<JValue>, tracker: &mut CidTracker) -> ExecutedState {
|
|
|
|
let cid = tracker.record_value(Rc::new(result.into())).unwrap();
|
|
|
|
let value = ValueRef::Scalar(cid);
|
2021-08-24 16:14:15 +03:00
|
|
|
ExecutedState::Call(CallResult::Executed(value))
|
2021-05-16 22:52:22 +03:00
|
|
|
}
|
|
|
|
|
2021-11-24 17:57:14 +03:00
|
|
|
pub fn scalar_number(result: impl Into<serde_json::Number>) -> ExecutedState {
|
|
|
|
let result = JValue::Number(result.into());
|
|
|
|
|
2022-12-26 15:45:14 +07:00
|
|
|
scalar(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stream_call_result(result: JValue, generation: u32) -> CallResult {
|
|
|
|
let cid = value_to_json_cid(&result)
|
|
|
|
.unwrap_or_else(|e| panic!("{:?}: failed to compute CID of {:?}", e, result));
|
|
|
|
CallResult::executed_stream(Rc::new(cid), generation)
|
2021-11-24 17:57:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stream(result: JValue, generation: u32) -> ExecutedState {
|
2022-12-26 15:45:14 +07:00
|
|
|
ExecutedState::Call(stream_call_result(result, generation))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stream_tracked(
|
|
|
|
value: impl Into<JValue>,
|
|
|
|
generation: u32,
|
|
|
|
tracker: &mut CidTracker,
|
|
|
|
) -> ExecutedState {
|
|
|
|
let cid = tracker.record_value(Rc::new(value.into())).unwrap();
|
|
|
|
ExecutedState::Call(CallResult::executed_stream(cid, generation))
|
2021-05-16 22:52:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn scalar_string(result: impl Into<String>) -> ExecutedState {
|
2021-08-24 16:14:15 +03:00
|
|
|
let result = JValue::String(result.into());
|
2022-12-26 15:45:14 +07:00
|
|
|
scalar(result)
|
2021-05-16 22:52:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn scalar_string_array(result: Vec<impl Into<String>>) -> ExecutedState {
|
|
|
|
let result = result
|
|
|
|
.into_iter()
|
|
|
|
.map(|s| JValue::String(s.into()))
|
|
|
|
.collect::<Vec<_>>();
|
2022-12-26 15:45:14 +07:00
|
|
|
let value = JValue::Array(result);
|
2021-05-16 22:52:22 +03:00
|
|
|
|
2022-12-26 15:45:14 +07:00
|
|
|
scalar(value)
|
2021-05-16 22:52:22 +03:00
|
|
|
}
|
|
|
|
|
2021-08-24 16:14:15 +03:00
|
|
|
pub fn stream_string(result: impl Into<String>, generation: u32) -> ExecutedState {
|
|
|
|
let result = JValue::String(result.into());
|
|
|
|
|
2022-12-26 15:45:14 +07:00
|
|
|
stream(result, generation)
|
2021-05-16 22:52:22 +03:00
|
|
|
}
|
|
|
|
|
2021-08-24 16:14:15 +03:00
|
|
|
pub fn stream_number(result: impl Into<serde_json::Number>, generation: u32) -> ExecutedState {
|
|
|
|
let result = JValue::Number(result.into());
|
|
|
|
|
2022-12-26 15:45:14 +07:00
|
|
|
stream(result, generation)
|
2021-05-16 22:52:22 +03:00
|
|
|
}
|
|
|
|
|
2021-08-24 16:14:15 +03:00
|
|
|
pub fn stream_string_array(result: Vec<impl Into<String>>, generation: u32) -> ExecutedState {
|
2021-05-16 22:52:22 +03:00
|
|
|
let result = result
|
|
|
|
.into_iter()
|
|
|
|
.map(|s| JValue::String(s.into()))
|
|
|
|
.collect::<Vec<_>>();
|
2022-12-26 15:45:14 +07:00
|
|
|
let value = JValue::Array(result);
|
2021-05-16 22:52:22 +03:00
|
|
|
|
2022-12-26 15:45:14 +07:00
|
|
|
stream(value, generation)
|
2021-05-16 22:52:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn request_sent_by(sender: impl Into<String>) -> ExecutedState {
|
2021-10-04 10:58:00 +03:00
|
|
|
ExecutedState::Call(CallResult::RequestSentBy(Sender::PeerId(Rc::new(
|
|
|
|
sender.into(),
|
|
|
|
))))
|
2021-05-16 22:52:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn par(left: usize, right: usize) -> ExecutedState {
|
2021-08-24 16:14:15 +03:00
|
|
|
let par_result = ParResult {
|
|
|
|
left_size: left as _,
|
|
|
|
right_size: right as _,
|
|
|
|
};
|
|
|
|
|
|
|
|
ExecutedState::Par(par_result)
|
2021-05-16 22:52:22 +03:00
|
|
|
}
|
|
|
|
|
2021-11-24 18:47:49 +03:00
|
|
|
pub fn service_failed(ret_code: i32, error_message: &str) -> ExecutedState {
|
2021-05-16 22:52:22 +03:00
|
|
|
ExecutedState::Call(CallResult::CallServiceFailed(
|
|
|
|
ret_code,
|
2022-12-12 22:37:05 +07:00
|
|
|
Rc::new(format!(r#""{error_message}""#)),
|
2021-05-16 22:52:22 +03:00
|
|
|
))
|
|
|
|
}
|
2021-08-24 16:14:15 +03:00
|
|
|
|
|
|
|
pub fn fold(lore: FoldLore) -> ExecutedState {
|
|
|
|
let result = FoldResult { lore };
|
|
|
|
ExecutedState::Fold(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn subtrace_lore(
|
2022-06-10 08:29:56 +03:00
|
|
|
value_pos: usize,
|
2021-08-24 16:14:15 +03:00
|
|
|
before: SubTraceDesc,
|
|
|
|
after: SubTraceDesc,
|
|
|
|
) -> FoldSubTraceLore {
|
|
|
|
FoldSubTraceLore {
|
2022-06-10 08:29:56 +03:00
|
|
|
value_pos: value_pos.into(),
|
2021-08-24 16:14:15 +03:00
|
|
|
subtraces_desc: vec![before, after],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-10 08:29:56 +03:00
|
|
|
pub fn subtrace_desc(begin_pos: impl Into<TracePos>, subtrace_len: u32) -> SubTraceDesc {
|
|
|
|
SubTraceDesc {
|
|
|
|
begin_pos: begin_pos.into(),
|
|
|
|
subtrace_len,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-07 14:38:29 +03:00
|
|
|
pub fn ap(generation: u32) -> ExecutedState {
|
|
|
|
let ap_result = ApResult::new(generation);
|
2021-08-24 16:14:15 +03:00
|
|
|
ExecutedState::Ap(ap_result)
|
|
|
|
}
|
|
|
|
|
2022-10-10 22:15:28 +03:00
|
|
|
pub fn canon(canonicalized_element: JValue) -> ExecutedState {
|
|
|
|
let canon_result = CanonResult::new(canonicalized_element);
|
2022-08-26 00:43:43 +03:00
|
|
|
ExecutedState::Canon(canon_result)
|
|
|
|
}
|