crabka_protocol/opt/rustwide/workdir/generated/
ElectLeadersRequest.owned.rs1use bytes::{Buf, BufMut};
4
5use crate::primitives::fixed::{get_i32, get_i8, put_i32, put_i8};
6use crate::primitives::string_bytes::{
7 compact_string_len, get_compact_string_owned, get_string_owned,
8 put_compact_string, put_string, string_len,
9};
10use crate::tagged_fields::{read_tagged_fields, tagged_fields_len, WriteTaggedFields};
11use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
12
13pub const API_KEY: i16 = 43;
14pub const MIN_VERSION: i16 = 0;
15pub const MAX_VERSION: i16 = 2;
16pub const FLEXIBLE_MIN: i16 = 2;
17
18#[inline]
19fn is_flexible(version: i16) -> bool { version >= FLEXIBLE_MIN }
20
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub struct ElectLeadersRequest {
23 pub election_type: i8,
24 pub topic_partitions: Option<Vec<TopicPartitions>>,
25 pub timeout_ms: i32,
26 pub unknown_tagged_fields: UnknownTaggedFields,
27}
28
29impl Default for ElectLeadersRequest {
30 fn default() -> Self {
31 Self {
32 election_type: 0i8,
33 topic_partitions: None,
34 timeout_ms: 60_000i32,
35 unknown_tagged_fields: Default::default(),
36 }
37 }
38}
39
40impl Encode for ElectLeadersRequest {
41 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
42 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
43 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
44 }
45 let flex = is_flexible(version);
46 if version >= 1 { put_i8(buf, self.election_type) }
47 if version >= 0 { { let len = (self.topic_partitions).as_ref().map(Vec::len); crate::primitives::array::put_nullable_array_len(buf, len, flex); if let Some(v) = &self.topic_partitions { for it in v { it.encode(buf, version)?; } } } }
48 if version >= 0 { put_i32(buf, self.timeout_ms) }
49 if flex {
50 let tagged = WriteTaggedFields::new();
51 tagged.write(buf, &self.unknown_tagged_fields);
52 }
53 Ok(())
54 }
55 fn encoded_len(&self, version: i16) -> usize {
56 let flex = is_flexible(version);
57 let mut n: usize = 0;
58 if version >= 1 { n += 1; }
59 if version >= 0 { n += { let opt: Option<&Vec<_>> = (self.topic_partitions).as_ref(); let prefix = crate::primitives::array::nullable_array_len_prefix_len(opt.map(|v| v.len()), flex); let body: usize = opt.map_or(0, |v| v.iter().map(|it| it.encoded_len(version)).sum()); prefix + body }; }
60 if version >= 0 { n += 4; }
61 if flex {
62 let known_pairs: Vec<(u32, usize)> = Vec::new();
63 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
64 }
65 n
66 }
67}
68
69impl<'de> Decode<'de> for ElectLeadersRequest {
70 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
71 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
72 return Err(ProtocolError::UnsupportedVersion { api_key: API_KEY, version });
73 }
74 let flex = is_flexible(version);
75 let mut out = Self::default();
76 if version >= 1 { out.election_type = get_i8(buf)?; }
77 if version >= 0 { out.topic_partitions = { let opt = crate::primitives::array::get_nullable_array_len(buf, flex)?; match opt { None => None, Some(n) => { let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(TopicPartitions::decode(buf, version)?); } Some(v) } } }; }
78 if version >= 0 { out.timeout_ms = get_i32(buf)?; }
79 if flex {
80 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
81 Ok(false)
82 })?;
83 }
84 Ok(out)
85 }
86}
87
88#[derive(Debug, Clone, PartialEq, Eq, Default)]
89pub struct TopicPartitions {
90 pub topic: String,
91 pub partitions: Vec<i32>,
92 pub unknown_tagged_fields: UnknownTaggedFields,
93}
94
95impl Encode for TopicPartitions {
96 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
97 let flex = version >= 2;
98 if version >= 0 { if flex { put_compact_string(buf, &self.topic) } else { put_string(buf, &self.topic) } }
99 if version >= 0 { { crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex); for it in &self.partitions { put_i32(buf, *it); } } }
100 if flex {
101 let tagged = WriteTaggedFields::new();
102 tagged.write(buf, &self.unknown_tagged_fields);
103 }
104 Ok(())
105 }
106 fn encoded_len(&self, version: i16) -> usize {
107 let flex = version >= 2;
108 let mut n: usize = 0;
109 if version >= 0 { n += if flex { compact_string_len(&self.topic) } else { string_len(&self.topic) }; }
110 if version >= 0 { n += { let prefix = crate::primitives::array::array_len_prefix_len((self.partitions).len(), flex); let body: usize = (self.partitions).iter().map(|_| 4).sum(); prefix + body }; }
111 if flex {
112 let known_pairs: Vec<(u32, usize)> = Vec::new();
113 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
114 }
115 n
116 }
117}
118
119impl<'de> Decode<'de> for TopicPartitions {
120 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
121 let flex = version >= 2;
122 let mut out = Self::default();
123 if version >= 0 { out.topic = if flex { get_compact_string_owned(buf)? } else { get_string_owned(buf)? }; }
124 if version >= 0 { out.partitions = { let n = crate::primitives::array::get_array_len(buf, flex)?; let mut v = Vec::with_capacity(n); for _ in 0..n { v.push(get_i32(buf)?); } v }; }
125 if flex {
126 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| {
127 Ok(false)
128 })?;
129 }
130 Ok(out)
131 }
132}
133
134#[must_use]
137#[allow(unused_comparisons)]
138pub fn default_json(version: i16) -> ::serde_json::Value {
139 let mut obj = ::serde_json::Map::new();
140 if version >= 1 {
141 obj.insert("electionType".to_string(), ::serde_json::json!(0));
142 }
143 obj.insert("topicPartitions".to_string(), ::serde_json::Value::Null);
144 obj.insert("timeoutMs".to_string(), ::serde_json::json!(60000));
145 ::serde_json::Value::Object(obj)
146}
147
148impl crate::ProtocolRequest for ElectLeadersRequest {
149 const API_KEY: i16 = API_KEY;
150 const MIN_VERSION: i16 = MIN_VERSION;
151 const MAX_VERSION: i16 = MAX_VERSION;
152 const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
153 type Response = super::elect_leaders_response::ElectLeadersResponse;
154}