Skip to main content

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

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