Skip to main content

Module codec

Module codec 

Source
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 any EventEncoder via EncoderCodec.

Encoder / CodecFeature flagContent-Type
JsonEncoder / JsonCodec(always available)application/json
JsonPrettyEncoder(always available)application/json
CloudEventsEncodercloudeventsapplication/cloudevents+json
ProtobufEncoderprotobufapplication/x-protobuf
AvroEncoderavroavro/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 JSON

Re-exports§

pub use json::JsonCodec;
pub use json::JsonEncoder;
pub use json::JsonPrettyEncoder;

Modules§

json
JSON event encoder.

Structs§

BoxedCodec
A type-erased Codec that wraps any concrete codec behind a single type.
CodecOutput
Combined key + value encoding of a CDC event.
EncodedOutput
Encoded event bytes with the associated MIME content type.
EncoderCodec
A Codec adapter that wraps any EventEncoder.

Traits§

Codec
Higher-level encoding abstraction that produces both a key and a value.
EventEncoder
Encodes a CDC Event into a specific wire format.