actor_core_client/
encoding.rs1use anyhow::Result;
2
3use crate::protocol::ToServer;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum EncodingKind {
7 Json,
8 Cbor,
9}
10
11impl EncodingKind {
12 pub fn as_str(&self) -> &str {
13 match self {
14 EncodingKind::Json => "json",
15 EncodingKind::Cbor => "cbor",
16 }
17 }
18
19 pub fn get_default_serializer(&self) -> fn(&ToServer) -> Result<Vec<u8>> {
20 match self {
21 EncodingKind::Json => json_serialize,
22 EncodingKind::Cbor => cbor_serialize,
23 }
24 }
25}
26
27fn json_serialize(value: &ToServer) -> Result<Vec<u8>> {
28 let msg = serde_json::to_vec(value)?;
29
30 Ok(msg)
31}
32
33fn cbor_serialize(msg: &ToServer) -> Result<Vec<u8>> {
34 let msg = serde_cbor::to_vec(msg)?;
35
36 Ok(msg)
37}