lunatic_distributed/distributed/
message.rs

1use bytes::Bytes;
2use serde::{Deserialize, Serialize};
3
4#[derive(Clone, Debug, Serialize, Deserialize)]
5pub enum Request {
6    Spawn(Spawn),
7    Message {
8        environment_id: u64,
9        process_id: u64,
10        tag: Option<i64>,
11        data: Vec<u8>,
12    },
13}
14
15impl Request {
16    pub fn kind(&self) -> &'static str {
17        match self {
18            Request::Spawn(_) => "Spawn",
19            Request::Message { .. } => "Message",
20        }
21    }
22}
23
24#[derive(Clone, Debug, Serialize, Deserialize)]
25pub struct Spawn {
26    pub environment_id: u64,
27    pub module_id: u64,
28    pub function: String,
29    pub params: Vec<Val>,
30    pub config: Vec<u8>,
31}
32
33#[derive(Clone, Debug, Serialize, Deserialize)]
34pub enum ClientError {
35    Unexpected(String),
36    Connection(String),
37    NodeNotFound,
38    ModuleNotFound,
39    ProcessNotFound,
40}
41
42impl Default for ClientError {
43    fn default() -> Self {
44        Self::Unexpected("Unexpected error.".to_string())
45    }
46}
47
48#[derive(Clone, Debug, Serialize, Deserialize)]
49pub enum Response {
50    Spawned(u64),
51    Sent,
52    Linked,
53    Error(ClientError),
54}
55
56impl Response {
57    pub fn kind(&self) -> &'static str {
58        match self {
59            Response::Spawned(_) => "Spawned",
60            Response::Sent => "Sent",
61            Response::Linked => "Linked",
62            Response::Error(_) => "Error",
63        }
64    }
65}
66
67#[derive(Clone, Debug, Serialize, Deserialize)]
68pub enum Val {
69    I32(i32),
70    I64(i64),
71    V128(u128),
72}
73
74#[allow(clippy::from_over_into)]
75impl Into<wasmtime::Val> for Val {
76    fn into(self) -> wasmtime::Val {
77        match self {
78            Val::I32(v) => wasmtime::Val::I32(v),
79            Val::I64(v) => wasmtime::Val::I64(v),
80            Val::V128(v) => wasmtime::Val::V128(v),
81        }
82    }
83}
84
85pub fn pack_response(msg_id: u64, resp: Response) -> [Bytes; 2] {
86    let data = rmp_serde::to_vec(&(msg_id, resp)).unwrap();
87    let size = (data.len() as u32).to_le_bytes();
88    let size: Bytes = Bytes::copy_from_slice(&size[..]);
89    let bytes: Bytes = data.into();
90    [size, bytes]
91}