Skip to main content

crabka_protocol/opt/rustwide/workdir/generated/
ControllerRegistrationRequest.borrowed.rs

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use bytes::BufMut;
4
5use crate::primitives::fixed::{
6    get_bool, get_i16, get_i32, get_u16, put_bool, put_i16, put_i32, put_u16,
7};
8use crate::primitives::string_bytes::{
9    compact_string_len, put_compact_string, put_string, string_len,
10};
11use crate::primitives::string_bytes_borrowed::{get_compact_string_borrowed, get_string_borrowed};
12use crate::tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len};
13use crate::{DecodeBorrow, Encode, ProtocolError, UnknownTaggedFields};
14
15pub const API_KEY: i16 = 70;
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 ControllerRegistrationRequest<'a> {
27    pub controller_id: i32,
28    pub incarnation_id: crate::primitives::uuid::Uuid,
29    pub zk_migration_ready: bool,
30    pub listeners: Vec<Listener<'a>>,
31    pub features: Vec<Feature<'a>>,
32    pub unknown_tagged_fields: UnknownTaggedFields,
33}
34impl ControllerRegistrationRequest<'_> {
35    pub fn to_owned(
36        &self,
37    ) -> crate::owned::controller_registration_request::ControllerRegistrationRequest {
38        crate::owned::controller_registration_request::ControllerRegistrationRequest {
39            controller_id: (self.controller_id),
40            incarnation_id: (self.incarnation_id),
41            zk_migration_ready: (self.zk_migration_ready),
42            listeners: (self.listeners).iter().map(Listener::to_owned).collect(),
43            features: (self.features).iter().map(Feature::to_owned).collect(),
44            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
45        }
46    }
47}
48impl Encode for ControllerRegistrationRequest<'_> {
49    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
50        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
51            return Err(ProtocolError::UnsupportedVersion {
52                api_key: API_KEY,
53                version,
54            });
55        }
56        let flex = is_flexible(version);
57        if version >= 0 {
58            put_i32(buf, self.controller_id);
59        }
60        if version >= 0 {
61            crate::primitives::uuid::put_uuid(buf, self.incarnation_id);
62        }
63        if version >= 0 {
64            put_bool(buf, self.zk_migration_ready);
65        }
66        if version >= 0 {
67            {
68                crate::primitives::array::put_array_len(buf, (self.listeners).len(), flex);
69                for it in &self.listeners {
70                    it.encode(buf, version)?;
71                }
72            }
73        }
74        if version >= 0 {
75            {
76                crate::primitives::array::put_array_len(buf, (self.features).len(), flex);
77                for it in &self.features {
78                    it.encode(buf, version)?;
79                }
80            }
81        }
82        if flex {
83            let tagged = WriteTaggedFields::new();
84            tagged.write(buf, &self.unknown_tagged_fields);
85        }
86        Ok(())
87    }
88    fn encoded_len(&self, version: i16) -> usize {
89        let flex = is_flexible(version);
90        let mut n: usize = 0;
91        if version >= 0 {
92            n += 4;
93        }
94        if version >= 0 {
95            n += 16;
96        }
97        if version >= 0 {
98            n += 1;
99        }
100        if version >= 0 {
101            n += {
102                let prefix =
103                    crate::primitives::array::array_len_prefix_len((self.listeners).len(), flex);
104                let body: usize = (self.listeners)
105                    .iter()
106                    .map(|it| it.encoded_len(version))
107                    .sum();
108                prefix + body
109            };
110        }
111        if version >= 0 {
112            n += {
113                let prefix =
114                    crate::primitives::array::array_len_prefix_len((self.features).len(), flex);
115                let body: usize = (self.features)
116                    .iter()
117                    .map(|it| it.encoded_len(version))
118                    .sum();
119                prefix + body
120            };
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<'de> DecodeBorrow<'de> for ControllerRegistrationRequest<'de> {
130    fn decode_borrow(buf: &mut &'de [u8], 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.controller_id = get_i32(buf)?;
141        }
142        if version >= 0 {
143            out.incarnation_id = crate::primitives::uuid::get_uuid(buf)?;
144        }
145        if version >= 0 {
146            out.zk_migration_ready = get_bool(buf)?;
147        }
148        if version >= 0 {
149            out.listeners = {
150                let n = crate::primitives::array::get_array_len(buf, flex)?;
151                let mut v = Vec::with_capacity(n);
152                for _ in 0..n {
153                    v.push(Listener::decode_borrow(buf, version)?);
154                }
155                v
156            };
157        }
158        if version >= 0 {
159            out.features = {
160                let n = crate::primitives::array::get_array_len(buf, flex)?;
161                let mut v = Vec::with_capacity(n);
162                for _ in 0..n {
163                    v.push(Feature::decode_borrow(buf, version)?);
164                }
165                v
166            };
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 ControllerRegistrationRequest<'_> {
176    #[must_use]
177    pub fn populated(version: i16) -> Self {
178        let mut m = Self::default();
179        if version >= 0 {
180            m.controller_id = 1i32;
181        }
182        if version >= 0 {
183            m.incarnation_id = crate::primitives::uuid::Uuid([1u8; 16]);
184        }
185        if version >= 0 {
186            m.zk_migration_ready = true;
187        }
188        if version >= 0 {
189            m.listeners = vec![Listener::populated(version)];
190        }
191        if version >= 0 {
192            m.features = vec![Feature::populated(version)];
193        }
194        m
195    }
196}
197#[derive(Debug, Clone, PartialEq, Eq, Default)]
198pub struct Listener<'a> {
199    pub name: &'a str,
200    pub host: &'a str,
201    pub port: u16,
202    pub security_protocol: i16,
203    pub unknown_tagged_fields: UnknownTaggedFields,
204}
205impl Listener<'_> {
206    pub fn to_owned(&self) -> crate::owned::controller_registration_request::Listener {
207        crate::owned::controller_registration_request::Listener {
208            name: (self.name).to_string(),
209            host: (self.host).to_string(),
210            port: (self.port),
211            security_protocol: (self.security_protocol),
212            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
213        }
214    }
215}
216impl Encode for Listener<'_> {
217    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
218        let flex = version >= 0;
219        if version >= 0 {
220            if flex {
221                put_compact_string(buf, self.name);
222            } else {
223                put_string(buf, self.name);
224            }
225        }
226        if version >= 0 {
227            if flex {
228                put_compact_string(buf, self.host);
229            } else {
230                put_string(buf, self.host);
231            }
232        }
233        if version >= 0 {
234            put_u16(buf, self.port);
235        }
236        if version >= 0 {
237            put_i16(buf, self.security_protocol);
238        }
239        if flex {
240            let tagged = WriteTaggedFields::new();
241            tagged.write(buf, &self.unknown_tagged_fields);
242        }
243        Ok(())
244    }
245    fn encoded_len(&self, version: i16) -> usize {
246        let flex = version >= 0;
247        let mut n: usize = 0;
248        if version >= 0 {
249            n += if flex {
250                compact_string_len(self.name)
251            } else {
252                string_len(self.name)
253            };
254        }
255        if version >= 0 {
256            n += if flex {
257                compact_string_len(self.host)
258            } else {
259                string_len(self.host)
260            };
261        }
262        if version >= 0 {
263            n += 2;
264        }
265        if version >= 0 {
266            n += 2;
267        }
268        if flex {
269            let known_pairs: Vec<(u32, usize)> = Vec::new();
270            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
271        }
272        n
273    }
274}
275impl<'de> DecodeBorrow<'de> for Listener<'de> {
276    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
277        let flex = version >= 0;
278        let mut out = Self::default();
279        if version >= 0 {
280            out.name = if flex {
281                get_compact_string_borrowed(buf)?
282            } else {
283                get_string_borrowed(buf)?
284            };
285        }
286        if version >= 0 {
287            out.host = if flex {
288                get_compact_string_borrowed(buf)?
289            } else {
290                get_string_borrowed(buf)?
291            };
292        }
293        if version >= 0 {
294            out.port = get_u16(buf)?;
295        }
296        if version >= 0 {
297            out.security_protocol = get_i16(buf)?;
298        }
299        if flex {
300            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
301        }
302        Ok(out)
303    }
304}
305#[cfg(test)]
306impl Listener<'_> {
307    #[must_use]
308    pub fn populated(version: i16) -> Self {
309        let mut m = Self::default();
310        if version >= 0 {
311            m.name = "x";
312        }
313        if version >= 0 {
314            m.host = "x";
315        }
316        if version >= 0 {
317            m.port = 1u16;
318        }
319        if version >= 0 {
320            m.security_protocol = 1i16;
321        }
322        m
323    }
324}
325#[derive(Debug, Clone, PartialEq, Eq, Default)]
326pub struct Feature<'a> {
327    pub name: &'a str,
328    pub min_supported_version: i16,
329    pub max_supported_version: i16,
330    pub unknown_tagged_fields: UnknownTaggedFields,
331}
332impl Feature<'_> {
333    pub fn to_owned(&self) -> crate::owned::controller_registration_request::Feature {
334        crate::owned::controller_registration_request::Feature {
335            name: (self.name).to_string(),
336            min_supported_version: (self.min_supported_version),
337            max_supported_version: (self.max_supported_version),
338            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
339        }
340    }
341}
342impl Encode for Feature<'_> {
343    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
344        let flex = version >= 0;
345        if version >= 0 {
346            if flex {
347                put_compact_string(buf, self.name);
348            } else {
349                put_string(buf, self.name);
350            }
351        }
352        if version >= 0 {
353            put_i16(buf, self.min_supported_version);
354        }
355        if version >= 0 {
356            put_i16(buf, self.max_supported_version);
357        }
358        if flex {
359            let tagged = WriteTaggedFields::new();
360            tagged.write(buf, &self.unknown_tagged_fields);
361        }
362        Ok(())
363    }
364    fn encoded_len(&self, version: i16) -> usize {
365        let flex = version >= 0;
366        let mut n: usize = 0;
367        if version >= 0 {
368            n += if flex {
369                compact_string_len(self.name)
370            } else {
371                string_len(self.name)
372            };
373        }
374        if version >= 0 {
375            n += 2;
376        }
377        if version >= 0 {
378            n += 2;
379        }
380        if flex {
381            let known_pairs: Vec<(u32, usize)> = Vec::new();
382            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
383        }
384        n
385    }
386}
387impl<'de> DecodeBorrow<'de> for Feature<'de> {
388    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
389        let flex = version >= 0;
390        let mut out = Self::default();
391        if version >= 0 {
392            out.name = if flex {
393                get_compact_string_borrowed(buf)?
394            } else {
395                get_string_borrowed(buf)?
396            };
397        }
398        if version >= 0 {
399            out.min_supported_version = get_i16(buf)?;
400        }
401        if version >= 0 {
402            out.max_supported_version = get_i16(buf)?;
403        }
404        if flex {
405            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
406        }
407        Ok(out)
408    }
409}
410#[cfg(test)]
411impl Feature<'_> {
412    #[must_use]
413    pub fn populated(version: i16) -> Self {
414        let mut m = Self::default();
415        if version >= 0 {
416            m.name = "x";
417        }
418        if version >= 0 {
419            m.min_supported_version = 1i16;
420        }
421        if version >= 0 {
422            m.max_supported_version = 1i16;
423        }
424        m
425    }
426}