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