crabka_protocol/opt/rustwide/workdir/generated/
HeartbeatRequest.borrowed.rs1use 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 = 12;
18pub const MIN_VERSION: i16 = 0;
19pub const MAX_VERSION: i16 = 4;
20pub const FLEXIBLE_MIN: i16 = 4;
21
22#[inline]
23fn is_flexible(version: i16) -> bool {
24 version >= FLEXIBLE_MIN
25}
26
27#[derive(Debug, Clone, PartialEq, Eq, Default)]
28pub struct HeartbeatRequest<'a> {
29 pub group_id: &'a str,
30 pub generation_id: i32,
31 pub member_id: &'a str,
32 pub group_instance_id: Option<&'a str>,
33 pub unknown_tagged_fields: UnknownTaggedFields,
34}
35impl HeartbeatRequest<'_> {
36 pub fn to_owned(&self) -> crate::owned::heartbeat_request::HeartbeatRequest {
37 crate::owned::heartbeat_request::HeartbeatRequest {
38 group_id: (self.group_id).to_string(),
39 generation_id: (self.generation_id),
40 member_id: (self.member_id).to_string(),
41 group_instance_id: (self.group_instance_id).map(std::string::ToString::to_string),
42 unknown_tagged_fields: self.unknown_tagged_fields.clone(),
43 }
44 }
45}
46impl Encode for HeartbeatRequest<'_> {
47 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
48 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
49 return Err(ProtocolError::UnsupportedVersion {
50 api_key: API_KEY,
51 version,
52 });
53 }
54 let flex = is_flexible(version);
55 if version >= 0 {
56 if flex {
57 put_compact_string(buf, self.group_id);
58 } else {
59 put_string(buf, self.group_id);
60 }
61 }
62 if version >= 0 {
63 put_i32(buf, self.generation_id);
64 }
65 if version >= 0 {
66 if flex {
67 put_compact_string(buf, self.member_id);
68 } else {
69 put_string(buf, self.member_id);
70 }
71 }
72 if version >= 3 {
73 if flex {
74 put_compact_nullable_string(buf, self.group_instance_id);
75 } else {
76 put_nullable_string(buf, self.group_instance_id);
77 }
78 }
79 if flex {
80 let tagged = WriteTaggedFields::new();
81 tagged.write(buf, &self.unknown_tagged_fields);
82 }
83 Ok(())
84 }
85 fn encoded_len(&self, version: i16) -> usize {
86 let flex = is_flexible(version);
87 let mut n: usize = 0;
88 if version >= 0 {
89 n += if flex {
90 compact_string_len(self.group_id)
91 } else {
92 string_len(self.group_id)
93 };
94 }
95 if version >= 0 {
96 n += 4;
97 }
98 if version >= 0 {
99 n += if flex {
100 compact_string_len(self.member_id)
101 } else {
102 string_len(self.member_id)
103 };
104 }
105 if version >= 3 {
106 n += if flex {
107 compact_nullable_string_len(self.group_instance_id)
108 } else {
109 nullable_string_len(self.group_instance_id)
110 };
111 }
112 if flex {
113 let known_pairs: Vec<(u32, usize)> = Vec::new();
114 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
115 }
116 n
117 }
118}
119impl<'de> DecodeBorrow<'de> for HeartbeatRequest<'de> {
120 fn decode_borrow(buf: &mut &'de [u8], version: i16) -> Result<Self, ProtocolError> {
121 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
122 return Err(ProtocolError::UnsupportedVersion {
123 api_key: API_KEY,
124 version,
125 });
126 }
127 let flex = is_flexible(version);
128 let mut out = Self::default();
129 if version >= 0 {
130 out.group_id = if flex {
131 get_compact_string_borrowed(buf)?
132 } else {
133 get_string_borrowed(buf)?
134 };
135 }
136 if version >= 0 {
137 out.generation_id = get_i32(buf)?;
138 }
139 if version >= 0 {
140 out.member_id = if flex {
141 get_compact_string_borrowed(buf)?
142 } else {
143 get_string_borrowed(buf)?
144 };
145 }
146 if version >= 3 {
147 out.group_instance_id = if flex {
148 get_compact_nullable_string_borrowed(buf)?
149 } else {
150 get_nullable_string_borrowed(buf)?
151 };
152 }
153 if flex {
154 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
155 }
156 Ok(out)
157 }
158}
159#[cfg(test)]
160impl HeartbeatRequest<'_> {
161 #[must_use]
162 pub fn populated(version: i16) -> Self {
163 let mut m = Self::default();
164 if version >= 0 {
165 m.group_id = "x";
166 }
167 if version >= 0 {
168 m.generation_id = 1i32;
169 }
170 if version >= 0 {
171 m.member_id = "x";
172 }
173 if version >= 3 {
174 m.group_instance_id = Some("x");
175 }
176 m
177 }
178}