1use crate::event::EventBatch;
4use crate::metadata::ArcEventMetadata;
5use bytes::Bytes;
6use snafu::Snafu;
7use std::sync::Arc;
8
9#[derive(Debug, Snafu)]
10pub enum Error {
11 #[snafu(display("Failed to encode batch: {}", source))]
12 Encode {
13 #[snafu(source(false))]
14 source: Box<dyn std::error::Error + Send + Sync + 'static>,
15 },
16
17 #[snafu(display("Failed to decode batch: {}", source))]
18 Decode {
19 #[snafu(source(false))]
20 source: Box<dyn std::error::Error + Send + Sync + 'static>,
21 },
22
23 #[snafu(display("Message size {} exceeds limit of {}", size, limit))]
24 MessageTooLarge { size: usize, limit: usize },
25}
26
27pub type Result<T, E = Error> = std::result::Result<T, E>;
28
29pub trait Encoder: Send + Sync {
31 fn encode(&self, batch: &EventBatch) -> Result<Bytes>;
34
35 fn encode_individual(&self, batch: &EventBatch) -> Result<Vec<Bytes>> {
39 self.encode(batch).map(|b| vec![b])
40 }
41}
42
43pub trait Decoder: Send + Sync {
45 fn decode(&self, data: &[u8], metadata: ArcEventMetadata) -> Result<EventBatch>;
48}
49
50pub trait EncoderConfig: Send + Sync {
52 fn build(&self) -> Result<Arc<dyn Encoder>>;
53}
54
55pub trait DecoderConfig: Send + Sync {
57 fn build(&self, schema: Arc<arrow_schema::Schema>) -> Result<Arc<dyn Decoder>>;
59}