Skip to main content

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

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