Skip to main content

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

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