Skip to main content

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

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use bytes::BufMut;
4
5use crate::primitives::fixed::{get_i16, get_i32, get_i64, put_i16, put_i32, put_i64};
6use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
7use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
8
9pub const API_KEY: i16 = 22;
10pub const MIN_VERSION: i16 = 0;
11pub const MAX_VERSION: i16 = 6;
12pub const FLEXIBLE_MIN: i16 = 2;
13
14#[inline]
15fn is_flexible(version: i16) -> bool {
16    version >= FLEXIBLE_MIN
17}
18
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct InitProducerIdResponse {
21    pub throttle_time_ms: i32,
22    pub error_code: i16,
23    pub producer_id: i64,
24    pub producer_epoch: i16,
25    pub ongoing_txn_producer_id: i64,
26    pub ongoing_txn_producer_epoch: i16,
27    pub unknown_tagged_fields: UnknownTaggedFields,
28}
29impl Default for InitProducerIdResponse {
30    fn default() -> Self {
31        Self {
32            throttle_time_ms: 0i32,
33            error_code: 0i16,
34            producer_id: -1i64,
35            producer_epoch: 0i16,
36            ongoing_txn_producer_id: -1i64,
37            ongoing_txn_producer_epoch: -1i16,
38            unknown_tagged_fields: Default::default(),
39        }
40    }
41}
42impl InitProducerIdResponse {
43    pub fn to_owned(&self) -> crate::owned::init_producer_id_response::InitProducerIdResponse {
44        crate::owned::init_producer_id_response::InitProducerIdResponse {
45            throttle_time_ms: (self.throttle_time_ms),
46            error_code: (self.error_code),
47            producer_id: (self.producer_id),
48            producer_epoch: (self.producer_epoch),
49            ongoing_txn_producer_id: (self.ongoing_txn_producer_id),
50            ongoing_txn_producer_epoch: (self.ongoing_txn_producer_epoch),
51            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
52        }
53    }
54}
55impl Encode for InitProducerIdResponse {
56    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
57        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
58            return Err(ProtocolError::UnsupportedVersion {
59                api_key: API_KEY,
60                version,
61            });
62        }
63        let flex = is_flexible(version);
64        if version >= 0 {
65            put_i32(buf, self.throttle_time_ms);
66        }
67        if version >= 0 {
68            put_i16(buf, self.error_code);
69        }
70        if version >= 0 {
71            put_i64(buf, self.producer_id);
72        }
73        if version >= 0 {
74            put_i16(buf, self.producer_epoch);
75        }
76        if version >= 6 {
77            put_i64(buf, self.ongoing_txn_producer_id);
78        }
79        if version >= 6 {
80            put_i16(buf, self.ongoing_txn_producer_epoch);
81        }
82        if flex {
83            let tagged = WriteTaggedFields::new();
84            tagged.write(buf, &self.unknown_tagged_fields);
85        }
86        Ok(())
87    }
88    fn encoded_len(&self, version: i16) -> usize {
89        let flex = is_flexible(version);
90        let mut n: usize = 0;
91        if version >= 0 {
92            n += 4;
93        }
94        if version >= 0 {
95            n += 2;
96        }
97        if version >= 0 {
98            n += 8;
99        }
100        if version >= 0 {
101            n += 2;
102        }
103        if version >= 6 {
104            n += 8;
105        }
106        if version >= 6 {
107            n += 2;
108        }
109        if flex {
110            let known_pairs: Vec<(u32, usize)> = Vec::new();
111            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
112        }
113        n
114    }
115}
116impl<'de> DecodeBorrow<'de> for InitProducerIdResponse {
117    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
118        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
119            return Err(ProtocolError::UnsupportedVersion {
120                api_key: API_KEY,
121                version,
122            });
123        }
124        let flex = is_flexible(version);
125        let mut out = Self::default();
126        if version >= 0 {
127            out.throttle_time_ms = get_i32(buf)?;
128        }
129        if version >= 0 {
130            out.error_code = get_i16(buf)?;
131        }
132        if version >= 0 {
133            out.producer_id = get_i64(buf)?;
134        }
135        if version >= 0 {
136            out.producer_epoch = get_i16(buf)?;
137        }
138        if version >= 6 {
139            out.ongoing_txn_producer_id = get_i64(buf)?;
140        }
141        if version >= 6 {
142            out.ongoing_txn_producer_epoch = get_i16(buf)?;
143        }
144        if flex {
145            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
146        }
147        Ok(out)
148    }
149}
150#[cfg(test)]
151impl InitProducerIdResponse {
152    #[must_use]
153    pub fn populated(version: i16) -> Self {
154        let mut m = Self::default();
155        if version >= 0 {
156            m.throttle_time_ms = 1i32;
157        }
158        if version >= 0 {
159            m.error_code = 1i16;
160        }
161        if version >= 0 {
162            m.producer_id = 1i64;
163        }
164        if version >= 0 {
165            m.producer_epoch = 1i16;
166        }
167        if version >= 6 {
168            m.ongoing_txn_producer_id = 1i64;
169        }
170        if version >= 6 {
171            m.ongoing_txn_producer_epoch = 1i16;
172        }
173        m
174    }
175}