use crate::SecurityTetraplet;
use marine_wasm_backend_traits::WasmBackend;
use marine_core::generic::HostImportDescriptor;
use wasmer_it::IValue;
use wasmer_it::IType;
use parking_lot::Mutex;
use serde::Serialize;
use std::ops::Deref;
use std::sync::Arc;
pub(crate) fn create_call_parameters_import<WB: WasmBackend, CP: Serialize + Send + 'static>(
call_parameters: Arc<Mutex<CP>>, ) -> HostImportDescriptor<WB> {
let call_parameters_closure = move |_ctx: &mut <WB as WasmBackend>::ImportCallContext<'_>,
_args: Vec<IValue>| {
let result = {
crate::to_interface_value(call_parameters.lock().deref())
.unwrap_or_else(|_| panic!("CallParameters should be convertible to IValue"))
};
Some(result)
};
HostImportDescriptor {
host_exported_func: Box::new(call_parameters_closure),
argument_types: vec![],
output_type: Some(IType::Record(0)),
error_handler: None,
}
}
pub(crate) fn call_parameters_v3_to_v0(
call_parameters: marine_rs_sdk::CallParameters,
) -> marine_call_parameters_v0::CallParameters {
let marine_rs_sdk::CallParameters {
particle,
service_id,
service_creator_peer_id,
host_id,
tetraplets,
..
} = call_parameters;
marine_call_parameters_v0::CallParameters {
init_peer_id: particle.init_peer_id,
service_id,
service_creator_peer_id,
host_id,
particle_id: particle.id,
tetraplets: to_v0_sdk_tetraplets(tetraplets),
}
}
pub(crate) fn call_parameters_v3_to_v1(
call_parameters: marine_rs_sdk::CallParameters,
) -> marine_call_parameters_v1::CallParameters {
let marine_rs_sdk::CallParameters {
particle,
service_id,
service_creator_peer_id,
host_id,
tetraplets,
worker_id,
} = call_parameters;
marine_call_parameters_v1::CallParameters {
init_peer_id: particle.init_peer_id,
service_id,
service_creator_peer_id,
host_id,
worker_id,
particle_id: particle.id,
tetraplets: to_v1_sdk_tetraplets(tetraplets),
}
}
pub(crate) fn call_parameters_v3_to_v2(
call_parameters: marine_rs_sdk::CallParameters,
) -> marine_call_parameters_v2::CallParameters {
let marine_rs_sdk::CallParameters {
particle,
service_id,
service_creator_peer_id,
host_id,
tetraplets,
worker_id,
} = call_parameters;
marine_call_parameters_v2::CallParameters {
particle: marine_call_parameters_v2::ParticleParameters {
id: particle.id,
init_peer_id: particle.init_peer_id,
signature: particle.signature,
timestamp: particle.timestamp,
ttl: particle.ttl,
script: particle.script,
token: particle.token,
},
service_id,
service_creator_peer_id,
host_id,
worker_id,
tetraplets: to_v2_sdk_tetraplets(tetraplets),
}
}
fn to_v0_sdk_tetraplets(
tetraplets: Vec<Vec<SecurityTetraplet>>,
) -> Vec<Vec<marine_call_parameters_v0::SecurityTetraplet>> {
tetraplets
.into_iter()
.map(|tetraplets| tetraplets.into_iter().map(to_v0_sdk_tetraplet).collect())
.collect()
}
fn to_v0_sdk_tetraplet(
tetraplet: SecurityTetraplet,
) -> marine_call_parameters_v0::SecurityTetraplet {
let SecurityTetraplet {
peer_pk,
service_id,
function_name,
lens,
} = tetraplet;
marine_call_parameters_v0::SecurityTetraplet {
peer_pk,
service_id,
function_name,
json_path: lens,
}
}
fn to_v1_sdk_tetraplets(
tetraplets: Vec<Vec<SecurityTetraplet>>,
) -> Vec<Vec<marine_call_parameters_v1::SecurityTetraplet>> {
tetraplets
.into_iter()
.map(|tetraplets| tetraplets.into_iter().map(to_v1_sdk_tetraplet).collect())
.collect()
}
fn to_v1_sdk_tetraplet(
tetraplet: SecurityTetraplet,
) -> marine_call_parameters_v1::SecurityTetraplet {
let SecurityTetraplet {
peer_pk,
service_id,
function_name,
lens,
} = tetraplet;
marine_call_parameters_v1::SecurityTetraplet {
peer_pk,
service_id,
function_name,
json_path: lens,
}
}
fn to_v2_sdk_tetraplets(
tetraplets: Vec<Vec<SecurityTetraplet>>,
) -> Vec<Vec<marine_call_parameters_v2::SecurityTetraplet>> {
tetraplets
.into_iter()
.map(|tetraplets| tetraplets.into_iter().map(to_v2_sdk_tetraplet).collect())
.collect()
}
fn to_v2_sdk_tetraplet(
tetraplet: SecurityTetraplet,
) -> marine_call_parameters_v2::SecurityTetraplet {
let SecurityTetraplet {
peer_pk,
service_id,
function_name,
lens,
} = tetraplet;
marine_call_parameters_v2::SecurityTetraplet {
peer_pk,
service_id,
function_name,
lambda: lens,
}
}