Skip to main content

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

1// AUTO-GENERATED by krabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2use bytes::BufMut;
3
4use crate::{
5    DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields,
6    primitives::{
7        fixed::{get_bool, get_i16, get_i32, get_i64, put_bool, put_i16, put_i32, put_i64},
8        string_bytes::{
9            compact_nullable_string_len, nullable_string_len, put_compact_nullable_string,
10            put_nullable_string,
11        },
12        string_bytes_borrowed::{
13            get_compact_nullable_string_borrowed, get_nullable_string_borrowed,
14        },
15    },
16    tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
17};
18pub const API_KEY: i16 = 22;
19pub const MIN_VERSION: i16 = 0;
20pub const MAX_VERSION: i16 = 6;
21pub const LATEST_STABLE_VERSION: i16 = 5;
22pub const FLEXIBLE_MIN: i16 = 2;
23#[inline]
24#[must_use]
25pub fn is_flexible(version: i16) -> bool {
26    version >= FLEXIBLE_MIN
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: Some(""),
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: UnknownTaggedFields::default(),
48        }
49    }
50}
51impl InitProducerIdRequest<'_> {
52    /// # Panics
53    ///
54    /// Panics if a records field contains an invalid encoded record batch.
55    #[must_use]
56    pub fn to_owned(&self) -> crate::owned::init_producer_id_request::InitProducerIdRequest {
57        crate::owned::init_producer_id_request::InitProducerIdRequest {
58            transactional_id: (self.transactional_id).map(std::string::ToString::to_string),
59            transaction_timeout_ms: (self.transaction_timeout_ms),
60            producer_id: (self.producer_id),
61            producer_epoch: (self.producer_epoch),
62            enable2_pc: (self.enable2_pc),
63            keep_prepared_txn: (self.keep_prepared_txn),
64            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
65        }
66    }
67}
68impl Encode for InitProducerIdRequest<'_> {
69    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
70        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
71            return Err(ProtocolError::UnsupportedVersion {
72                api_key: API_KEY,
73                version,
74            });
75        }
76        let flex = is_flexible(version);
77        if version >= 0 {
78            if flex {
79                let () = put_compact_nullable_string(buf, self.transactional_id);
80            } else {
81                let () = put_nullable_string(buf, self.transactional_id);
82            }
83        }
84        if version >= 0 {
85            put_i32(buf, self.transaction_timeout_ms);
86        }
87        if version >= 3 {
88            put_i64(buf, self.producer_id);
89        }
90        if version >= 3 {
91            put_i16(buf, self.producer_epoch);
92        }
93        if version >= 6 {
94            put_bool(buf, self.enable2_pc);
95        }
96        if version >= 6 {
97            put_bool(buf, self.keep_prepared_txn);
98        }
99        if flex {
100            let tagged = WriteTaggedFields::new();
101            tagged.write(buf, &self.unknown_tagged_fields);
102        }
103        Ok(())
104    }
105    fn encoded_len(&self, version: i16) -> usize {
106        let flex = is_flexible(version);
107        let mut n: usize = 0;
108        if version >= 0 {
109            n += if flex {
110                compact_nullable_string_len(self.transactional_id)
111            } else {
112                nullable_string_len(self.transactional_id)
113            };
114        }
115        if version >= 0 {
116            n += 4;
117        }
118        if version >= 3 {
119            n += 8;
120        }
121        if version >= 3 {
122            n += 2;
123        }
124        if version >= 6 {
125            n += 1;
126        }
127        if version >= 6 {
128            n += 1;
129        }
130        if flex {
131            let known_pairs: Vec<(u32, usize)> = Vec::new();
132            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
133        }
134        n
135    }
136}
137impl<'de> DecodeBorrow<'de> for InitProducerIdRequest<'de> {
138    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
139        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
140            return Err(ProtocolError::UnsupportedVersion {
141                api_key: API_KEY,
142                version,
143            });
144        }
145        let flex = is_flexible(version);
146        let mut out = Self::default();
147        if version >= 0 {
148            out.transactional_id = if flex {
149                get_compact_nullable_string_borrowed(buf)?
150            } else {
151                get_nullable_string_borrowed(buf)?
152            };
153        }
154        if version >= 0 {
155            out.transaction_timeout_ms = get_i32(buf)?;
156        }
157        if version >= 3 {
158            out.producer_id = get_i64(buf)?;
159        }
160        if version >= 3 {
161            out.producer_epoch = get_i16(buf)?;
162        }
163        if version >= 6 {
164            out.enable2_pc = get_bool(buf)?;
165        }
166        if version >= 6 {
167            out.keep_prepared_txn = get_bool(buf)?;
168        }
169        if flex {
170            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
171        }
172        Ok(out)
173    }
174}
175#[cfg(test)]
176impl InitProducerIdRequest<'_> {
177    #[must_use]
178    pub fn populated(version: i16) -> Self {
179        let mut m = Self::default();
180        if version >= 0 {
181            m.transactional_id = Some("x");
182        }
183        if version >= 0 {
184            m.transaction_timeout_ms = 1i32;
185        }
186        if version >= 3 {
187            m.producer_id = 1i64;
188        }
189        if version >= 3 {
190            m.producer_epoch = 1i16;
191        }
192        if version >= 6 {
193            m.enable2_pc = true;
194        }
195        if version >= 6 {
196            m.keep_prepared_txn = true;
197        }
198        m
199    }
200}