crabka_protocol/opt/rustwide/workdir/generated/
InitProducerIdRequest.borrowed.rs1use bytes::BufMut;
4
5use crate::primitives::fixed::{get_bool, get_i16, get_i32, get_i64, put_bool, put_i16, put_i32, put_i64};
6use crate::primitives::string_bytes::{
7 compact_nullable_string_len, compact_string_len, nullable_string_len,
8 put_compact_nullable_string, put_compact_string, put_nullable_string, put_string,
9 string_len,
10};
11use crate::primitives::string_bytes_borrowed::{
12 get_compact_nullable_string_borrowed, get_compact_string_borrowed,
13 get_nullable_string_borrowed, get_string_borrowed,
14};
15use crate::tagged_fields::{read_tagged_fields, tagged_fields_len, WriteTaggedFields};
16use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
17
18pub const API_KEY: i16 = 22;
19pub const MIN_VERSION: i16 = 0;
20pub const MAX_VERSION: i16 = 6;
21pub const FLEXIBLE_MIN: i16 = 2;
22
23#[inline]
24fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
25
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct InitProducerIdRequest<'a> {
28 pub transactional_id: Option<&'a str>,
29 pub transaction_timeout_ms: i32,
30 pub producer_id: i64,
31 pub producer_epoch: i16,
32 pub enable2_pc: bool,
33 pub keep_prepared_txn: bool,
34 pub unknown_tagged_fields: UnknownTaggedFields,
35}
36
37impl<'a> Default for InitProducerIdRequest<'a> {
38 fn default() -> Self {
39 Self {
40 transactional_id: None,
41 transaction_timeout_ms: 0i32,
42 producer_id: -1i64,
43 producer_epoch: -1i16,
44 enable2_pc: false,
45 keep_prepared_txn: false,
46 unknown_tagged_fields: Default::default(),
47 }
48 }
49}
50
51impl<'a> InitProducerIdRequest<'a> {
52 pub fn to_owned(&self) -> crate::owned::init_producer_id_request::InitProducerIdRequest {
53 crate::owned::init_producer_id_request::InitProducerIdRequest {
54 transactional_id: (self.transactional_id).map(|s| s.to_string()),
55 transaction_timeout_ms: (self.transaction_timeout_ms),
56 producer_id: (self.producer_id),
57 producer_epoch: (self.producer_epoch),
58 enable2_pc: (self.enable2_pc),
59 keep_prepared_txn: (self.keep_prepared_txn),
60 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
61 }
62 }
63}
64
65impl<'a> Encode for InitProducerIdRequest<'a> {
66 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
67 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
68 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
69 }
70 let flex = is_flexible(version);
71 if version >= 0 { if flex { put_compact_nullable_string(buf, self.transactional_id) } else { put_nullable_string(buf, self.transactional_id) } }
72 if version >= 0 { put_i32(buf, self.transaction_timeout_ms) }
73 if version >= 3 { put_i64(buf, self.producer_id) }
74 if version >= 3 { put_i16(buf, self.producer_epoch) }
75 if version >= 6 { put_bool(buf, self.enable2_pc) }
76 if version >= 6 { put_bool(buf, self.keep_prepared_txn) }
77 if flex {
78 let tagged = WriteTaggedFields::new();
79 tagged.write(buf, &self.unknown_tagged_fields);
80 }
81 Ok(())
82 }
83 fn encoded_len(&self, version: i16) -> usize {
84 let flex = is_flexible(version);
85 let mut n: usize = 0;
86 if version >= 0 { n += if flex { compact_nullable_string_len(self.transactional_id) } else { nullable_string_len(self.transactional_id) }; }
87 if version >= 0 { n += 4; }
88 if version >= 3 { n += 8; }
89 if version >= 3 { n += 2; }
90 if version >= 6 { n += 1; }
91 if version >= 6 { n += 1; }
92 if flex {
93 let known_pairs: Vec<(u32, usize)> = Vec::new();
94 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
95 }
96 n
97 }
98}
99
100impl<'de> DecodeBorrow<'de> for InitProducerIdRequest<'de> {
101 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
102 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
103 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
104 }
105 let flex = is_flexible(version);
106 let mut out = Self::default();
107 if version >= 0 { out.transactional_id = if flex { get_compact_nullable_string_borrowed(buf)? } else { get_nullable_string_borrowed(buf)? }; }
108 if version >= 0 { out.transaction_timeout_ms = get_i32(buf)?; }
109 if version >= 3 { out.producer_id = get_i64(buf)?; }
110 if version >= 3 { out.producer_epoch = get_i16(buf)?; }
111 if version >= 6 { out.enable2_pc = get_bool(buf)?; }
112 if version >= 6 { out.keep_prepared_txn = get_bool(buf)?; }
113 if flex {
114 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
115 Ok(false)
116 })?;
117 }
118 Ok(out)
119 }
120}