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