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