krabka_protocol/opt/rustwide/workdir/generated/
ElectLeadersRequest.owned.rs1use bytes::{Buf, BufMut};
4
5use crate::{
6 Decode, Encode, ProtocolError, UnknownTaggedFields,
7 primitives::{
8 array::{
9 array_len_prefix_len, get_array_len, get_nullable_array_len,
10 nullable_array_len_prefix_len, put_array_len, put_nullable_array_len,
11 },
12 fixed::{get_i8, get_i32, put_i8, put_i32},
13 string_bytes::{
14 compact_string_len, get_compact_string_owned, get_string_owned, put_compact_string,
15 put_string, string_len,
16 },
17 },
18 tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
19};
20pub const API_KEY: i16 = 43;
21pub const MIN_VERSION: i16 = 0;
22pub const MAX_VERSION: i16 = 2;
23pub const LATEST_STABLE_VERSION: i16 = 2;
24pub const FLEXIBLE_MIN: i16 = 2;
25#[inline]
26#[must_use]
27pub fn is_flexible(version: i16) -> bool {
28 version >= FLEXIBLE_MIN
29}
30#[derive(Debug, Clone, PartialEq, Eq)]
31pub struct ElectLeadersRequest {
32 pub election_type: i8,
33 pub topic_partitions: Option<Vec<TopicPartitions>>,
34 pub timeout_ms: i32,
35 pub unknown_tagged_fields: UnknownTaggedFields,
36}
37impl Default for ElectLeadersRequest {
38 fn default() -> Self {
39 Self {
40 election_type: 0i8,
41 topic_partitions: Some(Vec::new()),
42 timeout_ms: 60_000i32,
43 unknown_tagged_fields: UnknownTaggedFields::default(),
44 }
45 }
46}
47impl Encode for ElectLeadersRequest {
48 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
49 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
50 return Err(ProtocolError::UnsupportedVersion {
51 api_key: API_KEY,
52 version,
53 });
54 }
55 let flex = is_flexible(version);
56 if version >= 1 {
57 put_i8(buf, self.election_type);
58 }
59 if version >= 0 {
60 {
61 let len = (self.topic_partitions).as_ref().map(Vec::len);
62 put_nullable_array_len(buf, len, flex)?;
63 if let Some(v) = &self.topic_partitions {
64 for it in v {
65 it.encode(buf, version)?;
66 }
67 }
68 }
69 }
70 if version >= 0 {
71 put_i32(buf, self.timeout_ms);
72 }
73 if flex {
74 let tagged = WriteTaggedFields::new();
75 tagged.write(buf, &self.unknown_tagged_fields)?;
76 }
77 Ok(())
78 }
79 fn encoded_len(&self, version: i16) -> usize {
80 let flex = is_flexible(version);
81 let mut n: usize = 0;
82 if version >= 1 {
83 n += 1;
84 }
85 if version >= 0 {
86 n += {
87 let opt: Option<&Vec<_>> = (self.topic_partitions).as_ref();
88 let prefix = nullable_array_len_prefix_len(opt.map(Vec::len), flex);
89 let body: usize =
90 opt.map_or(0, |v| v.iter().map(|it| it.encoded_len(version)).sum());
91 prefix + body
92 };
93 }
94 if version >= 0 {
95 n += 4;
96 }
97 if flex {
98 let known_pairs: Vec<(u32, usize)> = Vec::new();
99 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
100 }
101 n
102 }
103}
104impl Decode<'_> for ElectLeadersRequest {
105 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
106 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
107 return Err(ProtocolError::UnsupportedVersion {
108 api_key: API_KEY,
109 version,
110 });
111 }
112 let flex = is_flexible(version);
113 let mut out = Self::default();
114 if version >= 1 {
115 out.election_type = get_i8(buf)?;
116 }
117 if version >= 0 {
118 out.topic_partitions = {
119 let opt = get_nullable_array_len(buf, flex)?;
120 match opt {
121 None => None,
122 Some(n) => {
123 let mut v = Vec::with_capacity(n);
124 for _ in 0..n {
125 v.push(TopicPartitions::decode(buf, version)?);
126 }
127 Some(v)
128 }
129 }
130 };
131 }
132 if version >= 0 {
133 out.timeout_ms = get_i32(buf)?;
134 }
135 if flex {
136 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
137 }
138 Ok(out)
139 }
140}
141#[cfg(test)]
142impl ElectLeadersRequest {
143 #[must_use]
144 pub fn populated(version: i16) -> Self {
145 let mut m = Self::default();
146 if version >= 1 {
147 m.election_type = 1i8;
148 }
149 if version >= 0 {
150 m.topic_partitions = Some(vec![TopicPartitions::populated(version)]);
151 }
152 if version >= 0 {
153 m.timeout_ms = 1i32;
154 }
155 m
156 }
157}
158#[derive(Debug, Clone, PartialEq, Eq, Default)]
159pub struct TopicPartitions {
160 pub topic: String,
161 pub partitions: Vec<i32>,
162 pub unknown_tagged_fields: UnknownTaggedFields,
163}
164impl Encode for TopicPartitions {
165 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
166 let flex = version >= 2;
167 if version >= 0 {
168 if flex {
169 put_compact_string(buf, &self.topic)?;
170 } else {
171 put_string(buf, &self.topic)?;
172 }
173 }
174 if version >= 0 {
175 {
176 put_array_len(buf, (self.partitions).len(), flex)?;
177 for it in &self.partitions {
178 put_i32(buf, *it);
179 }
180 }
181 }
182 if flex {
183 let tagged = WriteTaggedFields::new();
184 tagged.write(buf, &self.unknown_tagged_fields)?;
185 }
186 Ok(())
187 }
188 fn encoded_len(&self, version: i16) -> usize {
189 let flex = version >= 2;
190 let mut n: usize = 0;
191 if version >= 0 {
192 n += if flex {
193 compact_string_len(&self.topic)
194 } else {
195 string_len(&self.topic)
196 };
197 }
198 if version >= 0 {
199 n += {
200 let prefix = array_len_prefix_len((self.partitions).len(), flex);
201 let body: usize = (self.partitions).iter().map(|_| 4).sum();
202 prefix + body
203 };
204 }
205 if flex {
206 let known_pairs: Vec<(u32, usize)> = Vec::new();
207 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
208 }
209 n
210 }
211}
212impl Decode<'_> for TopicPartitions {
213 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
214 let flex = version >= 2;
215 let mut out = Self::default();
216 if version >= 0 {
217 out.topic = if flex {
218 get_compact_string_owned(buf)?
219 } else {
220 get_string_owned(buf)?
221 };
222 }
223 if version >= 0 {
224 out.partitions = {
225 let n = get_array_len(buf, flex)?;
226 let mut v = Vec::with_capacity(n);
227 for _ in 0..n {
228 v.push(get_i32(buf)?);
229 }
230 v
231 };
232 }
233 if flex {
234 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
235 }
236 Ok(out)
237 }
238}
239#[cfg(test)]
240impl TopicPartitions {
241 #[must_use]
242 pub fn populated(version: i16) -> Self {
243 let mut m = Self::default();
244 if version >= 0 {
245 m.topic = "x".to_owned();
246 }
247 if version >= 0 {
248 m.partitions = vec![1i32];
249 }
250 m
251 }
252}
253#[must_use]
256#[allow(unused_comparisons)]
257pub fn default_json(version: i16) -> ::serde_json::Value {
258 let mut obj = ::serde_json::Map::new();
259 if version >= 1 {
260 obj.insert("electionType".to_owned(), ::serde_json::json!(0));
261 }
262 obj.insert(
263 "topicPartitions".to_owned(),
264 ::serde_json::Value::Array(vec![]),
265 );
266 obj.insert("timeoutMs".to_owned(), ::serde_json::json!(60_000));
267 ::serde_json::Value::Object(obj)
268}
269impl crate::ProtocolRequest for ElectLeadersRequest {
270 const API_KEY: i16 = API_KEY;
271 const MIN_VERSION: i16 = MIN_VERSION;
272 const MAX_VERSION: i16 = MAX_VERSION;
273 const LATEST_STABLE_VERSION: i16 = LATEST_STABLE_VERSION;
274 const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
275 type Response = super::elect_leaders_response::ElectLeadersResponse;
276}