photon_protocol/codec/
mod.rs1pub mod postcard;
2
3pub use self::postcard::PostcardCodec;
4
5use bytes::BytesMut;
6use serde::{Serialize, de::DeserializeOwned};
7
8use crate::ports::codec::{Codec, CodecError};
9
10#[derive(Clone, Copy, Debug, Default)]
11pub enum CodecKind {
12 #[default]
13 Postcard,
14}
15
16impl CodecKind {
17 pub fn name(&self) -> &'static str {
18 match self {
19 Self::Postcard => "postcard",
20 }
21 }
22
23 pub fn all_variants() -> Vec<Self> {
24 vec![Self::Postcard]
25 }
26}
27
28impl<T> Codec<T> for CodecKind
29where
30 T: Serialize + DeserializeOwned + Send + Sync,
31{
32 fn encode(&self, value: &T, output: &mut BytesMut) -> Result<(), CodecError> {
33 match self {
34 Self::Postcard => PostcardCodec.encode(value, output),
35 }
36 }
37
38 fn decode(&self, input: &[u8]) -> Result<T, CodecError> {
39 match self {
40 Self::Postcard => PostcardCodec.decode(input),
41 }
42 }
43}