rustcdc 0.6.7

Embeddable Rust CDC library focused on correctness-first capture primitives
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//! 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 / 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`
//!
//! ```rust
//! 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)
//!
//! ```rust
//! 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
//! ```

#[cfg(feature = "avro")]
pub mod avro;
#[cfg(feature = "cloudevents")]
pub mod cloudevents;
pub mod json;
#[cfg(feature = "protobuf")]
pub mod protobuf;
#[cfg(feature = "schemreg")]
pub mod schema_registry;

#[cfg(feature = "avro")]
pub use avro::AvroEncoder;
#[cfg(feature = "cloudevents")]
pub use cloudevents::CloudEventsEncoder;
pub use json::{JsonCodec, JsonEncoder, JsonPrettyEncoder};
#[cfg(feature = "protobuf")]
pub use protobuf::ProtobufEncoder;
#[cfg(feature = "schemreg")]
pub use schema_registry::{
    decode_wire_format, encode_wire_format, CachedSchemaRegistry, CompatibilityLevel,
    ConfluentAvroCodec, ConfluentAvroDecoder, ConfluentAvroEncoder, ConfluentJsonSchemaCodec,
    ConfluentJsonSchemaDecoder, ConfluentJsonSchemaEncoder, ConfluentSchemaRegistry, EncodeTarget,
    SchemaId, SchemaRegistryAuth, SchemaRegistryClient, SchemaRegistryConfig, SchemaType,
    SubjectNameStrategy, EVENT_JSON_SCHEMA, KEY_AVRO_SCHEMA, KEY_JSON_SCHEMA,
};

use crate::core::{Event, Result};

// ─── EncodedOutput ────────────────────────────────────────────────────────────

/// Encoded event bytes with the associated MIME content type.
#[derive(Debug, Clone)]
pub struct EncodedOutput {
    /// The encoded bytes.
    pub bytes: Vec<u8>,
    /// MIME content type that describes the encoding.
    pub content_type: &'static str,
}

impl EncodedOutput {
    /// Create a new `EncodedOutput`.
    pub fn new(bytes: Vec<u8>, content_type: &'static str) -> Self {
        Self {
            bytes,
            content_type,
        }
    }
}

// ─── EventEncoder ─────────────────────────────────────────────────────────────

/// Encodes a CDC [`Event`] into a specific wire format.
///
/// Implementations are `Send + Sync` so they can be shared across async tasks
/// (e.g. via `Arc<dyn EventEncoder>`).
///
/// # Implementing a custom encoder
///
/// ```rust
/// use rustcdc::codec::{EncodedOutput, EventEncoder};
/// use rustcdc::core::{Event, Result};
///
/// struct MyEncoder;
///
/// impl EventEncoder for MyEncoder {
///     fn encode(&self, event: &Event) -> Result<EncodedOutput> {
///         let bytes = format!("{}:{}", event.table, event.op).into_bytes();
///         Ok(EncodedOutput::new(bytes, "text/plain"))
///     }
///
///     fn content_type(&self) -> &'static str {
///         "text/plain"
///     }
/// }
/// ```
pub trait EventEncoder: Send + Sync {
    /// Encode a single CDC event into bytes.
    fn encode(&self, event: &Event) -> Result<EncodedOutput>;

    /// The MIME content type for every successful [`encode`](Self::encode) call.
    ///
    /// This is a constant associated with the encoder type, not with individual events.
    fn content_type(&self) -> &'static str;

    /// Encode the primary-key columns of an event as a compact JSON key.
    ///
    /// The default implementation serialises the object returned by
    /// [`Event::primary_key_values`] as compact JSON. Returns `None` when the
    /// event has no `primary_key` defined, when none of the key columns appear in
    /// the row image, or when serialisation fails.
    ///
    /// Override this method to produce a different key format (e.g. Avro-encoded
    /// keys, string-formatted composite keys, or opaque binary keys).
    ///
    /// # Use case
    ///
    /// Kafka / Pulsar producers need a separate key payload for message routing and
    /// log-compaction. Passing the result of `encode_key()` as the Kafka message key
    /// ensures that all events for the same row land on the same partition.
    ///
    /// # Example
    ///
    /// ```
    /// use rustcdc::codec::{EventEncoder, JsonEncoder};
    /// 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 encoder = JsonEncoder;
    /// let key = encoder.encode_key(&event).unwrap();
    /// assert_eq!(key, br#"{"id":5}"#);
    /// ```
    fn encode_key(&self, event: &Event) -> Option<Vec<u8>> {
        let value = event.primary_key_values()?;
        serde_json::to_vec(&value).ok()
    }
}

// ─── Codec ────────────────────────────────────────────────────────────────────

/// Combined key + value encoding of a CDC event.
///
/// Returned by [`Codec::encode`].
#[derive(Debug, Clone)]
pub struct CodecOutput {
    /// Encoded message key, or `None` when the event has no primary key.
    ///
    /// For Kafka producers: pass this as the Kafka message key so all events
    /// for the same row land on the same partition and log-compaction works.
    pub key: Option<Vec<u8>>,
    /// Encoded event value bytes.
    pub value: Vec<u8>,
    /// MIME content type of the *value* bytes.
    pub content_type: &'static str,
}

impl CodecOutput {
    /// Create a new `CodecOutput`.
    pub fn new(key: Option<Vec<u8>>, value: Vec<u8>, content_type: &'static str) -> Self {
        Self {
            key,
            value,
            content_type,
        }
    }
}

/// Higher-level encoding abstraction that produces both a key and a value.
///
/// This is the right abstraction for Kafka / Pulsar producers and any pipeline
/// that must route or compact messages by primary key. Prefer [`Codec`] over
/// [`EventEncoder`] when building producer-side integrations.
///
/// Implementations must be `Send + Sync` so they can be shared across async tasks
/// (e.g. behind an `Arc<dyn Codec>`).
///
/// # Implementing a custom codec
///
/// ```rust
/// use rustcdc::codec::{Codec, CodecOutput};
/// use rustcdc::core::{Event, Result};
///
/// struct MyAvroCodec;
///
/// impl Codec for MyAvroCodec {
///     fn encode(&self, event: &Event) -> Result<CodecOutput> {
///         let key = event.primary_key_values()
///             .and_then(|v| serde_json::to_vec(&v).ok());
///         let value = serde_json::to_vec(event)?;
///         Ok(CodecOutput::new(key, value, "application/json"))
///     }
///
///     fn content_type(&self) -> &'static str {
///         "application/json"
///     }
/// }
/// ```
pub trait Codec: Send + Sync {
    /// Encode the event into a key + value pair.
    fn encode(&self, event: &Event) -> Result<CodecOutput>;

    /// The MIME content type for every successful `value` byte sequence.
    fn content_type(&self) -> &'static str;

    /// Wrap `self` in a [`BoxedCodec`], erasing the concrete type.
    ///
    /// Enables storing heterogeneous codecs without a shared enum or generic
    /// parameters on the containing struct.  Requires `Self: 'static`.
    fn boxed(self) -> BoxedCodec
    where
        Self: Sized + 'static,
    {
        BoxedCodec::new(self)
    }
}

/// A [`Codec`] adapter that wraps any [`EventEncoder`].
///
/// `EncoderCodec` calls `EventEncoder::encode_key` for the key and
/// `EventEncoder::encode` for the value.  It is the canonical bridge between the
/// lower-level [`EventEncoder`] trait and the higher-level [`Codec`] trait.
///
/// # Example
///
/// ```rust
/// use rustcdc::codec::{Codec, EncoderCodec, JsonEncoder};
/// use rustcdc::{Event, Operation, EVENT_ENVELOPE_VERSION};
/// use serde_json::json;
///
/// let codec = EncoderCodec::new(JsonEncoder);
/// let event = Event {
///     after: Some(json!({"id": 1})),
///     op: Operation::Insert,
///     primary_key: Some(vec!["id".into()]),
///     ..Event::default()
/// };
/// let out = codec.encode(&event).unwrap();
/// assert_eq!(out.content_type, "application/json");
/// assert!(out.key.is_some());
/// ```
#[derive(Debug, Clone, Default)]
pub struct EncoderCodec<E> {
    encoder: E,
}

impl<E: EventEncoder> EncoderCodec<E> {
    /// Wrap an `EventEncoder` as a `Codec`.
    pub fn new(encoder: E) -> Self {
        Self { encoder }
    }

    /// Borrow the inner encoder.
    pub fn encoder(&self) -> &E {
        &self.encoder
    }

    /// Unwrap the inner encoder.
    pub fn into_encoder(self) -> E {
        self.encoder
    }
}

impl<E: EventEncoder> Codec for EncoderCodec<E> {
    fn encode(&self, event: &Event) -> Result<CodecOutput> {
        let key = self.encoder.encode_key(event);
        let value_output = self.encoder.encode(event)?;
        Ok(CodecOutput::new(
            key,
            value_output.bytes,
            value_output.content_type,
        ))
    }

    fn content_type(&self) -> &'static str {
        self.encoder.content_type()
    }
}

// ─── BoxedCodec ───────────────────────────────────────────────────────────────

/// A type-erased [`Codec`] that wraps any concrete codec behind a single type.
///
/// `BoxedCodec` removes the need for generic parameters when storing or
/// passing codecs across pipeline stages that support multiple encodings
/// (JSON, CloudEvents, Avro, …) without a shared enum.
///
/// # Construction
///
/// ```rust
/// use rustcdc::codec::{BoxedCodec, Codec, JsonCodec};
///
/// // Via the .boxed() convenience method (preferred):
/// let codec: BoxedCodec = JsonCodec::default().boxed();
///
/// // Or explicitly:
/// let codec = BoxedCodec::new(JsonCodec::default());
/// ```
pub struct BoxedCodec(Box<dyn Codec>);

impl BoxedCodec {
    /// Wrap any [`Codec`] implementation in a type-erased `BoxedCodec`.
    ///
    /// Prefer [`Codec::boxed`] for ergonomic construction.
    pub fn new<C: Codec + 'static>(codec: C) -> Self {
        Self(Box::new(codec))
    }
}

impl Codec for BoxedCodec {
    fn encode(&self, event: &Event) -> Result<CodecOutput> {
        self.0.encode(event)
    }

    fn content_type(&self) -> &'static str {
        self.0.content_type()
    }
}

#[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,
            before_is_key_only: false,
        }
    }

    #[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");
    }

    #[test]
    fn encoder_codec_no_primary_key_gives_no_key() {
        let codec = EncoderCodec::new(JsonEncoder);
        let event = sample_event(); // primary_key = None
        let out = codec.encode(&event).unwrap();
        assert!(out.key.is_none());
        assert!(!out.value.is_empty());
        assert_eq!(out.content_type, "application/json");
    }

    #[test]
    fn encoder_codec_with_primary_key_encodes_key() {
        let codec = EncoderCodec::new(JsonEncoder);
        let mut event = sample_event();
        event.primary_key = Some(vec!["id".into()]);
        event.after = Some(serde_json::json!({"id": 7, "name": "bob"}));
        let out = codec.encode(&event).unwrap();
        let key = out.key.expect("key should be present");
        let parsed: serde_json::Value = serde_json::from_slice(&key).unwrap();
        assert_eq!(parsed["id"], 7);
    }

    #[test]
    fn encoder_codec_content_type_matches_encoder() {
        let codec = EncoderCodec::new(JsonEncoder);
        let event = sample_event();
        let out = codec.encode(&event).unwrap();
        assert_eq!(out.content_type, codec.content_type());
    }

    #[test]
    fn codec_output_constructor() {
        let o = CodecOutput::new(Some(b"k".to_vec()), b"v".to_vec(), "text/plain");
        assert_eq!(o.key.unwrap(), b"k");
        assert_eq!(o.value, b"v");
        assert_eq!(o.content_type, "text/plain");
    }

    #[test]
    fn json_codec_default_works() {
        use crate::codec::json::JsonCodec;
        let codec = JsonCodec::default();
        let mut event = sample_event();
        event.primary_key = Some(vec!["id".into()]);
        event.after = Some(serde_json::json!({"id": 1}));
        let out = codec.encode(&event).unwrap();
        assert!(out.key.is_some());
        assert_eq!(out.content_type, "application/json");
    }

    #[test]
    fn boxed_codec_erases_type_and_encodes() {
        use crate::codec::json::JsonCodec;
        let codec: BoxedCodec = JsonCodec::default().boxed();
        let mut event = sample_event();
        event.primary_key = Some(vec!["id".into()]);
        event.after = Some(serde_json::json!({"id": 42}));
        let out = codec.encode(&event).unwrap();
        assert_eq!(out.content_type, "application/json");
        assert!(out.key.is_some());
    }

    #[test]
    fn boxed_codec_new_works() {
        use crate::codec::json::JsonCodec;
        let codec = BoxedCodec::new(JsonCodec::default());
        let event = sample_event();
        let out = codec.encode(&event).unwrap();
        assert!(!out.value.is_empty());
    }
}