1use async_trait::async_trait;
2use serde::de::DeserializeOwned;
3use serde::Deserialize;
4use serde::Serialize;
5use std::fmt::Debug;
6
7#[derive(Serialize, Deserialize, Debug, Clone)]
8pub struct Message {
9 pub request_id: u64,
10 pub response_id: Option<u64>,
11 pub msg: Vec<u8>,
12}
13
14impl Message {
15 pub fn to_bytes(&self) -> Vec<u8> {
16 serde_cbor::to_vec(self).unwrap()
17 }
18
19 pub fn from_bytes(bytes: &[u8]) -> Self {
20 serde_cbor::from_slice(bytes).unwrap()
21 }
22}
23
24#[async_trait]
25pub trait ProtoTrait {
26 type Response: ProtoTrait + Send + Serialize + DeserializeOwned + Debug;
27 type Client;
28
29 async fn dispatch(self, _request_id: u64, _client: Self::Client) -> Option<Self::Response>
30 where
31 Self: Sized,
32 Self::Client: Send,
33 {
34 None
35 }
36
37 fn from_bytes(bytes: &[u8]) -> Self
38 where
39 Self: Sized + DeserializeOwned,
40 {
41 serde_cbor::from_slice(bytes).unwrap()
42 }
43
44 fn to_bytes(&self) -> Vec<u8>
45 where
46 Self: Sized + Serialize,
47 {
48 serde_cbor::to_vec(self).unwrap()
49 }
50}