Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
ElectLeadersRequest.owned.rs

1// AUTO-GENERATED by krabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use bytes::{Buf, BufMut};
4
5use crate::{
6    Decode, Encode, ProtocolError, UnknownTaggedFields,
7    primitives::{
8        fixed::{get_i8, get_i32, put_i8, put_i32},
9        string_bytes::{
10            compact_string_len, get_compact_string_owned, get_string_owned, put_compact_string,
11            put_string, string_len,
12        },
13    },
14    tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
15};
16pub const API_KEY: i16 = 43;
17pub const MIN_VERSION: i16 = 0;
18pub const MAX_VERSION: i16 = 2;
19pub const LATEST_STABLE_VERSION: i16 = 2;
20pub const FLEXIBLE_MIN: i16 = 2;
21#[inline]
22#[must_use]
23pub fn is_flexible(version: i16) -> bool {
24    version >= FLEXIBLE_MIN
25}
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct ElectLeadersRequest {
28    pub election_type: i8,
29    pub topic_partitions: Option<Vec<TopicPartitions>>,
30    pub timeout_ms: i32,
31    pub unknown_tagged_fields: UnknownTaggedFields,
32}
33impl Default for ElectLeadersRequest {
34    fn default() -> Self {
35        Self {
36            election_type: 0i8,
37            topic_partitions: Some(Vec::new()),
38            timeout_ms: 60_000i32,
39            unknown_tagged_fields: UnknownTaggedFields::default(),
40        }
41    }
42}
43impl Encode for ElectLeadersRequest {
44    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
45        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
46            return Err(ProtocolError::UnsupportedVersion {
47                api_key: API_KEY,
48                version,
49            });
50        }
51        let flex = is_flexible(version);
52        if version >= 1 {
53            put_i8(buf, self.election_type);
54        }
55        if version >= 0 {
56            {
57                let len = (self.topic_partitions).as_ref().map(Vec::len);
58                crate::primitives::array::put_nullable_array_len(buf, len, flex);
59                if let Some(v) = &self.topic_partitions {
60                    for it in v {
61                        it.encode(buf, version)?;
62                    }
63                }
64            };
65        }
66        if version >= 0 {
67            put_i32(buf, self.timeout_ms);
68        }
69        if flex {
70            let tagged = WriteTaggedFields::new();
71            tagged.write(buf, &self.unknown_tagged_fields);
72        }
73        Ok(())
74    }
75    fn encoded_len(&self, version: i16) -> usize {
76        let flex = is_flexible(version);
77        let mut n: usize = 0;
78        if version >= 1 {
79            n += 1;
80        }
81        if version >= 0 {
82            n += {
83                let opt: Option<&Vec<_>> = (self.topic_partitions).as_ref();
84                let prefix = crate::primitives::array::nullable_array_len_prefix_len(
85                    opt.map(std::vec::Vec::len),
86                    flex,
87                );
88                let body: usize =
89                    opt.map_or(0, |v| v.iter().map(|it| it.encoded_len(version)).sum());
90                prefix + body
91            };
92        }
93        if version >= 0 {
94            n += 4;
95        }
96        if flex {
97            let known_pairs: Vec<(u32, usize)> = Vec::new();
98            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
99        }
100        n
101    }
102}
103impl Decode<'_> for ElectLeadersRequest {
104    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
105        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
106            return Err(ProtocolError::UnsupportedVersion {
107                api_key: API_KEY,
108                version,
109            });
110        }
111        let flex = is_flexible(version);
112        let mut out = Self::default();
113        if version >= 1 {
114            out.election_type = get_i8(buf)?;
115        }
116        if version >= 0 {
117            out.topic_partitions = {
118                let opt = crate::primitives::array::get_nullable_array_len(buf, flex)?;
119                match opt {
120                    None => None,
121                    Some(n) => {
122                        let mut v = Vec::with_capacity(n);
123                        for _ in 0..n {
124                            v.push(TopicPartitions::decode(buf, version)?);
125                        }
126                        Some(v)
127                    }
128                }
129            };
130        }
131        if version >= 0 {
132            out.timeout_ms = get_i32(buf)?;
133        }
134        if flex {
135            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
136        }
137        Ok(out)
138    }
139}
140#[cfg(test)]
141impl ElectLeadersRequest {
142    #[must_use]
143    pub fn populated(version: i16) -> Self {
144        let mut m = Self::default();
145        if version >= 1 {
146            m.election_type = 1i8;
147        }
148        if version >= 0 {
149            m.topic_partitions = Some(vec![TopicPartitions::populated(version)]);
150        }
151        if version >= 0 {
152            m.timeout_ms = 1i32;
153        }
154        m
155    }
156}
157#[derive(Debug, Clone, PartialEq, Eq, Default)]
158pub struct TopicPartitions {
159    pub topic: String,
160    pub partitions: Vec<i32>,
161    pub unknown_tagged_fields: UnknownTaggedFields,
162}
163impl Encode for TopicPartitions {
164    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
165        let flex = version >= 2;
166        if version >= 0 {
167            if flex {
168                let () = put_compact_string(buf, &self.topic);
169            } else {
170                let () = put_string(buf, &self.topic);
171            }
172        }
173        if version >= 0 {
174            {
175                crate::primitives::array::put_array_len(buf, (self.partitions).len(), flex);
176                for it in &self.partitions {
177                    put_i32(buf, *it);
178                }
179            };
180        }
181        if flex {
182            let tagged = WriteTaggedFields::new();
183            tagged.write(buf, &self.unknown_tagged_fields);
184        }
185        Ok(())
186    }
187    fn encoded_len(&self, version: i16) -> usize {
188        let flex = version >= 2;
189        let mut n: usize = 0;
190        if version >= 0 {
191            n += if flex {
192                compact_string_len(&self.topic)
193            } else {
194                string_len(&self.topic)
195            };
196        }
197        if version >= 0 {
198            n += {
199                let prefix =
200                    crate::primitives::array::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 = crate::primitives::array::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_string();
246        }
247        if version >= 0 {
248            m.partitions = vec![1i32];
249        }
250        m
251    }
252}
253/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
254/// Only includes fields valid for the given version.
255#[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_string(), ::serde_json::json!(0));
261    }
262    obj.insert(
263        "topicPartitions".to_string(),
264        ::serde_json::Value::Array(vec![]),
265    );
266    obj.insert("timeoutMs".to_string(), ::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}