#[cfg(feature = "avro")]
pub mod avro;
#[cfg(feature = "cloudevents")]
pub mod cloudevents;
pub mod json;
#[cfg(feature = "protobuf")]
pub mod protobuf;
#[cfg(feature = "avro")]
pub use avro::AvroEncoder;
#[cfg(feature = "cloudevents")]
pub use cloudevents::CloudEventsEncoder;
pub use json::{JsonEncoder, JsonPrettyEncoder};
#[cfg(feature = "protobuf")]
pub use protobuf::ProtobufEncoder;
use crate::core::{Event, Result};
#[derive(Debug, Clone)]
pub struct EncodedOutput {
pub bytes: Vec<u8>,
pub content_type: &'static str,
}
impl EncodedOutput {
pub fn new(bytes: Vec<u8>, content_type: &'static str) -> Self {
Self {
bytes,
content_type,
}
}
}
pub trait EventEncoder: Send + Sync {
fn encode(&self, event: &Event) -> Result<EncodedOutput>;
fn content_type(&self) -> &'static str;
}
#[cfg(test)]
mod tests {
use super::*;
use crate::codec::json::JsonEncoder;
use crate::core::{Event, Operation, SourceMetadata, EVENT_ENVELOPE_VERSION};
fn sample_event() -> Event {
Event {
before: None,
after: Some(serde_json::json!({"id": 1})),
op: Operation::Insert,
source: SourceMetadata {
source_name: "test".into(),
offset: "0".into(),
timestamp: 1,
},
ts: 1,
schema: None,
table: "t".into(),
primary_key: None,
snapshot: None,
transaction: None,
envelope_version: EVENT_ENVELOPE_VERSION,
}
}
#[test]
fn json_encoder_content_type_matches_output() {
let enc = JsonEncoder;
let out = enc.encode(&sample_event()).unwrap();
assert_eq!(out.content_type, enc.content_type());
}
#[test]
fn encoded_output_fields_accessible() {
let out = EncodedOutput::new(b"hello".to_vec(), "text/plain");
assert_eq!(out.content_type, "text/plain");
assert_eq!(out.bytes, b"hello");
}
}