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