1
Fork 0

encoder/decoder impl

This commit is contained in:
Andy Killorin 2025-02-03 13:13:02 -05:00
parent 60aa584f43
commit 8a972cd660
Signed by: ank
GPG key ID: 23F9463ECB67FE8C

View file

@ -0,0 +1,55 @@
use anyhow::anyhow;
use common::Command;
use framed::{BoxPayload, FRAME_END_SYMBOL};
use tokio_util::{bytes::BytesMut, codec::{Decoder, Encoder}};
pub struct FramedCodec {
codec: framed::bytes::Codec,
}
impl FramedCodec {
pub fn new() -> FramedCodec {
let codec = framed::bytes::Config::default().to_codec();
FramedCodec {codec}
}
}
impl Decoder for FramedCodec {
type Item = BoxPayload;
type Error = framed::Error;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
match src.iter().position(|e| *e==FRAME_END_SYMBOL) {
Some(len) => {
let data = src.split_to(len);
Ok(Some(self.codec.decode_to_box(&data)?))
}
None => {
Ok(None)
}
}
}
}
impl Encoder<Vec<u8>> for FramedCodec {
type Error = framed::Error;
fn encode(&mut self, item: Vec<u8>, dst: &mut BytesMut) -> Result<(), Self::Error> {
let encoded = self.codec.encode_to_box(&item)?;
dst.extend_from_slice(&encoded);
Ok(())
}
}
impl Encoder<Command> for FramedCodec {
type Error = anyhow::Error;
fn encode(&mut self, item: Command, dst: &mut BytesMut) -> Result<(), Self::Error> {
let data = postcard::to_stdvec(&item)?;
Ok(self.encode(data, dst).map_err(|e| anyhow!("encode fail: {e:?}"))?)
}
}