rskafka/protocol/
traits.rs

1use std::io::{Read, Write};
2
3use thiserror::Error;
4
5#[derive(Error, Debug)]
6#[non_exhaustive]
7pub enum ReadError {
8    #[error("Cannot read data: {0}")]
9    IO(#[from] std::io::Error),
10
11    #[error("Overflow converting integer: {0}")]
12    Overflow(#[from] std::num::TryFromIntError),
13
14    #[error("Malformed data: {0}")]
15    Malformed(#[from] Box<dyn std::error::Error + Send + Sync>),
16}
17
18pub trait ReadType<R>: Sized
19where
20    R: Read,
21{
22    fn read(reader: &mut R) -> Result<Self, ReadError>;
23}
24
25#[derive(Error, Debug)]
26#[non_exhaustive]
27pub enum WriteError {
28    #[error("Cannot write data: {0}")]
29    IO(#[from] std::io::Error),
30
31    #[error("Overflow converting integer: {0}")]
32    Overflow(#[from] std::num::TryFromIntError),
33
34    #[error("Malformed data: {0}")]
35    Malformed(#[from] Box<dyn std::error::Error + Send + Sync>),
36}
37
38pub trait WriteType<W>: Sized
39where
40    W: Write,
41{
42    fn write(&self, writer: &mut W) -> Result<(), WriteError>;
43}