Skip to main content

odradek_protocol/messages/
init_producer_id_request.rs

1//! `InitProducerIdRequest` — generated from `schemas/InitProducerIdRequest.json`.
2//!
3//! @generated by `cargo xtask codegen`. Do not edit by hand.
4
5#![allow(
6    clippy::field_reassign_with_default,
7    clippy::nonminimal_bool,
8    clippy::derivable_impls,
9    clippy::borrow_deref_ref,
10    clippy::double_parens,
11    unused_parens
12)]
13
14use bytes::{Buf, BufMut};
15
16use crate::budget::{Budget, Limits};
17#[allow(unused_imports)]
18use crate::error::{DecodeError, EncodeError};
19use crate::wire::{self, RawTaggedField};
20
21/// Whether `version` of this message uses the flexible (compact) encoding.
22pub fn is_flexible(version: i16) -> bool {
23    let _ = version;
24    version >= 2
25}
26
27/// `InitProducerIdRequest`, versions 0-6.
28#[derive(Debug, Clone, PartialEq)]
29#[non_exhaustive]
30pub struct InitProducerIdRequest {
31    /// The transactional id, or null if the producer is not transactional.
32    pub transactional_id: Option<String>,
33    /// The time in ms to wait before aborting idle transactions sent by this producer. This is only relevant if a TransactionalId has been defined.
34    pub transaction_timeout_ms: i32,
35    /// The producer id. This is used to disambiguate requests if a transactional id is reused following its expiration.
36    pub producer_id: i64,
37    /// The producer's current epoch. This will be checked against the producer epoch on the broker, and the request will return an error if they do not match.
38    pub producer_epoch: i16,
39    /// True if the client wants to enable two-phase commit (2PC) protocol for transactions.
40    pub enable2_pc: bool,
41    /// True if the client wants to keep the currently ongoing transaction instead of aborting it.
42    pub keep_prepared_txn: bool,
43    /// Tagged fields not (yet) known to this crate, kept raw.
44    pub unknown_tagged_fields: Vec<RawTaggedField>,
45}
46
47impl Default for InitProducerIdRequest {
48    fn default() -> Self {
49        Self {
50            transactional_id: Some(String::new()),
51            transaction_timeout_ms: 0,
52            producer_id: -1,
53            producer_epoch: -1,
54            enable2_pc: false,
55            keep_prepared_txn: false,
56            unknown_tagged_fields: Vec::new(),
57        }
58    }
59}
60
61impl InitProducerIdRequest {
62    /// This message's api key in Kafka's registry.
63    pub const API_KEY: i16 = 22;
64    /// The lowest schema version this snapshot can speak.
65    pub const MIN_VERSION: i16 = 0;
66    /// The highest schema version this snapshot can speak.
67    pub const MAX_VERSION: i16 = 6;
68
69    /// Encode this `InitProducerIdRequest` at `version`.
70    pub fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
71        if is_flexible(version) {
72            wire::put_compact_nullable_string(buf, self.transactional_id.as_deref());
73        } else {
74            wire::put_nullable_string(buf, self.transactional_id.as_deref())?;
75        }
76        buf.put_i32(self.transaction_timeout_ms);
77        if version >= 3 {
78            buf.put_i64(self.producer_id);
79        }
80        if version >= 3 {
81            buf.put_i16(self.producer_epoch);
82        }
83        if version >= 6 {
84            buf.put_u8(u8::from(self.enable2_pc));
85        }
86        if version >= 6 {
87            buf.put_u8(u8::from(self.keep_prepared_txn));
88        }
89        if is_flexible(version) {
90            wire::put_tagged_fields(buf, &self.unknown_tagged_fields);
91        }
92        Ok(())
93    }
94
95    /// Decode one `InitProducerIdRequest`, bounding allocation by the default
96    /// [`Limits`] derived from `buf`'s remaining length.
97    pub fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
98        Self::decode_with_limits(buf, version, Limits::default())
99    }
100
101    /// Decode one `InitProducerIdRequest` under `limits`.
102    pub fn decode_with_limits(
103        buf: &mut impl Buf,
104        version: i16,
105        limits: Limits,
106    ) -> Result<Self, DecodeError> {
107        let mut budget = limits.budget(buf.remaining());
108        Self::decode_with_budget(buf, version, &mut budget)
109    }
110
111    /// Decode one `InitProducerIdRequest` against an existing `budget`.
112    pub fn decode_with_budget(
113        buf: &mut impl Buf,
114        version: i16,
115        budget: &mut Budget,
116    ) -> Result<Self, DecodeError> {
117        let mut this = Self::default();
118        this.transactional_id = if is_flexible(version) {
119            wire::get_compact_nullable_string(buf)?
120        } else {
121            wire::get_nullable_string(buf)?
122        };
123        this.transaction_timeout_ms = wire::get_i32(buf)?;
124        if version >= 3 {
125            this.producer_id = wire::get_i64(buf)?;
126        }
127        if version >= 3 {
128            this.producer_epoch = wire::get_i16(buf)?;
129        }
130        if version >= 6 {
131            this.enable2_pc = wire::get_bool(buf)?;
132        }
133        if version >= 6 {
134            this.keep_prepared_txn = wire::get_bool(buf)?;
135        }
136        if is_flexible(version) {
137            this.unknown_tagged_fields = wire::get_tagged_fields_with_budget(buf, budget)?;
138        }
139        Ok(this)
140    }
141}
142
143impl crate::Message for InitProducerIdRequest {
144    const API_KEY: i16 = 22;
145    const MIN_VERSION: i16 = 0;
146    const MAX_VERSION: i16 = 6;
147
148    fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError> {
149        InitProducerIdRequest::encode(self, buf, version)
150    }
151    fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError> {
152        InitProducerIdRequest::decode(buf, version)
153    }
154    fn decode_with_limits(
155        buf: &mut impl Buf,
156        version: i16,
157        limits: Limits,
158    ) -> Result<Self, DecodeError> {
159        InitProducerIdRequest::decode_with_limits(buf, version, limits)
160    }
161}