1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use bytes::Bytes;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum Request {
    Spawn(Spawn),
    Message {
        environment_id: u64,
        process_id: u64,
        tag: Option<i64>,
        data: Vec<u8>,
    },
}

impl Request {
    pub fn kind(&self) -> &'static str {
        match self {
            Request::Spawn(_) => "Spawn",
            Request::Message { .. } => "Message",
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Spawn {
    pub environment_id: u64,
    pub module_id: u64,
    pub function: String,
    pub params: Vec<Val>,
    pub config: Vec<u8>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum ClientError {
    Unexpected(String),
    Connection(String),
    NodeNotFound,
    ModuleNotFound,
    ProcessNotFound,
}

impl Default for ClientError {
    fn default() -> Self {
        Self::Unexpected("Unexpected error.".to_string())
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum Response {
    Spawned(u64),
    Sent,
    Linked,
    Error(ClientError),
}

impl Response {
    pub fn kind(&self) -> &'static str {
        match self {
            Response::Spawned(_) => "Spawned",
            Response::Sent => "Sent",
            Response::Linked => "Linked",
            Response::Error(_) => "Error",
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum Val {
    I32(i32),
    I64(i64),
    V128(u128),
}

#[allow(clippy::from_over_into)]
impl Into<wasmtime::Val> for Val {
    fn into(self) -> wasmtime::Val {
        match self {
            Val::I32(v) => wasmtime::Val::I32(v),
            Val::I64(v) => wasmtime::Val::I64(v),
            Val::V128(v) => wasmtime::Val::V128(v),
        }
    }
}

pub fn pack_response(msg_id: u64, resp: Response) -> [Bytes; 2] {
    let data = rmp_serde::to_vec(&(msg_id, resp)).unwrap();
    let size = (data.len() as u32).to_le_bytes();
    let size: Bytes = Bytes::copy_from_slice(&size[..]);
    let bytes: Bytes = data.into();
    [size, bytes]
}