Skip to main content

crabka_protocol/opt/rustwide/workdir/generated/
AddRaftVoterRequest.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_bool, get_i32, get_u16, put_bool, put_i32, put_u16};
6use crate::primitives::string_bytes::{
7    compact_nullable_string_len, compact_string_len, get_compact_nullable_string_owned,
8    get_compact_string_owned, get_nullable_string_owned, get_string_owned, nullable_string_len,
9    put_compact_nullable_string, put_compact_string, put_nullable_string, put_string, string_len,
10};
11use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
12use crate::{Decode, Encode, ProtocolError, UnknownTaggedFields};
13
14pub const API_KEY: i16 = 80;
15pub const MIN_VERSION: i16 = 0;
16pub const MAX_VERSION: i16 = 1;
17pub const FLEXIBLE_MIN: i16 = 0;
18
19#[inline]
20fn is_flexible(version: i16) -> bool {
21    version >= FLEXIBLE_MIN
22}
23
24#[derive(Debug, Clone, PartialEq, Eq)]
25pub struct AddRaftVoterRequest {
26    pub cluster_id: Option<String>,
27    pub timeout_ms: i32,
28    pub voter_id: i32,
29    pub voter_directory_id: crate::primitives::uuid::Uuid,
30    pub listeners: Vec<Listener>,
31    pub ack_when_committed: bool,
32    pub unknown_tagged_fields: UnknownTaggedFields,
33}
34impl Default for AddRaftVoterRequest {
35    fn default() -> Self {
36        Self {
37            cluster_id: None,
38            timeout_ms: 0i32,
39            voter_id: 0i32,
40            voter_directory_id: Default::default(),
41            listeners: Vec::new(),
42            ack_when_committed: true,
43            unknown_tagged_fields: Default::default(),
44        }
45    }
46}
47impl Encode for AddRaftVoterRequest {
48    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
49        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
50            return Err(ProtocolError::UnsupportedVersion {
51                api_key: API_KEY,
52                version,
53            });
54        }
55        let flex = is_flexible(version);
56        if version >= 0 {
57            if flex {
58                put_compact_nullable_string(buf, self.cluster_id.as_deref());
59            } else {
60                put_nullable_string(buf, self.cluster_id.as_deref());
61            }
62        }
63        if version >= 0 {
64            put_i32(buf, self.timeout_ms);
65        }
66        if version >= 0 {
67            put_i32(buf, self.voter_id);
68        }
69        if version >= 0 {
70            crate::primitives::uuid::put_uuid(buf, self.voter_directory_id);
71        }
72        if version >= 0 {
73            {
74                crate::primitives::array::put_array_len(buf, (self.listeners).len(), flex);
75                for it in &self.listeners {
76                    it.encode(buf, version)?;
77                }
78            }
79        }
80        if version >= 1 {
81            put_bool(buf, self.ack_when_committed);
82        }
83        if flex {
84            let tagged = WriteTaggedFields::new();
85            tagged.write(buf, &self.unknown_tagged_fields);
86        }
87        Ok(())
88    }
89    fn encoded_len(&self, version: i16) -> usize {
90        let flex = is_flexible(version);
91        let mut n: usize = 0;
92        if version >= 0 {
93            n += if flex {
94                compact_nullable_string_len(self.cluster_id.as_deref())
95            } else {
96                nullable_string_len(self.cluster_id.as_deref())
97            };
98        }
99        if version >= 0 {
100            n += 4;
101        }
102        if version >= 0 {
103            n += 4;
104        }
105        if version >= 0 {
106            n += 16;
107        }
108        if version >= 0 {
109            n += {
110                let prefix =
111                    crate::primitives::array::array_len_prefix_len((self.listeners).len(), flex);
112                let body: usize = (self.listeners)
113                    .iter()
114                    .map(|it| it.encoded_len(version))
115                    .sum();
116                prefix + body
117            };
118        }
119        if version >= 1 {
120            n += 1;
121        }
122        if flex {
123            let known_pairs: Vec<(u32, usize)> = Vec::new();
124            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
125        }
126        n
127    }
128}
129impl Decode<'_> for AddRaftVoterRequest {
130    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
131        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
132            return Err(ProtocolError::UnsupportedVersion {
133                api_key: API_KEY,
134                version,
135            });
136        }
137        let flex = is_flexible(version);
138        let mut out = Self::default();
139        if version >= 0 {
140            out.cluster_id = if flex {
141                get_compact_nullable_string_owned(buf)?
142            } else {
143                get_nullable_string_owned(buf)?
144            };
145        }
146        if version >= 0 {
147            out.timeout_ms = get_i32(buf)?;
148        }
149        if version >= 0 {
150            out.voter_id = get_i32(buf)?;
151        }
152        if version >= 0 {
153            out.voter_directory_id = crate::primitives::uuid::get_uuid(buf)?;
154        }
155        if version >= 0 {
156            out.listeners = {
157                let n = crate::primitives::array::get_array_len(buf, flex)?;
158                let mut v = Vec::with_capacity(n);
159                for _ in 0..n {
160                    v.push(Listener::decode(buf, version)?);
161                }
162                v
163            };
164        }
165        if version >= 1 {
166            out.ack_when_committed = get_bool(buf)?;
167        }
168        if flex {
169            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
170        }
171        Ok(out)
172    }
173}
174#[cfg(test)]
175impl AddRaftVoterRequest {
176    #[must_use]
177    pub fn populated(version: i16) -> Self {
178        let mut m = Self::default();
179        if version >= 0 {
180            m.cluster_id = Some("x".to_string());
181        }
182        if version >= 0 {
183            m.timeout_ms = 1i32;
184        }
185        if version >= 0 {
186            m.voter_id = 1i32;
187        }
188        if version >= 0 {
189            m.voter_directory_id = crate::primitives::uuid::Uuid([1u8; 16]);
190        }
191        if version >= 0 {
192            m.listeners = vec![Listener::populated(version)];
193        }
194        if version >= 1 {
195            m.ack_when_committed = true;
196        }
197        m
198    }
199}
200#[derive(Debug, Clone, PartialEq, Eq, Default)]
201pub struct Listener {
202    pub name: String,
203    pub host: String,
204    pub port: u16,
205    pub unknown_tagged_fields: UnknownTaggedFields,
206}
207impl Encode for Listener {
208    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
209        let flex = version >= 0;
210        if version >= 0 {
211            if flex {
212                put_compact_string(buf, &self.name);
213            } else {
214                put_string(buf, &self.name);
215            }
216        }
217        if version >= 0 {
218            if flex {
219                put_compact_string(buf, &self.host);
220            } else {
221                put_string(buf, &self.host);
222            }
223        }
224        if version >= 0 {
225            put_u16(buf, self.port);
226        }
227        if flex {
228            let tagged = WriteTaggedFields::new();
229            tagged.write(buf, &self.unknown_tagged_fields);
230        }
231        Ok(())
232    }
233    fn encoded_len(&self, version: i16) -> usize {
234        let flex = version >= 0;
235        let mut n: usize = 0;
236        if version >= 0 {
237            n += if flex {
238                compact_string_len(&self.name)
239            } else {
240                string_len(&self.name)
241            };
242        }
243        if version >= 0 {
244            n += if flex {
245                compact_string_len(&self.host)
246            } else {
247                string_len(&self.host)
248            };
249        }
250        if version >= 0 {
251            n += 2;
252        }
253        if flex {
254            let known_pairs: Vec<(u32, usize)> = Vec::new();
255            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
256        }
257        n
258    }
259}
260impl Decode<'_> for Listener {
261    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
262        let flex = version >= 0;
263        let mut out = Self::default();
264        if version >= 0 {
265            out.name = if flex {
266                get_compact_string_owned(buf)?
267            } else {
268                get_string_owned(buf)?
269            };
270        }
271        if version >= 0 {
272            out.host = if flex {
273                get_compact_string_owned(buf)?
274            } else {
275                get_string_owned(buf)?
276            };
277        }
278        if version >= 0 {
279            out.port = get_u16(buf)?;
280        }
281        if flex {
282            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
283        }
284        Ok(out)
285    }
286}
287#[cfg(test)]
288impl Listener {
289    #[must_use]
290    pub fn populated(version: i16) -> Self {
291        let mut m = Self::default();
292        if version >= 0 {
293            m.name = "x".to_string();
294        }
295        if version >= 0 {
296            m.host = "x".to_string();
297        }
298        if version >= 0 {
299            m.port = 1u16;
300        }
301        m
302    }
303}
304
305/// Default JSON payload matching `Self::default()` for JVM oracle differential testing.
306/// Only includes fields valid for the given version.
307#[must_use]
308#[allow(unused_comparisons)]
309pub fn default_json(version: i16) -> ::serde_json::Value {
310    let mut obj = ::serde_json::Map::new();
311    obj.insert("clusterId".to_string(), ::serde_json::Value::Null);
312    obj.insert("timeoutMs".to_string(), ::serde_json::json!(0));
313    obj.insert("voterId".to_string(), ::serde_json::json!(0));
314    obj.insert(
315        "voterDirectoryId".to_string(),
316        ::serde_json::Value::String("AAAAAAAAAAAAAAAAAAAAAA".to_string()),
317    );
318    obj.insert("listeners".to_string(), ::serde_json::Value::Array(vec![]));
319    if version >= 1 {
320        obj.insert(
321            "ackWhenCommitted".to_string(),
322            ::serde_json::Value::Bool(true),
323        );
324    }
325    ::serde_json::Value::Object(obj)
326}
327
328impl crate::ProtocolRequest for AddRaftVoterRequest {
329    const API_KEY: i16 = API_KEY;
330    const MIN_VERSION: i16 = MIN_VERSION;
331    const MAX_VERSION: i16 = MAX_VERSION;
332    const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
333    type Response = super::add_raft_voter_response::AddRaftVoterResponse;
334}