Skip to main content

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

1// AUTO-GENERATED by crabka-protocol-codegen against a9ce3221537b8653448750697915607dc7936cf3. Do not edit.
2
3use bytes::BufMut;
4
5use crate::primitives::fixed::{get_i32, put_i32};
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 = 76;
18pub const MIN_VERSION: i16 = 1;
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, Default)]
28pub struct ShareGroupHeartbeatRequest<'a> {
29    pub group_id: &'a str,
30    pub member_id: &'a str,
31    pub member_epoch: i32,
32    pub rack_id: Option<&'a str>,
33    pub subscribed_topic_names: Option<Vec<&'a str>>,
34    pub unknown_tagged_fields: UnknownTaggedFields,
35}
36impl ShareGroupHeartbeatRequest<'_> {
37    pub fn to_owned(
38        &self,
39    ) -> crate::owned::share_group_heartbeat_request::ShareGroupHeartbeatRequest {
40        crate::owned::share_group_heartbeat_request::ShareGroupHeartbeatRequest {
41            group_id: (self.group_id).to_string(),
42            member_id: (self.member_id).to_string(),
43            member_epoch: (self.member_epoch),
44            rack_id: (self.rack_id).map(std::string::ToString::to_string),
45            subscribed_topic_names: (self.subscribed_topic_names)
46                .as_ref()
47                .map(|v| v.iter().map(std::string::ToString::to_string).collect()),
48            unknown_tagged_fields: self.unknown_tagged_fields.clone(),
49        }
50    }
51}
52impl Encode for ShareGroupHeartbeatRequest<'_> {
53    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
54        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
55            return Err(ProtocolError::UnsupportedVersion {
56                api_key: API_KEY,
57                version,
58            });
59        }
60        let flex = is_flexible(version);
61        if version >= 0 {
62            if flex {
63                put_compact_string(buf, self.group_id);
64            } else {
65                put_string(buf, self.group_id);
66            }
67        }
68        if version >= 0 {
69            if flex {
70                put_compact_string(buf, self.member_id);
71            } else {
72                put_string(buf, self.member_id);
73            }
74        }
75        if version >= 0 {
76            put_i32(buf, self.member_epoch);
77        }
78        if version >= 0 {
79            if flex {
80                put_compact_nullable_string(buf, self.rack_id);
81            } else {
82                put_nullable_string(buf, self.rack_id);
83            }
84        }
85        if version >= 0 {
86            {
87                let len = (self.subscribed_topic_names).as_ref().map(Vec::len);
88                crate::primitives::array::put_nullable_array_len(buf, len, flex);
89                if let Some(v) = &self.subscribed_topic_names {
90                    for it in v {
91                        if flex {
92                            put_compact_string(buf, it);
93                        } else {
94                            put_string(buf, it);
95                        }
96                    }
97                }
98            }
99        }
100        if flex {
101            let tagged = WriteTaggedFields::new();
102            tagged.write(buf, &self.unknown_tagged_fields);
103        }
104        Ok(())
105    }
106    fn encoded_len(&self, version: i16) -> usize {
107        let flex = is_flexible(version);
108        let mut n: usize = 0;
109        if version >= 0 {
110            n += if flex {
111                compact_string_len(self.group_id)
112            } else {
113                string_len(self.group_id)
114            };
115        }
116        if version >= 0 {
117            n += if flex {
118                compact_string_len(self.member_id)
119            } else {
120                string_len(self.member_id)
121            };
122        }
123        if version >= 0 {
124            n += 4;
125        }
126        if version >= 0 {
127            n += if flex {
128                compact_nullable_string_len(self.rack_id)
129            } else {
130                nullable_string_len(self.rack_id)
131            };
132        }
133        if version >= 0 {
134            n += {
135                let opt: Option<&Vec<_>> = (self.subscribed_topic_names).as_ref();
136                let prefix = crate::primitives::array::nullable_array_len_prefix_len(
137                    opt.map(std::vec::Vec::len),
138                    flex,
139                );
140                let body: usize = opt.map_or(0, |v| {
141                    v.iter()
142                        .map(|it| {
143                            if flex {
144                                compact_string_len(it)
145                            } else {
146                                string_len(it)
147                            }
148                        })
149                        .sum()
150                });
151                prefix + body
152            };
153        }
154        if flex {
155            let known_pairs: Vec<(u32, usize)> = Vec::new();
156            n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
157        }
158        n
159    }
160}
161impl<'de> DecodeBorrow<'de> for ShareGroupHeartbeatRequest<'de> {
162    fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
163        if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
164            return Err(ProtocolError::UnsupportedVersion {
165                api_key: API_KEY,
166                version,
167            });
168        }
169        let flex = is_flexible(version);
170        let mut out = Self::default();
171        if version >= 0 {
172            out.group_id = if flex {
173                get_compact_string_borrowed(buf)?
174            } else {
175                get_string_borrowed(buf)?
176            };
177        }
178        if version >= 0 {
179            out.member_id = if flex {
180                get_compact_string_borrowed(buf)?
181            } else {
182                get_string_borrowed(buf)?
183            };
184        }
185        if version >= 0 {
186            out.member_epoch = get_i32(buf)?;
187        }
188        if version >= 0 {
189            out.rack_id = if flex {
190                get_compact_nullable_string_borrowed(buf)?
191            } else {
192                get_nullable_string_borrowed(buf)?
193            };
194        }
195        if version >= 0 {
196            out.subscribed_topic_names = {
197                let opt = crate::primitives::array::get_nullable_array_len(buf, flex)?;
198                match opt {
199                    None => None,
200                    Some(n) => {
201                        let mut v = Vec::with_capacity(n);
202                        for _ in 0..n {
203                            v.push(if flex {
204                                get_compact_string_borrowed(buf)?
205                            } else {
206                                get_string_borrowed(buf)?
207                            });
208                        }
209                        Some(v)
210                    }
211                }
212            };
213        }
214        if flex {
215            out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
216        }
217        Ok(out)
218    }
219}
220#[cfg(test)]
221impl ShareGroupHeartbeatRequest<'_> {
222    #[must_use]
223    pub fn populated(version: i16) -> Self {
224        let mut m = Self::default();
225        if version >= 0 {
226            m.group_id = "x";
227        }
228        if version >= 0 {
229            m.member_id = "x";
230        }
231        if version >= 0 {
232            m.member_epoch = 1i32;
233        }
234        if version >= 0 {
235            m.rack_id = Some("x");
236        }
237        if version >= 0 {
238            m.subscribed_topic_names = Some(vec!["x"]);
239        }
240        m
241    }
242}