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