Skip to content

Commit

Permalink
feat: improve CodecError handling
Browse files Browse the repository at this point in the history
  • Loading branch information
fbozic committed Jun 19, 2024
1 parent 411ff93 commit b830f7d
Showing 1 changed file with 10 additions and 24 deletions.
34 changes: 10 additions & 24 deletions examples/chat/src/network/stream/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,10 @@ pub struct Message {
#[derive(Debug, Error)]
#[error("CodecError")]
pub enum CodecError {
StdIo(std::io::Error),
EncodeDecode(std::io::Error),
StdIo(#[from] std::io::Error),
SerDe(serde_json::Error),
}

impl From<std::io::Error> for CodecError {
fn from(err: std::io::Error) -> Self {
CodecError::StdIo(err)
}
}

#[derive(Debug)]
pub(crate) struct MessageJsonCodec;

Expand All @@ -31,16 +24,13 @@ impl Decoder for MessageJsonCodec {

fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
let mut length_codec = LengthDelimitedCodec::new();
let frame = match length_codec.decode(src) {
Ok(Some(frame)) => frame,
Ok(None) => return Ok(None),
Err(e) => return Err(CodecError::EncodeDecode(e)),
let Some(frame) = length_codec.decode(src)? else {
return Ok(None);
};

match serde_json::from_slice(&frame) {
Ok(frame) => Ok(Some(frame)),
Err(e) => Err(CodecError::SerDe(e)),
}
serde_json::from_slice(&frame)
.map(Some)
.map_err(CodecError::SerDe)
}
}

Expand All @@ -49,15 +39,11 @@ impl Encoder<Message> for MessageJsonCodec {

fn encode(&mut self, item: Message, dst: &mut BytesMut) -> Result<(), Self::Error> {
let mut length_codec = LengthDelimitedCodec::new();
let json = match serde_json::to_vec(&item) {
Ok(json) => json,
Err(e) => return Err(CodecError::SerDe(e)),
};
let json = serde_json::to_vec(&item).map_err(CodecError::SerDe)?;

match length_codec.encode(Bytes::from(json), dst) {
Ok(()) => Ok(()),
Err(err) => Err(CodecError::EncodeDecode(err)),
}
length_codec
.encode(Bytes::from(json), dst)
.map_err(CodecError::StdIo)
}
}

Expand Down

0 comments on commit b830f7d

Please sign in to comment.