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