Skip to main content

crabka_protocol/opt/rustwide/workdir/generated/
InitProducerIdRequest.borrowed.rs

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use bytes::BufMut;
4
5use crate::primitives::fixed::{
6    get_bool, get_i16, get_i32, get_i64, put_bool, put_i16, put_i32, put_i64,
7};
8use crate::primitives::string_bytes::{
9    compact_nullable_string_len, nullable_string_len, put_compact_nullable_string,
10    put_nullable_string,
11};
12use crate::primitives::string_bytes_borrowed::{
13    get_compact_nullable_string_borrowed, get_nullable_string_borrowed,
14};
15use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
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 {
25    version >= FLEXIBLE_MIN
26}
27
28#[derive(Debug, Clone, PartialEq, Eq)]
29pub struct InitProducerIdRequest<'a> {
30    pub transactional_id: Option<&'a str>,
31    pub transaction_timeout_ms: i32,
32    pub producer_id: i64,
33    pub producer_epoch: i16,
34    pub enable2_pc: bool,
35    pub keep_prepared_txn: bool,
36    pub unknown_tagged_fields: UnknownTaggedFields,
37}
38impl Default for InitProducerIdRequest<'_> {
39    fn default() -> Self {
40        Self {
41            transactional_id: None,
42            transaction_timeout_ms: 0i32,
43            producer_id: -1i64,
44            producer_epoch: -1i16,
45            enable2_pc: false,
46            keep_prepared_txn: false,
47            unknown_tagged_fields: Default::default(),
48        }
49    }
50}
51impl InitProducerIdRequest<'_> {
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(std::string::ToString::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}
64impl Encode for InitProducerIdRequest<'_> {
65    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
66        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
67            return Err(ProtocolError::UnsupportedVersion {
68                api_key: API_KEY,
69                version,
70            });
71        }
72        let flex = is_flexible(version);
73        if version >= 0 {
74            if flex {
75                put_compact_nullable_string(buf, self.transactional_id);
76            } else {
77                put_nullable_string(buf, self.transactional_id);
78            }
79        }
80        if version >= 0 {
81            put_i32(buf, self.transaction_timeout_ms);
82        }
83        if version >= 3 {
84            put_i64(buf, self.producer_id);
85        }
86        if version >= 3 {
87            put_i16(buf, self.producer_epoch);
88        }
89        if version >= 6 {
90            put_bool(buf, self.enable2_pc);
91        }
92        if version >= 6 {
93            put_bool(buf, self.keep_prepared_txn);
94        }
95        if flex {
96            let tagged = WriteTaggedFields::new();
97            tagged.write(buf, &self.unknown_tagged_fields);
98        }
99        Ok(())
100    }
101    fn encoded_len(&self, version: i16) -> usize {
102        let flex = is_flexible(version);
103        let mut n: usize = 0;
104        if version >= 0 {
105            n += if flex {
106                compact_nullable_string_len(self.transactional_id)
107            } else {
108                nullable_string_len(self.transactional_id)
109            };
110        }
111        if version >= 0 {
112            n += 4;
113        }
114        if version >= 3 {
115            n += 8;
116        }
117        if version >= 3 {
118            n += 2;
119        }
120        if version >= 6 {
121            n += 1;
122        }
123        if version >= 6 {
124            n += 1;
125        }
126        if flex {
127            let known_pairs: Vec<(u32, usize)> = Vec::new();
128            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
129        }
130        n
131    }
132}
133impl<'de> DecodeBorrow<'de> for InitProducerIdRequest<'de> {
134    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
135        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
136            return Err(ProtocolError::UnsupportedVersion {
137                api_key: API_KEY,
138                version,
139            });
140        }
141        let flex = is_flexible(version);
142        let mut out = Self::default();
143        if version >= 0 {
144            out.transactional_id = if flex {
145                get_compact_nullable_string_borrowed(buf)?
146            } else {
147                get_nullable_string_borrowed(buf)?
148            };
149        }
150        if version >= 0 {
151            out.transaction_timeout_ms = get_i32(buf)?;
152        }
153        if version >= 3 {
154            out.producer_id = get_i64(buf)?;
155        }
156        if version >= 3 {
157            out.producer_epoch = get_i16(buf)?;
158        }
159        if version >= 6 {
160            out.enable2_pc = get_bool(buf)?;
161        }
162        if version >= 6 {
163            out.keep_prepared_txn = get_bool(buf)?;
164        }
165        if flex {
166            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
167        }
168        Ok(out)
169    }
170}
171#[cfg(test)]
172impl InitProducerIdRequest<'_> {
173    #[must_use]
174    pub fn populated(version: i16) -> Self {
175        let mut m = Self::default();
176        if version >= 0 {
177            m.transactional_id = Some("x");
178        }
179        if version >= 0 {
180            m.transaction_timeout_ms = 1i32;
181        }
182        if version >= 3 {
183            m.producer_id = 1i64;
184        }
185        if version >= 3 {
186            m.producer_epoch = 1i16;
187        }
188        if version >= 6 {
189            m.enable2_pc = true;
190        }
191        if version >= 6 {
192            m.keep_prepared_txn = true;
193        }
194        m
195    }
196}