Skip to main content

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