Skip to main content

krabka_protocol/opt/rustwide/workdir/generated/
AddRaftVoterRequest.owned.rs

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