Expand description
Wire-format encoders for CDC events.
This module provides two complementary traits:
-
EventEncoder— low-level, encodes a single event into value bytes and a MIME content type. Use this when your downstream system handles key routing separately. -
Codec— higher-level, produces a(CodecOutput)containing both the optional key bytes (for Kafka/Pulsar message keys) and the value bytes in a single call. Built on top of anyEventEncoderviaEncoderCodec.
| Encoder / Codec | Feature flag | Content-Type |
|---|---|---|
JsonEncoder / JsonCodec | (always available) | application/json |
JsonPrettyEncoder | (always available) | application/json |
CloudEventsEncoder | cloudevents | application/cloudevents+json |
ProtobufEncoder | protobuf | application/x-protobuf |
AvroEncoder | avro | avro/binary |
§Usage — EventEncoder
use rustcdc::codec::{EventEncoder, JsonEncoder};
use rustcdc::{Event, Operation, SourceMetadata, EVENT_ENVELOPE_VERSION};
let event = Event {
before: None,
after: Some(serde_json::json!({"id": 1, "name": "alice"})),
op: Operation::Insert,
source: SourceMetadata {
source_name: "postgres".into(),
offset: "0/16B6A70".into(),
timestamp: 1,
},
ts: 1,
schema: Some("public".into()),
table: "users".into(),
primary_key: Some(vec!["id".into()]),
snapshot: None,
transaction: None,
envelope_version: EVENT_ENVELOPE_VERSION,
before_is_key_only: false,
};
let encoder = JsonEncoder;
let output = encoder.encode(&event).unwrap();
assert_eq!(output.content_type, "application/json");
assert!(!output.bytes.is_empty());§Usage — Codec (key + value)
use rustcdc::codec::{Codec, JsonCodec};
use rustcdc::{Event, Operation, EVENT_ENVELOPE_VERSION};
use serde_json::json;
let event = Event {
after: Some(json!({"id": 5, "name": "alice"})),
op: Operation::Insert,
primary_key: Some(vec!["id".into()]),
..Event::default()
};
let codec = JsonCodec::default();
let output = codec.encode(&event).unwrap();
assert!(output.key.is_some()); // compact JSON of primary key
assert!(!output.value.is_empty()); // full event JSONRe-exports§
pub use json::JsonCodec;pub use json::JsonEncoder;pub use json::JsonPrettyEncoder;
Modules§
- json
- JSON event encoder.
Structs§
- Boxed
Codec - A type-erased
Codecthat wraps any concrete codec behind a single type. - Codec
Output - Combined key + value encoding of a CDC event.
- Encoded
Output - Encoded event bytes with the associated MIME content type.
- Encoder
Codec - A
Codecadapter that wraps anyEventEncoder.
Traits§
- Codec
- Higher-level encoding abstraction that produces both a key and a value.
- Event
Encoder - Encodes a CDC
Eventinto a specific wire format.