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
//! Pluggable serialization applied on the way to and from the wire.
//!
//! A [`Serializer`] runs on every record the producer sends, after the
//! interceptors and before partitioning. A [`Deserializer`] runs on every
//! record the consumer delivers, after the interceptors and immediately before
//! `poll()` returns. Attach them with `key_serializer` / `value_serializer` on
//! either producer builder and `key_deserializer` / `value_deserializer` on the
//! consumer builder.
//!
//! This is the same hook the Java client exposes as `key.serializer` /
//! `value.serializer`, and it exists for the same reason: the Kafka client
//! should own the *place* the transformation happens, and the ecosystem should
//! own the transformations.
//!
//! # What krafka deliberately does not ship
//!
//! There is no schema-registry client here, and no Avro, Protobuf or JSON
//! codec. Every comparable client draws the line in the same place — Java's
//! `kafka-clients` has no registry support (`kafka-avro-serializer` is a
//! separate artifact), librdkafka has none (`libschemaregistry` is a separate
//! library), and franz-go keeps `pkg/sr` out of `kgo`. A schema registry is a
//! different service, with a different protocol, auth model and release
//! cadence; coupling it to the Kafka protocol client means a registry API
//! change forces a Kafka client release.
//!
//! krafka carried a Confluent + AWS Glue registry client until 0.18. It now
//! lives in [`schemreg`](https://crates.io/crates/schemreg), which also has
//! native Apicurio support and real Avro / Protobuf / JSON codecs that krafka
//! never had. Pair the two with a small adapter — see the
//! [Cookbook](https://hupe1980.github.io/krafka/docs/cookbook/#use-a-schema-registry).
//!
//! # Beyond schemas
//!
//! Because the traits are plain `Bytes -> Bytes`, they are not limited to
//! schema framing. Envelope encryption, an application-level compression
//! scheme, or a bare `serde_json` round-trip all fit the same hook.
//!
//! # Errors
//!
//! An error from either trait fails the operation: the producer's `send`
//! returns it, and `poll` returns it rather than delivering a record it could
//! not decode. Neither is invoked for an absent key or value.
use Future;
use Pin;
use Bytes;
use crateResult;
/// Transforms a record's key or value on its way to the broker.
///
/// Implementations must be cheap to share: one instance handles every record on
/// the producer, so keep per-call work off the hot path and cache anything
/// derived from the topic.
///
/// # Example
///
/// A serializer that prefixes a version byte.
///
/// ```rust
/// use std::future::Future;
/// use std::pin::Pin;
///
/// use bytes::{BufMut, Bytes, BytesMut};
/// use krafka::serdes::Serializer;
///
/// #[derive(Debug)]
/// struct VersionTagged(u8);
///
/// impl Serializer for VersionTagged {
/// fn serialize(
/// &self,
/// payload: Bytes,
/// _topic: &str,
/// _record_name: Option<&str>,
/// _is_key: bool,
/// ) -> Pin<Box<dyn Future<Output = krafka::Result<Bytes>> + Send + '_>> {
/// let version = self.0;
/// Box::pin(async move {
/// let mut out = BytesMut::with_capacity(1 + payload.len());
/// out.put_u8(version);
/// out.put_slice(&payload);
/// Ok(out.freeze())
/// })
/// }
/// }
/// ```
/// Transforms a record's key or value on its way to the application.
///
/// The inverse of [`Serializer`], applied by the consumer immediately before
/// `poll()` returns.