odradek-protocol 0.1.0

Sans-I/O implementation of the Kafka wire protocol: primitive codecs, message framing, and versioned message types
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
//! `AddPartitionsToTxnResponse` — generated from `schemas/AddPartitionsToTxnResponse.json`.
//!
//! @generated by `cargo xtask codegen`. Do not edit by hand.

#![allow(
    clippy::field_reassign_with_default,
    clippy::nonminimal_bool,
    clippy::derivable_impls,
    clippy::borrow_deref_ref,
    clippy::double_parens,
    unused_parens
)]

use bytes::{Buf, BufMut};

use crate::budget::{Budget, Limits};
#[allow(unused_imports)]
use crate::error::{DecodeError, EncodeError};
use crate::wire::{self, RawTaggedField};

/// Whether `version` of this message uses the flexible (compact) encoding.
pub fn is_flexible(version: i16) -> bool {
    let _ = version;
    version >= 3
}

/// `AddPartitionsToTxnResponse`, versions 0-5.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct AddPartitionsToTxnResponse {
    /// Duration in milliseconds for which the request was throttled due to a quota violation, or zero if the request did not violate any quota.
    pub throttle_time_ms: i32,
    /// The response top level error code.
    pub error_code: i16,
    /// Results categorized by transactional ID.
    pub results_by_transaction: Vec<AddPartitionsToTxnResult>,
    /// The results for each topic.
    pub results_by_topic_v3_and_below: Vec<AddPartitionsToTxnTopicResult>,
    /// Tagged fields not (yet) known to this crate, kept raw.
    pub unknown_tagged_fields: Vec<RawTaggedField>,
}

impl Default for AddPartitionsToTxnResponse {
    fn default() -> Self {
        Self {
            throttle_time_ms: 0,
            error_code: 0,
            results_by_transaction: Vec::new(),
            results_by_topic_v3_and_below: Vec::new(),
            unknown_tagged_fields: Vec::new(),
        }
    }
}

impl AddPartitionsToTxnResponse {
    /// This message's api key in Kafka's registry.
    pub const API_KEY: i16 = 24;
    /// The lowest schema version this snapshot can speak.
    pub const MIN_VERSION: i16 = 0;
    /// The highest schema version this snapshot can speak.
    pub const MAX_VERSION: i16 = 5;

    /// Encode this `AddPartitionsToTxnResponse` at `version`.
    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
        buf.put_i32(self.throttle_time_ms);
        if version >= 4 {
            buf.put_i16(self.error_code);
        }
        if version >= 4 {
            if is_flexible(version) {
                wire::put_compact_array_len(buf, Some(self.results_by_transaction.len()));
            } else {
                wire::put_array_len(buf, Some(self.results_by_transaction.len()))?;
            }
            for item in &self.results_by_transaction {
                item.encode(buf, version)?;
            }
        }
        if version <= 3 {
            if is_flexible(version) {
                wire::put_compact_array_len(buf, Some(self.results_by_topic_v3_and_below.len()));
            } else {
                wire::put_array_len(buf, Some(self.results_by_topic_v3_and_below.len()))?;
            }
            for item in &self.results_by_topic_v3_and_below {
                item.encode(buf, version)?;
            }
        }
        if is_flexible(version) {
            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }

    /// Decode one `AddPartitionsToTxnResponse`, bounding allocation by the default
    /// [`Limits`] derived from `buf`'s remaining length.
    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
        Self::decode_with_limits(buf, version, Limits::default())
    }

    /// Decode one `AddPartitionsToTxnResponse` under `limits`.
    pub fn decode_with_limits(
        buf: &mut impl Buf,
        version: i16,
        limits: Limits,
    ) -> Result<Self, DecodeError> {
        let mut budget = limits.budget(buf.remaining());
        Self::decode_with_budget(buf, version, &mut budget)
    }

    /// Decode one `AddPartitionsToTxnResponse` against an existing `budget`.
    pub fn decode_with_budget(
        buf: &mut impl Buf,
        version: i16,
        budget: &mut Budget,
    ) -> Result<Self, DecodeError> {
        let mut this = Self::default();
        this.throttle_time_ms = wire::get_i32(buf)?;
        if version >= 4 {
            this.error_code = wire::get_i16(buf)?;
        }
        if version >= 4 {
            this.results_by_transaction = {
                let len = if is_flexible(version) {
                    wire::get_compact_array_len(buf)?
                } else {
                    wire::get_array_len(buf)?
                };
                match len {
                    None => return Err(DecodeError::InvalidLength(-1)),
                    Some(n) => {
                        let mut items = Vec::new();
                        for _ in 0..n {
                            let mark = buf.remaining();
                            let item =
                                AddPartitionsToTxnResult::decode_with_budget(buf, version, budget)?;
                            budget.progress(mark, buf.remaining())?;
                            budget.push(&mut items, item)?;
                        }
                        items
                    }
                }
            };
        }
        if version <= 3 {
            this.results_by_topic_v3_and_below = {
                let len = if is_flexible(version) {
                    wire::get_compact_array_len(buf)?
                } else {
                    wire::get_array_len(buf)?
                };
                match len {
                    None => return Err(DecodeError::InvalidLength(-1)),
                    Some(n) => {
                        let mut items = Vec::new();
                        for _ in 0..n {
                            let mark = buf.remaining();
                            let item = AddPartitionsToTxnTopicResult::decode_with_budget(
                                buf, version, budget,
                            )?;
                            budget.progress(mark, buf.remaining())?;
                            budget.push(&mut items, item)?;
                        }
                        items
                    }
                }
            };
        }
        if is_flexible(version) {
            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
        }
        Ok(this)
    }
}

impl crate::Message for AddPartitionsToTxnResponse {
    const API_KEY: i16 = 24;
    const MIN_VERSION: i16 = 0;
    const MAX_VERSION: i16 = 5;

    fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
        AddPartitionsToTxnResponse::encode(self, buf, version)
    }
    fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
        AddPartitionsToTxnResponse::decode(buf, version)
    }
    fn decode_with_limits(
        buf: &mut impl Buf,
        version: i16,
        limits: Limits,
    ) -> Result<Self, DecodeError> {
        AddPartitionsToTxnResponse::decode_with_limits(buf, version, limits)
    }
    fn throttle_time_ms(&self) -> Option<i32> {
        Some(self.throttle_time_ms)
    }
}

/// `AddPartitionsToTxnResult` (nested in [`AddPartitionsToTxnResponse`]).
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct AddPartitionsToTxnResult {
    /// The transactional id corresponding to the transaction.
    pub transactional_id: String,
    /// The results for each topic.
    pub topic_results: Vec<AddPartitionsToTxnTopicResult>,
    /// Tagged fields not (yet) known to this crate, kept raw.
    pub unknown_tagged_fields: Vec<RawTaggedField>,
}

impl Default for AddPartitionsToTxnResult {
    fn default() -> Self {
        Self {
            transactional_id: String::new(),
            topic_results: Vec::new(),
            unknown_tagged_fields: Vec::new(),
        }
    }
}

impl AddPartitionsToTxnResult {
    /// Encode this `AddPartitionsToTxnResult` at `version`.
    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
        if version >= 4 {
            if is_flexible(version) {
                wire::put_compact_string(buf, &self.transactional_id);
            } else {
                wire::put_string(buf, &self.transactional_id)?;
            }
        }
        if version >= 4 {
            if is_flexible(version) {
                wire::put_compact_array_len(buf, Some(self.topic_results.len()));
            } else {
                wire::put_array_len(buf, Some(self.topic_results.len()))?;
            }
            for item in &self.topic_results {
                item.encode(buf, version)?;
            }
        }
        if is_flexible(version) {
            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }

    /// Decode one `AddPartitionsToTxnResult`, bounding allocation by the default
    /// [`Limits`] derived from `buf`'s remaining length.
    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
        Self::decode_with_limits(buf, version, Limits::default())
    }

    /// Decode one `AddPartitionsToTxnResult` under `limits`.
    pub fn decode_with_limits(
        buf: &mut impl Buf,
        version: i16,
        limits: Limits,
    ) -> Result<Self, DecodeError> {
        let mut budget = limits.budget(buf.remaining());
        Self::decode_with_budget(buf, version, &mut budget)
    }

    /// Decode one `AddPartitionsToTxnResult` against an existing `budget`.
    pub fn decode_with_budget(
        buf: &mut impl Buf,
        version: i16,
        budget: &mut Budget,
    ) -> Result<Self, DecodeError> {
        let mut this = Self::default();
        if version >= 4 {
            this.transactional_id = if is_flexible(version) {
                wire::get_compact_string(buf)?
            } else {
                wire::get_string(buf)?
            };
        }
        if version >= 4 {
            this.topic_results = {
                let len = if is_flexible(version) {
                    wire::get_compact_array_len(buf)?
                } else {
                    wire::get_array_len(buf)?
                };
                match len {
                    None => return Err(DecodeError::InvalidLength(-1)),
                    Some(n) => {
                        let mut items = Vec::new();
                        for _ in 0..n {
                            let mark = buf.remaining();
                            let item = AddPartitionsToTxnTopicResult::decode_with_budget(
                                buf, version, budget,
                            )?;
                            budget.progress(mark, buf.remaining())?;
                            budget.push(&mut items, item)?;
                        }
                        items
                    }
                }
            };
        }
        if is_flexible(version) {
            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
        }
        Ok(this)
    }
}

/// `AddPartitionsToTxnTopicResult` (nested in [`AddPartitionsToTxnResponse`]).
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct AddPartitionsToTxnTopicResult {
    /// The topic name.
    pub name: String,
    /// The results for each partition.
    pub results_by_partition: Vec<AddPartitionsToTxnPartitionResult>,
    /// Tagged fields not (yet) known to this crate, kept raw.
    pub unknown_tagged_fields: Vec<RawTaggedField>,
}

impl Default for AddPartitionsToTxnTopicResult {
    fn default() -> Self {
        Self {
            name: String::new(),
            results_by_partition: Vec::new(),
            unknown_tagged_fields: Vec::new(),
        }
    }
}

impl AddPartitionsToTxnTopicResult {
    /// Encode this `AddPartitionsToTxnTopicResult` at `version`.
    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
        if is_flexible(version) {
            wire::put_compact_string(buf, &self.name);
        } else {
            wire::put_string(buf, &self.name)?;
        }
        if is_flexible(version) {
            wire::put_compact_array_len(buf, Some(self.results_by_partition.len()));
        } else {
            wire::put_array_len(buf, Some(self.results_by_partition.len()))?;
        }
        for item in &self.results_by_partition {
            item.encode(buf, version)?;
        }
        if is_flexible(version) {
            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }

    /// Decode one `AddPartitionsToTxnTopicResult`, bounding allocation by the default
    /// [`Limits`] derived from `buf`'s remaining length.
    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
        Self::decode_with_limits(buf, version, Limits::default())
    }

    /// Decode one `AddPartitionsToTxnTopicResult` under `limits`.
    pub fn decode_with_limits(
        buf: &mut impl Buf,
        version: i16,
        limits: Limits,
    ) -> Result<Self, DecodeError> {
        let mut budget = limits.budget(buf.remaining());
        Self::decode_with_budget(buf, version, &mut budget)
    }

    /// Decode one `AddPartitionsToTxnTopicResult` against an existing `budget`.
    pub fn decode_with_budget(
        buf: &mut impl Buf,
        version: i16,
        budget: &mut Budget,
    ) -> Result<Self, DecodeError> {
        let mut this = Self::default();
        this.name = if is_flexible(version) {
            wire::get_compact_string(buf)?
        } else {
            wire::get_string(buf)?
        };
        this.results_by_partition = {
            let len = if is_flexible(version) {
                wire::get_compact_array_len(buf)?
            } else {
                wire::get_array_len(buf)?
            };
            match len {
                None => return Err(DecodeError::InvalidLength(-1)),
                Some(n) => {
                    let mut items = Vec::new();
                    for _ in 0..n {
                        let mark = buf.remaining();
                        let item = AddPartitionsToTxnPartitionResult::decode_with_budget(
                            buf, version, budget,
                        )?;
                        budget.progress(mark, buf.remaining())?;
                        budget.push(&mut items, item)?;
                    }
                    items
                }
            }
        };
        if is_flexible(version) {
            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
        }
        Ok(this)
    }
}

/// `AddPartitionsToTxnPartitionResult` (nested in [`AddPartitionsToTxnResponse`]).
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct AddPartitionsToTxnPartitionResult {
    /// The partition indexes.
    pub partition_index: i32,
    /// The response error code.
    pub partition_error_code: i16,
    /// Tagged fields not (yet) known to this crate, kept raw.
    pub unknown_tagged_fields: Vec<RawTaggedField>,
}

impl Default for AddPartitionsToTxnPartitionResult {
    fn default() -> Self {
        Self {
            partition_index: 0,
            partition_error_code: 0,
            unknown_tagged_fields: Vec::new(),
        }
    }
}

impl AddPartitionsToTxnPartitionResult {
    /// Encode this `AddPartitionsToTxnPartitionResult` at `version`.
    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
        buf.put_i32(self.partition_index);
        buf.put_i16(self.partition_error_code);
        if is_flexible(version) {
            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
        }
        Ok(())
    }

    /// Decode one `AddPartitionsToTxnPartitionResult`, bounding allocation by the default
    /// [`Limits`] derived from `buf`'s remaining length.
    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
        Self::decode_with_limits(buf, version, Limits::default())
    }

    /// Decode one `AddPartitionsToTxnPartitionResult` under `limits`.
    pub fn decode_with_limits(
        buf: &mut impl Buf,
        version: i16,
        limits: Limits,
    ) -> Result<Self, DecodeError> {
        let mut budget = limits.budget(buf.remaining());
        Self::decode_with_budget(buf, version, &mut budget)
    }

    /// Decode one `AddPartitionsToTxnPartitionResult` against an existing `budget`.
    pub fn decode_with_budget(
        buf: &mut impl Buf,
        version: i16,
        budget: &mut Budget,
    ) -> Result<Self, DecodeError> {
        let mut this = Self::default();
        this.partition_index = wire::get_i32(buf)?;
        this.partition_error_code = wire::get_i16(buf)?;
        if is_flexible(version) {
            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
        }
        Ok(this)
    }
}